Reputation: 11
I use Beego for my project, but after setting up the project, I only get 404 errors... My main.go is ok, as well as my router. Any idea of the problem, I've been looking for several hours, in vain...
main.go
import (
_ "bealinkapi/routers"
"github.com/beego/beego/v2/client/orm"
beego "github.com/beego/beego/v2/server/web"
_ "github.com/lib/pq"
"time"
)
func main() {
orm.RegisterDriver("Postgres", orm.DRPostgres)
orm.DefaultTimeLoc = time.UTC
o, _ := beego.AppConfig.String("sqlconn")
err := orm.RegisterDataBase("MyAppName", "postgres", o)
if err != nil {
panic(err)
}
if beego.BConfig.RunMode == "dev" {
beego.BConfig.WebConfig.DirectoryIndex = true
beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
}
beego.Run()
}
router.go
// @APIVersion 1.0.0
// @Title beego Test API
// @Description beego has a very cool tools to autogenerate documents for your API
// @Contact [email protected]
// @TermsOfServiceUrl http://beego.me/
// @License Apache 2.0
// @LicenseUrl http://www.apache.org/licenses/LICENSE-2.0.html
package routers
import (
"bealinkapi/controllers"
beego "github.com/beego/beego/v2/server/web"
)
func init() {
ns := beego.NewNamespace("/v1",
beego.NSNamespace("/bl_user",
beego.NSInclude(
&controllers.BlUserController{},
),
),
)
beego.AddNamespace(ns)
}
console:
2022/04/11 15:04:55.627 [D] [router.go:1272] | 127.0.0.1| 404 | 265.248µs| nomatch| GET /v1/bl_user/1
Upvotes: 1
Views: 749
Reputation: 11
Check the file named commentsRouter_controller.go Seems this file is missing in your project.
commentsRouter_controller.go is generated automatically. If not please check this link for more info https://github.com/beego/beego/issues/2520
Upvotes: 1
Reputation: 31
I had the same issue. From Bee V2.0.2 you have to generate the routers using an extra command.
Try this:
bee generate routers
bee run
Upvotes: 1
Reputation: 71
I had a similar issue when using the swagger interface to test the api calls, but when I did a postman call it was normal, for some reason the problem was with swagger. Also when I created new namespaces and routers everything worked as expected when testing in postman.
Upvotes: 1