调用/调用子项目的最佳Golang方法

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

Golang best way of calling/invoking sub project

问题

我正在将我的项目从PHP迁移到Golang,我正在寻找一种高效的方式来从srcmain.go调用/调用/处理sub projectmain.go。我想要实现以下控制转移:

http://localhost/  =>   http://localhost/sub-project1/
http://localhost/  =>   http://localhost/sub-project2/
http://localhost/  =>   http://localhost/sub-projectn/

我对Golang还不熟悉,不知道最佳实践是什么,我的项目结构如下:

src/
    main.go
    sub-project1/
        main.go
    sub-project2/
        main.go
    sub-projectn/
        main.go
    gitHub.com/
        ......
    golang.org/
        ......

我在src下的main.go中使用httprouter进行路由,包含以下内容:

package main
import ....

// homePageHandler
// contactPageHandler
// aboutPageHandler
// loginPageHandler
// signupPageHandler

func main() {
    router := httprouter.New()
    router.GET("/", homePageHandler)
    router.GET("/contact", contactPageHandler)
    router.GET("/about", aboutPageHandler)
    router.GET("/login", loginPageHandler)
    router.GET("/signup", signupPageHandler)

    // 在这里,我想要将控制权传递给我的子项目main.go
    // 我不想在这里编写任何/sub-project的路由URL,
    // 因为每个/sub-project都包含许多URL
    router.GET("/sub-project1", ??????)
    router.GET("/sub-project2", ??????)
    router.GET("/sub-project3", ??????)
    router.GET("/sub-projectn", ??????)
}

所有文件都必须从srcmain.go传递,因为整个项目只有一个main()函数,在任何/sub-projectxmain.go中,我想要实现以下内容:

package main
import ....
// subprojectPageHandler
// feature1PageHandler
// feature2PageHandler
// feature3PageHandler
// ........
// featurenPageHandler

func main() {
    router := httprouter.New()
    router.GET("/sub-projectx", subprojectPageHandler)
    router.GET("/sub-projectx/feature1", feature1PageHandler)
    router.GET("/sub-projectx/feature2", feature2PageHandler)
    router.GET("/sub-projectx/feature3", feature3PageHandler)
    ..........
    router.GET("/sub-projectx/featureN", featureNPageHandler)

    // 在这里,我想要将控制权传递给我的子项目main.go
    // 我不想在这里编写任何/sub-project的路由URL,
    // 因为每个/sub-project都包含许多URL
    router.GET("/sub-project1", ??????)
    router.GET("/sub-project2", ??????)
    router.GET("/sub-project3", ??????)
    router.GET("/sub-projectn", ??????)
}
英文:

Im moving my project from PHP to Golang and I looking efficient way of calling/invoking/handling control to sub project main.go from src main.go, I want to pass control from

http://localhost/  =>   http://localhost/sub-project1/
http://localhost/  =>   http://localhost/sub-project2/
http://localhost/  =>   http://localhost/sub-projectn/

I'm new to Golang I don't know how to do it in best way, and my project structure is

src/
    main.go
    sub-project1/
        main.go
    sub-project2/
        main.go
    sub-projectn/
        main.go
    gitHub.com/
        ......
    golang.org/
        ......

I'm using httprouter for routing, in main.go which is located under src contain following

package main
import ....

// homePageHandler
// contactPageHandler
// aboutPageHandler
// loginPageHandler
// signupPageHandler

func main() {
	router := httprouter.New()
	router.GET("/", homePageHandler)
	router.GET("/contact", contactPageHandler)
	router.GET("/about", aboutPageHandler)
	router.GET("/login", loginPageHandler)
	router.GET("/signup", signupPageHandler)

    // here I want to pass control to my sub project main.go 
    // and I don't want to write any /sub-project routing urls here,
    // because each /sub-project's contain many urls 
    router.GET("/sub-project1", ??????)
    router.GET("/sub-project2", ??????)
    router.GET("/sub-project3", ??????)
    router.GET("/sub-projectn", ??????)
}

and all files must be passes from src main.go because whole project has only one main() and inside any /sub-projectx main.go I want to do this

package main
import ....
// subprojectPageHandler
// feature1PageHandler
// feature2PageHandler
// feature3PageHandler
// ........
// featurenPageHandler

func main() {
	router := httprouter.New()
	router.GET("/sub-projectx", subprojectPageHandler)
	router.GET("/sub-projectx/feature1", feature1PageHandler)
	router.GET("/sub-projectx/feature2", feature2PageHandler)
	router.GET("/sub-projectx/feature3", feature3PageHandler)
    ..........
	router.GET("/sub-projectx/featureN", featureNPageHandler)


    // here I want to pass control to my sub project main.go 
    // and I don't want to write any /sub-project routing urls here,
    // because each /sub-project's contain many urls 
    router.GET("/sub-project1", ??????)
    router.GET("/sub-project2", ??????)
    router.GET("/sub-project3", ??????)
    router.GET("/sub-projectn", ??????)
}

答案1

得分: 3

是还是不是

Golang源代码不通过解释器运行,而是构建成二进制文件,这在动态项目中灵活性较低。因此,我会将我的项目相互隔离,并让Nginx(例如)负责多个项目的分组。当然,这需要进行一些重构,比如创建共享包等。

或者不是

如果出于某种原因,你仍然认为通过单个二进制文件运行多个项目是可以的,那就是你的选择。在这种情况下,你可以看看一些框架提供的路由分组功能。这里是Go Gin提供的示例

func main() {
    router := gin.Default()

    // 简单分组:v1
    v1 := router.Group("/v1")
    {
        v1.POST("/login", loginEndpoint)
        v1.POST("/submit", submitEndpoint)
        v1.POST("/read", readEndpoint)
    }

    // 简单分组:v2
    v2 := router.Group("/v2")
    {
        v2.POST("/login", loginEndpoint)
        v2.POST("/submit", submitEndpoint)
        v2.POST("/read", readEndpoint)
    }

    router.Run(":8080")
}
英文:

To be

Golang source code is not running through interpreter, but is built into a binary, which gives less flexibility in case of dynamic projects. That said, I'd keep my projects isolated one from another, and would let Nginx (for example) take care of multiple project grouping. Of course, that would require some refactoring like creating shared packages, etc.

Or not to be

If, for some reason, you still think running multiple projects via single binary is ok, it's your choice. In this case you might have a look into route grouping that are available in some frameworks. Here's what Go Gin provides:

func main() {
	router := gin.Default()

	// Simple group: v1
	v1 := router.Group("/v1")
	{
		v1.POST("/login", loginEndpoint)
		v1.POST("/submit", submitEndpoint)
		v1.POST("/read", readEndpoint)
	}

	// Simple group: v2
	v2 := router.Group("/v2")
	{
		v2.POST("/login", loginEndpoint)
		v2.POST("/submit", submitEndpoint)
		v2.POST("/read", readEndpoint)
	}

	router.Run(":8080")
}

huangapple
  • 本文由 发表于 2017年5月3日 12:51:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/43751322.html
匿名

发表评论

匿名网友

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

确定