2

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.

  • An extra writeheader (while not pretty) does not look like the cause of a panic. Can you post more of the code since the current sample seems to be lacking any implementation. – Norbert Jul 22 '22 at 21:52

1 Answers1

1

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.

  • This is mentioned [in the docs](https://doc.getqor.com/admin/deploy.html) (with [more info here](https://doc.getqor.com/admin/theming_and_customization.html)). Not really anything to do with modules; just the way this is written (it needs templates and, to simplify development, assumes its running on a machine with the package installed). Either copy the files, as you have, or embed them in the executable. – Brits Jul 24 '22 at 04:04
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 31 '22 at 22:08