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", ??????)
}
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")
}
Related
I am switching to Go from Python/Django. In Django I really liked about its modular Apps project structure design, where each App would have separate bussiness models, routes and views. All the apps would then communicate within the central/project's main routing system and so on.
Django project structure Eg:
- myproject
- myproject
- urls.py
- views.py
...
- planner
- urls.py
- views.py
- models.py
...
I am trying to achieve similar project design in Go project:
- myproject
- cmd
- api
- main.go
- routes.go
- handlers.go
- planner
- routes.go
- handlers.go
- models.go
Excerpt from cmd/api/main.go:
package main
...
db, err := sql.Open("pgx", cfg.db.dsn)
...
srv := &http.Server{
Addr: fmt.Sprintf("localhost:%d", app.config.port),
Handler: app.routes()
}
...
Excerpt from cmd/api/routes.go:
package main
func (app *application) routes() *httprouter.Router {
router := httprouter.New()
planner.Routes(router)
return router
}
Excerpt from cmd/planner/routes.go:
package planner
...
func Routes(router *httprouter.Router) {
router.HandlerFunc(http.MethodPost, "/v1/planner/todos", CreateTodoHandler)
}
Excerpt from cmd/planner/models.go:
package planner
type PlannerModel struct {
DB *sql.DB
}
func (p PlannerModel) InsertTodo(todo *Todo) error {
query := `INSERT INTO todos (title, user_id)
VALUES ($1, $2)
RETURNING id, created_at`
return p.DB.QueryRow(query, todo.Title, todo.UserID).Scan(&todo.ID, &todo.CreatedAt)
}
Now the problem is I need to use the DB connection initialized in cmd/api/main.go file from package main into cmd/planner/handlers.go. Since the variable is from main package I cannot import it into my app's (planner) handler functions.
package planner
func CreateTodoHandler(w http.ResponseWriter, r *http.Request) {
var input struct {
Title string `json:"title"`
UserID int64 `json:"user_id"`
}
err := helpers.ReadJSON(w, r, &input)
...
todo := &Todo{
Title: input.Title,
UserID: input.UserID,
}
...
// How to use/inject DB connection dependency into the PlannerModel?
pm := PlannerModel{
DB: // ????
}
err = pm.InsertTodo(todo)
...
}
I think having a global DB variable solves the problem or a reasonable answer I found was to declare the variable outside in a separate package, and initialize it in main.go. Another approach would be to make the Planner/handlers.go to be of package main and create an application struct in the main package to hold all the models of the project and use it inside the handlers, but I guess this would defeat the decoupled architecture design.
I was wondering what is the preferred way or if there are better ways to go about having similar decoupled project structure like Django, and still use variables initialized in main.go into other packages and do tests?
I had a similar experience when I switched from Python/Django to Go.
The solution to access the db connection in every app is to define structs in each app having a field for db connection, and then create the db connection and all the apps structs in the main.
// planner.go
func (t *Todo) Routes(router *httprouter.Router) {
router.HandlerFunc(http.MethodPost, "/v1/planner/todos", t.CreateTodoHandler)
}
// handlers.go
struct Todo {
DB: *sql.DB
}
func (t *Todo) CreateTodo(w http.ResponseWriter, r *http.Request) {
var input struct {
Title string `json:"title"`
UserID int64 `json:"user_id"`
}
err := helpers.ReadJSON(w, r, &input)
...
todo := &Todo{
Title: input.Title,
UserID: input.UserID,
}
...
pm := PlannerModel{
DB: t.DB
}
err = pm.InsertTodo(todo)
...
}
This would solve your current problem but other problems will arise if you don't have a better design for your application.
I'd recommend reading these two blog posts to better understand designing applications and structuring code in Go.
https://www.gobeyond.dev/standard-package-layout/
https://www.gobeyond.dev/packages-as-layers/
My way is using clean architecture with DI.
Example of repository constructor: https://github.com/zubroide/go-api-boilerplate/blob/master/model/repository/user_repository.go#L16-L19
Example of declaring of repository with db connection: https://github.com/zubroide/go-api-boilerplate/blob/master/dic/app.go#L58-L63
Example of using repository in the service declaration: https://github.com/zubroide/go-api-boilerplate/blob/master/dic/app.go#L65-L70
This looks similar with other DI's, for example wire.
Pluses are:
no problems with cyclic dependencies,
simplifying of services dependencies support.
I just learnt how to use the httprouter go package and read many documents about it but
failed to use the :name style of passing paramenters toe templates when it comes to the index
page template.
ex.
My router code:
func getRouter() *httprouter.Router {
// Load and parse templates (from binary or disk)
templateBox = rice.MustFindBox("templates")
templateBox.Walk("", newTemplate)
// mux handler
router := httprouter.New()
// Example route that encounters an error
router.GET("/broken/handler", broken)
// Index route
router.GET("/:email", index)
router.GET("/read/all", readingHandler)
router.POST("/submit/newcon", Handler1)
router.POST("/read/newcon", Handler2)
// Serve static assets via the "static" directory
fs := rice.MustFindBox("static").HTTPBox()
router.ServeFiles("/static/*filepath", fs)
return router
}
Then i get this error:
panic: wildcard segment ':email' conflicts with existing children in path '/:email'
so it should work with /user/:email instead of just /:email.
I have a simple Go application, it has a few template files where I render some text. After I build my binary with Go build I try to run the file and I get the error:
panic: html/template: pattern matches no files: public/*.html
I am using the Echo framework and have followed their steps on adding a render for templates.
Here is the code in my main.go file
// TemplateRenderer is a custom html/template renderer for Echo framework
type TemplateRenderer struct {
templates *template.Template
}
// Render renders a template document
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
// Create a new instance of Echo
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
renderer := &TemplateRenderer{
templates: template.Must(template.ParseGlob("public/*.html")),
}
e.Renderer = renderer
e.GET("/", func(context echo.Context) error {
return context.Render(http.StatusOK, "index.html", api.FetchCoinList())
})
}
Is there something I have to do to package my templates up in the binary? It works perfectly when I run go run main.go
Is there something I have to do to package my templates up in the binary?
Yes, make them avialable at the same (relative) folder where they are present when you run them with go run main.go.
For example if there is a public folder containing the templates next to your main.go, then make sure you "copy" the public folder next to your executable binary.
Read this question+answer for more options: how to reference a relative file from code and tests
Usually you should provide ways to define where to get static assets and files from. The app may have a default place to look for them, but it should be easy to change this setting (e.g. via command line flags, via environment variables or via config files).
Another option is to include static files in the executable binary. Check out this question how to do that: What's the best way to bundle static resources in a Go program?
I am newbie to golang.
To learn it I have started with a simple web app using gin framework.
I have followed the gin doc & configured template file but not able to make it work. I am getting an error -
panic: html/template: pattern matches no files: `templates/*`
goroutine 1 [running]:
html/template.Must
/usr/local/Cellar/go/1.5.2/libexec/src/html/template/template.go:330
github.com/gin-gonic/gin.(*Engine).LoadHTMLGlob
/Users/ameypatil/deployment/go/src/github.com/gin-gonic/gin/gin.go:126
main.main()
/Users/ameypatil/deployment/go/src/github.com/ameykpatil/gospike/main.go:17
Below is my code -
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
//os.Setenv("GIN_MODE", "release")
//gin.SetMode(gin.ReleaseMode)
// Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware
router := gin.Default()
router.LoadHTMLGlob("templates/*")
//router.LoadHTMLFiles("templates/index.tmpl")
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
"title": "GoSpike",
})
})
// By default it serves on :8080 unless a
// PORT environment variable was defined.
router.Run(":4848")
}
My directory structure is
- gospike
--- templates
------index.tmpl
--- main.go
go install command does not give any error
but on actually running, it gives the above error. I searched & there were similar issues logged on gin's github repo but they are closed now.
I have tried various things but I guess I am missing something obvious. What am I missing?
I'm guessing the issue is that you're using a relative filepath to access your templates.
If I compile and run your code from the gospike directory, it works fine. But if I run gospike from any other directory, I get the same error you were seeing.
So either you need to always run gospike in the parent directory of templates, or you need to use the absolute path. You could either hard code it:
router.LoadHTMLGlob("/go/src/github.com/ameykpatil/gospike/templates/*")
or you could do something like
router.LoadHTMLGlob(filepath.Join(os.Getenv("GOPATH"),
"src/github.com/ameykpatil/gospike/templates/*"))
but that will fail if you have multiple paths set in your GOPATH. A better long-term solution might be setting a special environment variable like TMPL_DIR, and then just using that:
router.LoadHTMLGlob(filepath.Join(os.Getenv("TMPL_DIR"), "*"))
use relative path glob will working, you can try to code
router.LoadHTMLGlob("./templates/*")
notice the . dot sign which meaning current directory, gin.Engine will load template
base on templates subdirectory of current directory .
Here is how I do it. This walks through the directory and collects the files marked with my template suffix which is .html & then I just include all of those. I haven't seen this answer anywhere so I thought Id post it.
// START UP THE ROUTER
router := gin.Default()
var files []string
filepath.Walk("./views", func(path string, info os.FileInfo, err error) error {
if strings.HasSuffix(path, ".html") {
files = append(files, path)
}
return nil
})
router.LoadHTMLFiles(files...)
// SERVE STATICS
router.Use(static.Serve("/css", static.LocalFile("./css", true)))
router.Use(static.Serve("/js", static.LocalFile("./js", true)))
router.Use(static.Serve("/images", static.LocalFile("./images", true)))
routers.LoadBaseRoutes(router)
routers.LoadBlog(router)
router.Run(":8080")
There is a multitemplate HTML render to support multi tempaltes.
You can use AddFromFiles to add multi files:
r.AddFromFiles("index", "templates/base.html", "templates/index.html")
r.AddFromFiles("article", "templates/base.html", "templates/index.html", "templates/article.html")
Or load multi files under a directory:
package main
import (
"path/filepath"
"github.com/gin-contrib/multitemplate"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.New()
r.HTMLRender = loadTemplates()
// ...
}
func loadTemplates() multitemplate.Render {
files, err := filepath.Glob("template/*.tmpl")
if err != nil {
panic("failed to load html templates: " + err.Error())
}
r := multitemplate.New()
// Generate our templates map from our template/ directories
for _, file := range files {
r.AddFromFiles(filepath.Base(file), file)
}
// add other html templates directly
r.Add("test.tmpl", someTemplate)
return r
}
You could see more APIs in this repo you want, hope this post help :)
I have a file called login.go and account.go
In login.go
func (api *ApiResource) test() {
fmt.Println("Works!")
}
In account.go I have:
func main () {
Res := new(ApiResource)
Res.test()
}
Buit I'm getting undefined:test error.
They both use package main and are on same src/ folder
What do I need to fix here?
If you used go run then you must pass both files to like go run login.go account.go.