Beego: Feature Request - generate commentsRouter_controllers.go without bee run

Created on 21 Mar 2017  ·  18Comments  ·  Source: astaxie/beego

Please answer these questions before submitting your issue. Thanks!

  1. What version of Go and beego are you using (bee version)?
    ├── Beego : 1.7.2
    ├── GoVersion : go1.8

  2. What operating system and processor architecture are you using (go env)?
    Mac amd64

  3. What did you do?
    As I understand, Beego support annotation in controllers and namespace routers. During runtime, it will generate the commentsRouter_controllers.go file and register the routers.
    But best practice for any generated files, is not to checkin SCM. Then on build machine, there is no easy way to generate such file today, and worse thing is build shall succeed falsely only tests will fail due to lack of routers. Running 'bee run' on build machine seems not a good idea, due to it impact application running contexts behavior.

  4. What did you expect to see?
    There should be a explicit way to generate such file without running the real application (such as bee run), integrated with "govendor generate" is preferred, for example "go:generate bee generate commentsRouter_controllers".
    Then in build scripts, we could generate the commentsRouter_controllers.go prior govendor build.

  5. What did you see instead?
    Seems commentsRouter_controllers can only be generated via 'bee run' or application running, which is not friendly for build process (without commentsRouter_controllers.go checked in SCM).

Most helpful comment

"bee generate router" That sounds great! "bee generate comments_router" probably less confusion and more accurate to describe the intention, due to user still need to provide their own router (with namespace etc).

All 18 comments

As I understand, Beego support annotation in controllers and namespace routers. During runtime, it will generate the commentsRouter_controllers.go file and register the routers.
But best practice for any generated files, is not to checkin SCM. Then on build machine, there is no easy way to generate such file today, and worse thing is build shall succeed falsely only tests will fail due to lack of routers. Running 'bee run' on build machine seems not a good idea, due to it impact application running contexts behavior.

Actually it's not generate by bee run. It will generate this files when you run in dev mode. https://github.com/astaxie/beego/blob/master/router.go#L217-L243

Thank you for your quick reply. But still same thing / limitation apply, we are looking for way to generate commentsRouter_controllers.go file without actually running the built application, because it is not good idea to run the built application on the actual build machine, which is meant to be clean and integrate/API tests are performed on other test machines.
Of course once commit the commentsRouter_controllers.go file into repo will solve this problem, but commit a generated file into repo is not good practice and lead to conflicting resolve when merging generated files...

lead to conflicting resolve when merging generated files.

I think the problem is how to make sure commentsRouter_controllers.go will not conflict. IMO this file should in repo, but the current problem is we use the map to generate this file, while it's random. So I think we need to use the order map to generate it.

Generally for generated files, we'd better not check into repo. There are other reasons than managing conflicting, for example static code analysis, formatting etc etc etc.
I think I'm not the only one wish for the "explicit" generation of commentsRouter_controllers.go file, I see other people log issue under bee tool as well.
https://github.com/beego/bee/issues/189

Note this does not saying I wish to move the annotation controller routers generation code out from the beego application, I just wish there could be a separate step (other than run application itself) to explicit control's generation.

When I search the "commentsRouter_controllers" string in this repo issues or google it, I see multiple pages people are wondering why their routers not updated or failed to generate (some due to non-Dev mode, some due to missing GOROOT etc), I believe having an explicit generation step could have more chance to self-describe the error or more intuitive for whom cares.

Thanks!

do you have any suggestion on this? add a command to bee like bee generate router?

"bee generate router" That sounds great! "bee generate comments_router" probably less confusion and more accurate to describe the intention, due to user still need to provide their own router (with namespace etc).

@astaxie Any idea when this feature "bee generate router" is going to be done?

@astaxie Is there any conclusion to this question?

Any update on this?

I have run into the same problem, the feature "bee generate router" sounds wonderful. Any update?

@astaxie any plan to develop this feature?. It's hard to maintain this file in the repo in a team environment.

I solved the problem by a temporary solution:

  • add following go generate commands at first line in your main.go
//go:generate sh -c "echo 'package routers; import \"github.com/astaxie/beego\"; func init() {beego.BConfig.RunMode = beego.DEV}' > routers/0.go"
//go:generate sh -c "echo 'package routers; import \"os\"; func init() {os.Exit(0)}' > routers/z.go"
//go:generate go run $GOFILE
//go:generate sh -c "rm routers/0.go routers/z.go"

then run go generate main.go to generate comments router

2018/06/07 19:07:53 [I] [parser.go:91] generate router from comments
2018/06/07 19:07:53 [I] [router.go:239] /path/to/project/controllers no changed
......
......

Remove move all file in routers folder exclude router.go, then run bee run command

For me commentsRouter wasn't generated in any way, even after file removal.
Turns out I was missing line in app.conf :
runmode = dev

这种奇怪的特性给CICD带来了很多麻烦,同样的代码,同样的命令在不同的环境(runmode)最终执行效果不一样

解决方法是:
1、将dev环境下的commentsRouter_controllers.go添加到代码库
2、并且每次路由更新都必须执行一次bee run,重新生成commentsRouter_controllers.go后,再提交代码,确保路由是最新的

建议:
追加bee generate router命令类似 bee generate docs
或者启动参数 bee run -genrouter=true

The GOPATH is the problem, even if you are using go mod you still need to include a GOPATH to your current project. Should be fixed in the code though..

@gadelkareem So apart from waiting for a new code release how can I include GOPATH to my current project?

Was this page helpful?
0 / 5 - 0 ratings