将Qor与Beego集成时,出现了无效的内存地址或空指针解引用错误。

huangapple go评论77阅读模式
英文:

Integrating Qor with Beego, getting invalid memory address or nil pointer dereference error

问题

我有一个使用Beego框架的简单Web应用程序。最近我发现了Qor并希望在Beego之外使用它。

我按照这个文档将Qor与我的Beego Web应用程序集成起来。

部署时一切顺利。容器构建和运行都很好,原始部分的Web应用程序都按预期工作。然而,当我尝试访问/admin页面时,出现以下错误:

beego-app:runtime error: invalid memory address or nil pointer dereference
Request Method:	GET
Request URL:	/admin
RemoteAddr:	REDACTED
Stack
/usr/local/go/src/runtime/panic.go:838
/usr/local/go/src/runtime/panic.go:220
/usr/local/go/src/runtime/signal_unix.go:818
/usr/local/go/src/html/template/template.go:97
/usr/local/go/src/html/template/template.go:121
/go/pkg/mod/github.com/qor/admin@v1.2.0/context.go:227
/go/pkg/mod/github.com/qor/admin@v1.2.0/controller.go:28
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:197
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:187
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
/go/pkg/mod/github.com/qor/admin@v1.2.0/composite_primary_key_callback.go:27
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:288
/usr/local/go/src/net/http/server.go:2462
/go/pkg/mod/github.com/astaxie/beego@v1.12.3/router.go:820
/usr/local/go/src/net/http/server.go:2916
/usr/local/go/src/net/http/server.go:1966
/usr/local/go/src/runtime/asm_amd64.s:1571
beego 1.12.3 (beego framework)

golang version: go1.18.3

这是我从GCP获取的日志:

Default
2022-07-21T20:18:54.881125Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/controller.go:28
Default
2022-07-21T20:18:54.881131Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:197
Default
2022-07-21T20:18:54.881138Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
Default
2022-07-21T20:18:54.881144Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:187
Default
2022-07-21T20:18:54.881152Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
Default
2022-07-21T20:18:54.881161Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/composite_primary_key_callback.go:27
Default
2022-07-21T20:18:54.881168Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:288
Default
2022-07-21T20:18:54.881174Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/net/http/server.go:2462
Default
2022-07-21T20:18:54.881181Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/astaxie/beego@v1.12.3/router.go:820
Default
2022-07-21T20:18:54.881248Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/net/http/server.go:2916
Default
2022-07-21T20:18:54.881336Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/net/http/server.go:1966
Default
2022-07-21T20:18:54.881346Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/runtime/asm_amd64.s:1571
Default
2022-07-21T20:18:54.881475Z2022/07/21 20:18:54.881 [server.go:3195] [HTTP] http: superfluous response.WriteHeader call from github.com/astaxie/beego/context.(*Response).WriteHeader (context.go:230)
Info
2022-07-21T20:18:54.882870ZGET2002.99 KB2 msChrome 103 https://beego-app-epcfdn7vua-uc.a.run.app/admin

[server.go:3195] [HTTP] http: superfluous response.WriteHeader call from github.com/astaxie/beego/context.(*Response).WriteHeader (context.go:230)

根据我的搜索,这与头部被多次写入有关,但我无法找到其来源。

main.go:

package main

import (
	_ "beego-app/routers"
	"net/http"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	"github.com/qor/admin"

	web "github.com/astaxie/beego"
)

// 定义一个基于GORM的模型
type User struct {
	gorm.Model
	Name string
}

// 定义另一个基于GORM的模型
type Product struct {
	gorm.Model
	Name        string
	Description string
}

func main() {
	// 设置数据库
	database, _ := gorm.Open("sqlite3", "demo.db")
	database.AutoMigrate(&User{}, &Product{})

	// 初始化
	Admin := admin.New(&admin.AdminConfig{DB: database})

	// 从GORM模型创建资源
	Admin.AddResource(&User{})
	Admin.AddResource(&Product{})

	// 将admin挂载到mux
	newMux := http.NewServeMux()
	Admin.MountTo("/admin", newMux)
	web.Handler("/admin/*", newMux)
	web.Run()
}

Dockerfile:

FROM registry.semaphoreci.com/golang:1.18 as builder

ENV APP_HOME /go/src/beego-app

WORKDIR "$APP_HOME"
COPY / .

RUN go mod download
RUN go mod verify
RUN go build -o beego-app

FROM registry.semaphoreci.com/golang:1.18

ENV APP_HOME /go/src/beego-app
RUN mkdir -p "$APP_HOME"
WORKDIR "$APP_HOME"

COPY / .
COPY --from=builder "$APP_HOME"/beego-app $APP_HOME

EXPOSE 8080
CMD ["./beego-app"]

非常感谢您的帮助。

英文:

I have a simple web app that is using the Beego framework. I have recently discovered and want to play around with using it along side Beego.

I have followed this documentation to integrate Qor with my Beego web app.

When deploying, everything goes smoothly. The container builds and runs just fine and the original parts of my web app all work as expected. However, I get the following error when attempting to access the /admin page:

beego-app:runtime error: invalid memory address or nil pointer dereference
Request Method:	GET
Request URL:	/admin
RemoteAddr:	REDACTED
Stack
/usr/local/go/src/runtime/panic.go:838
/usr/local/go/src/runtime/panic.go:220
/usr/local/go/src/runtime/signal_unix.go:818
/usr/local/go/src/html/template/template.go:97
/usr/local/go/src/html/template/template.go:121
/go/pkg/mod/github.com/qor/admin@v1.2.0/context.go:227
/go/pkg/mod/github.com/qor/admin@v1.2.0/controller.go:28
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:197
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:187
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
/go/pkg/mod/github.com/qor/admin@v1.2.0/composite_primary_key_callback.go:27
/go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:288
/usr/local/go/src/net/http/server.go:2462
/go/pkg/mod/github.com/astaxie/beego@v1.12.3/router.go:820
/usr/local/go/src/net/http/server.go:2916
/usr/local/go/src/net/http/server.go:1966
/usr/local/go/src/runtime/asm_amd64.s:1571
beego 1.12.3 (beego framework)

golang version: go1.18.3

Here is the logs I get from GCP:

Default
2022-07-21T20:18:54.881125Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/controller.go:28
Default
2022-07-21T20:18:54.881131Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:197
Default
2022-07-21T20:18:54.881138Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
Default
2022-07-21T20:18:54.881144Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:187
Default
2022-07-21T20:18:54.881152Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:38
Default
2022-07-21T20:18:54.881161Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/composite_primary_key_callback.go:27
Default
2022-07-21T20:18:54.881168Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/qor/admin@v1.2.0/route.go:288
Default
2022-07-21T20:18:54.881174Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/net/http/server.go:2462
Default
2022-07-21T20:18:54.881181Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /go/pkg/mod/github.com/astaxie/beego@v1.12.3/router.go:820
Default
2022-07-21T20:18:54.881248Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/net/http/server.go:2916
Default
2022-07-21T20:18:54.881336Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/net/http/server.go:1966
Default
2022-07-21T20:18:54.881346Z2022/07/21 20:18:54.881 [1;35m[C][0m [panic.go:838] /usr/local/go/src/runtime/asm_amd64.s:1571
Default
2022-07-21T20:18:54.881475Z2022/07/21 20:18:54.881 [server.go:3195] [HTTP] http: superfluous response.WriteHeader call from github.com/astaxie/beego/context.(*Response).WriteHeader (context.go:230)
Info
2022-07-21T20:18:54.882870ZGET2002.99 KB2 msChrome 103 https://beego-app-epcfdn7vua-uc.a.run.app/admin

[server.go:3195] [HTTP] http: superfluous response.WriteHeader call from github.com/astaxie/beego/context.(*Response).WriteHeader (context.go:230)

From my searching, this has something to do with the header being written to more than once, but I am unable to locate the source of this.

main.go:

package main

import (
	_ "beego-app/routers"
	"net/http"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/sqlite"
	"github.com/qor/admin"

	web "github.com/astaxie/beego"
)

// Define a GORM-backend model
type User struct {
	gorm.Model
	Name string
}

// Define another GORM-backend model
type Product struct {
	gorm.Model
	Name        string
	Description string
}

func main() {
	// Set up the database
	database, _ := gorm.Open("sqlite3", "demo.db")
	database.AutoMigrate(&User{}, &Product{})

	// Initalize
	Admin := admin.New(&admin.AdminConfig{DB: database})

	// Create resources from GORM-backend model
	Admin.AddResource(&User{})
	Admin.AddResource(&Product{})

	// Mount admin to the mux
	newMux := http.NewServeMux()
	Admin.MountTo("/admin", newMux)
	web.Handler("/admin/*", newMux)
	web.Run()
}

Dockerfile:

FROM registry.semaphoreci.com/golang:1.18 as builder

ENV APP_HOME /go/src/beego-app

WORKDIR "$APP_HOME"
COPY / .

RUN go mod download
RUN go mod verify
RUN go build -o beego-app

FROM registry.semaphoreci.com/golang:1.18

ENV APP_HOME /go/src/beego-app
RUN mkdir -p "$APP_HOME"
WORKDIR "$APP_HOME"

COPY / .
COPY --from=builder "$APP_HOME"/beego-app $APP_HOME

EXPOSE 8080
CMD ["./beego-app"]

Any help would be greatly appreciated.

答案1

得分: 1

我自己解决了这个问题。我并不完全理解原因,但据我所知,Qor没有使用go模块编写,因此它会抛出这个错误,因为应用程序无法找到默认的模板文件。

解决方法很简单,只需将$GOPATH/pkg/mod/github.com/qor/qor/admin/views/复制到$APP_DIR/app/views/qor/,这样应用程序就可以访问模板文件了。

英文:

Solved this myself. I don't entirely understand why, but from what I can gather, Qor was not written using go modules and therefore it was throwing this error because the app couldn't locate the default template files.

The fix was simply to copy $GOPATH/pkg/mod/github.com/qor/qor/admin/views/ to $APP_DIR/app/views/qor/ so the application can access the template files.

huangapple
  • 本文由 发表于 2022年7月22日 14:27:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/73075952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定