gobeego

Beego: Routing issues with modules


Routing doesn't work at all for me in bee if I use modules while GOPATH old approach works perfectly. I am new in golang, correct me if I did something wrong.
I have been trying to create API project with bee, but I found that all new projects have to use the modules approach released in go 1.11. I used bee api api-name command to scaffold the new project. It appeared in GOPATH. I moved out it from GOPATH path to another directory. Then I did go mod init mod, then I was able to run bee run and API was successfully started but routing didn't work. I did some comparing and found out that commentsRouter file wasn't generated. What am I do wrong?

I tried to do set GO111MODULE=on and then go get -u github.com/beego/bee but this wasn't helped as well. Now I can't even run bee API, I see 0004 Failed to build the application: main.go:4:2: package api-name/routers is not in GOROOT (c:\go\src\api-name\routers) What's happens? I am confused. Official go documentation says that I should prefer modules approach with new projects but I can't find somewhere in docs anything about how to scaffold and use bee with modules system.


Solution

  • Just because missing file commentsRouter_controllers.go. Don't know how beego does not generate file routers/commentsRouter_controllers.go when running app outside $gopath. Then we can only access to localhost/swagger. Any another path (or route) is all 404 returned.

    Solution:

    Not a good way but worked:

    1. copy source code of $your_project to $gopath/src
    2. cd $gopath/src/$your_project > bee run (just for generating file commentsRouter_controllers.go) > stop running app
    3. copy file $gopath/src/$your_project/routers/commentsRouter_controllers.go to $your_project/routers
    4. cd $your_project > bee run

    Good luck