Can gin describe route like django?
In all examples, the routers are in one place, never found about attachment.
I would like to describe the routes in the package, and in the main file is simply to write something like.
example:
r := gin.New()
r.Include("/main", here_imported_route.Route)
here_imported_route.go
package here_imported_route
Router := gin.New()
Router.Use(midl())
Router.Get("/test", hello)
and then on "/main/test" we get "hello".
in main route like here
package main
import (
"path_to_pkg/pkg"
"github.com/gin-gonic/gin"
)
var r *gin.Engine
func init() {
r = gin.New()
pkg.Concon(r.Group("/pkg"))
}
func main() {
r.Run(":8080")
}
in imported package create concatenation func
pkg.go
package pkg
import "github.com/gin-gonic/gin"
func Concon(g *gin.RouterGroup) {
g.GET("/ping", func(c *gin.Context) {
c.String(200, "pong")
})
}
open 127.0.0.1:8080/pkg/ping and get "pong"
If I understand your question correctly, I think you can accomplish this with route grouping. So you would have something like this:
r := gin.New()
main := r.Group("/main")
{
main.GET("/test", hello)
}
See more details here.
Related
Lets say I have some routes in a subemenuRoutes.go file like this:
package routes
import(
"github.com/IamFrost/api-5/pkg/middlewares"
"github.com/gin-gonic/gin"
)
// GetSubMenuRoutes return a group of routes for login
func GetSubMenuRoutes() *gin.Engine{
router := gin.Default()
menuRoutes := router.Group("/submenu")
{
getRoutes := menuRoutes.Group("/get")
{
getRoutes.GET("/getallsubmenus", middlewares.GetAllSubMenus)
}
}
return router
}
And I am running them from main.go like this :
package main
import(
"fmt"
"github.com/IamFrost/api-5/pkg/routes"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"
)
func main(){
fmt.Println("from main")
router := gin.Default()
router = routes.GetSubMenuRoutes()
router.Use(cors.Default())
router.Run(":3000")
}
Up to this, all working.
Now my question is :
lets say I have some other routes in a file menuRoutes.go :
package routes
import(
"github.com/IamFrost/api-5/pkg/middlewares"
"github.com/gin-gonic/gin"
)
// GetMenuRoutes return a group of routes for login
func GetMenuRoutes() *gin.Engine{
router := gin.Default()
menuRoutes := router.Group("/menu")
{
getRoutes := menuRoutes.Group("/get")
{
getRoutes.GET("/getallmenus", middlewares.GetAllMenus)
}
}
return router
}
Now, I want to run both menu routes and submenu routes from main .
But In my main.go , you see, I did this before running,
router = routes.GetSubMenuRoutes()
So How Can I merge all routes coming from routes.GetSubMenuRoutes() and routes.GetMenuRoutes()
before I call router.Run in main.go?
Update:
As their doc said I wanted something like this in main.go, But my routes are coming from function. You see : I cant hardcode my route like v1 or v2 like them, because mine is coming from getMenuRoute() and getSubmenuRoute() function
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")
}
instead of declaring your gin.Default() inside each function you can pass it as a parameter to your router functions.
func GetMenuRoutes(router *gin.Engine){
menuRoutes := router.Group("/menu")
{
getRoutes := menuRoutes.Group("/get")
{
getRoutes.GET("/getallmenus", middlewares.GetAllMenus)
}
}
return
}
or
you can register all these routes on to a main router using
a for loop.
your main func() will have the following lines
func main() {
v1 := getGroup1().Routes()
v2 := getGroup2().Routes()
rt := gin.Default()
for _, a := range v1 {
{
rt.Handle(a.Method, a.Path, a.HandlerFunc)
}
}
for _, a := range v2 {
{
rt.Handle(a.Method, a.Path, a.HandlerFunc)
}
}
rt.Run(":8080")
}
#whitespace 's answer is good. But I found another solution.
So, as you remember, I have a lot of routes coming from 2 functions.
I need to merge them, before main calls all of them
So my first route function from : menuRoutes.go
package routes
import(
"github.com/IamFrost/api-5/pkg/middlewares"
"github.com/gin-gonic/gin"
)
// GetMenuRoutes return a group of routes for menu
func GetMenuRoutes(router *gin.Engine) *gin.Engine{
menuRoutes := router.Group("/menu")
{
getRoutes := menuRoutes.Group("/get")
{
getRoutes.GET("/getallmenus", middlewares.GetAllMenus)
}
}
return router
}
My second route function from: submenuRoutes.go
package routes
import(
"github.com/IamFrost/api-5/pkg/middlewares"
"github.com/gin-gonic/gin"
)
// GetSubMenuRoutes return a group of routes for submenu
func GetSubMenuRoutes(router *gin.Engine) *gin.Engine{
menuRoutes := router.Group("/submenu")
{
getRoutes := menuRoutes.Group("/get")
{
getRoutes.GET("/getallsubmenus", middlewares.GetAllSubMenus)
}
}
// router.GET("/getallsubmenus", middlewares.GetAllSubMenus)
return router
}
Finally, I merged both route groups to : main.go
package main
import(
"fmt"
"github.com/IamFrost/api-5/pkg/routes"
"github.com/gin-gonic/gin"
"github.com/gin-contrib/cors"
)
func main(){
fmt.Println("from main")
router := gin.Default()
// router = routes.GetSubMenuRoutes()
routes.GetMenuRoutes(router)
routes.GetSubMenuRoutes(router)
router.Use(cors.Default())
router.Run(":3000")
}
I am new in Golang and need some help.
As you can see in the code below I am tring to create REST API in Golang. I use mux (Gorilla Mux) and pq (PostgreSQL driver) as third party libraries. Don't want to use ORM.
Inside application.go file I have InitializeRoutes function with a list of all aviable routes. GetFactors function process one of these routes. I am tring to define GetFactors function logic in other file called factors.go. Inside factors.go file I want to use Application struct which was defined in application.go. How to make it correctly? Right now as you can see they are in different packages. For thats why factors.go file don't see Application struct.
Project structure:
main.go
application.go
controllers
factors.go
main.go:
package main
func main() {
application := Application{}
application.Initialization()
application.Run("localhost:8000")
}
application.go:
package main
import (
"database/sql"
"github.com/gorilla/mux"
"log"
"net/http"
"rest-api/configurations"
)
type Application struct {
Router *mux.Router
Database *sql.DB
}
func (application *Application) Initialization() {
var err error
application.Database, err = configurations.DatabaseConnection()
if err != nil {
log.Fatal(err)
}
application.Router = mux.NewRouter()
application.Router.StrictSlash(true)
application.InitializeRoutes()
}
func (application *Application) Run(address string) {
log.Fatal(http.ListenAndServe(address, application.Router))
}
func (application *Application) InitializeRoutes() {
application.Router.HandleFunc("/api/factors", application.GetFactors).Methods("GET")
// other code
}
controllers/factors.go:
package controllers
import (
"net/http"
)
func (application *Application) GetFactors(rw http.ResponseWriter, request *http.Request) {
// code
}
Well, finally I decided to redesign the project structure.
main.go
routes
routes.go
controllers
factors.go
models
factors.go
main.go:
import (
"your_project_name/routes"
)
func main() {
// code
router := mux.NewRouter()
routes.Use(router)
// code
}
routes/routes.go:
package routes
import (
"github.com/gorilla/mux"
"your_application_name/controllers"
)
func Use(router *mux.Router) {
router.HandleFunc("/api/factors", controllers.GetFactors).Methods("GET")
}
controllers/factors.go:
package controllers
var GetFactors = func(res http.ResponseWriter, req *http.Request) {
// code
}
I have two files main.go and group.go... it looks something like this
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
func main() {
// Creates a gin router with default middlewares:
// logger and recovery (crash-free) middlewares
router := gin.Default()
v1 := router.Group("/v1")
{
v1.GET("/", func (c *gin.Context) {
c.JSON(http.StatusOK, "{'sup': 'dup'}")
})
groups := v1.Group("/groups")
{
groups.GET("/", groupIndex)
groups.GET("/:id", groupShow)
groups.POST("/", groupCreate)
groups.PUT("/:id", groupUpdate)
groups.DELETE("/:id", groupDelete)
}
}
// Listen and server on 0.0.0.0:8080
router.Run(":3000")
}
So the methods groupIndex, groupCreate, groupUpdate, etc are located in another file under routes/group.go
package main
import (
"strings"
"github.com/gin-gonic/gin"
)
func groupIndex(c *gin.Context) {
var group struct {
Name string
Description string
}
group.Name = "Famzz"
group.Description = "Jamzzz"
c.JSON(http.StatusOK, group)
}
func groupShow(c *gin.Context) {
c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
}
func groupCreate(c *gin.Context) {
c.JSON(http.StatusOK, "{'groupShow': 'someContent'}")
}
func groupUpdate(c *gin.Context) {
c.JSON(http.StatusOK, "{'groupUpdate': 'someContent'}")
}
func groupDelete(c *gin.Context) {
c.JSON(http.StatusOK, "{'groupDelete': 'someContent'}")
}
But when I try to compile I get the following error
stuff/main.go:21: undefined: groupIndex
stuff/main.go:23: undefined: groupShow
stuff/main.go:24: undefined: groupCreate
stuff/main.go:25: undefined: groupUpdate
stuff/main.go:26: undefined: groupDelete
I'm super new to go, but I thought if you put files in the same package, then they'll have access to each other. What am I doing wrong here?
There are two ways to fix this:
Move group.go to the same directory as main.go.
Import group.go as a package. Change the package declaration on group.go to:
package routes // or the name of your choice
Export the functions by starting them with a capital letter:
func GroupIndex(c *gin.Context) {
Import the package from main:
import "path/to/routes"
...
groups.GET("/", routes.GroupIndex)
The document How To Write Go Code explains this and more.
How do I integrate just like http.FileServer with martini?
` package main
import (
"github.com/go-martini/martini"
"net/http"
)
func main() {
m := martini.Classic()
//http.Handle("/", http.FileServer(http.Dir("."))) //It doesn't work!
m.Run()
}`
I believe the FileServer isn't used directly in Martini: see issues/20:
Unfortunately The fileserver middleware throws a 404 if there is no match, which means we will need to roll our own
Hence PR 26 and commit a945713 in static.go that you can see in static_test.go
m := New()
r := NewRouter()
m.Use(Static(currentRoot))
m.Action(r.Handle)
I have a relatively big web application written in Go that uses Gorilla's mux for routing. I recently realised that my web application is quite slow and I would like to profile the web application.
After reading about it, it seems that net/http/pprof is what I need. But I can't make it run with mux; even in the case of the most trivial web application.
Does anyone knows how to make that work?
Here is an example of a trivial code that does not work (i.e. nothing is served at /debug).
package main
import (
"fmt"
"github.com/gorilla/mux"
"math"
"net/http"
)
import _ "net/http/pprof"
func SayHello(w http.ResponseWriter, r *http.Request) {
for i := 0; i < 1000000; i++ {
math.Pow(36, 89)
}
fmt.Fprint(w, "Hello!")
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/hello", SayHello)
http.ListenAndServe(":6060", r)
}
My preferred method for this is to just let net/http/pprof register itself to http.DefaultServeMux, and then pass all requests starting with /debug/pprof/ along:
package main
import (
"net/http"
_ "net/http/pprof"
"github.com/gorilla/mux"
)
func main() {
router := mux.NewRouter()
router.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
if err := http.ListenAndServe(":6060", router); err != nil {
panic(err)
}
}
I find that this approach is a lot more stable than one that depends on the implementation of a hidden initialization method, and also guarantees that you didn't miss anything.
user983716 - Thanks for your question and solution!
I was not able to use the links from the web index (http://[my-server]/debug/pprof), until I added a few lines to your solution, like so:
...
func AttachProfiler(router *mux.Router) {
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
// Manually add support for paths linked to by index page at /debug/pprof/
router.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
router.Handle("/debug/pprof/heap", pprof.Handler("heap"))
router.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
router.Handle("/debug/pprof/block", pprof.Handler("block"))
}
...
If anyone has the same problem, I hope this helps!
Sorry for that question. The answer is in the init() function of pprof. One just need to add 4 functions from pprof to the mux router. Here is the fixed code from above.
package main
import (
"fmt"
"github.com/gorilla/mux"
"math"
"net/http"
)
import "net/http/pprof"
func AttachProfiler(router *mux.Router) {
router.HandleFunc("/debug/pprof/", pprof.Index)
router.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
router.HandleFunc("/debug/pprof/profile", pprof.Profile)
router.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
}
func SayHello(w http.ResponseWriter, r *http.Request) {
for i := 0; i < 1000000; i++ {
math.Pow(36, 89)
}
fmt.Fprint(w, "Hello!")
}
func main() {
r := mux.NewRouter()
AttachProfiler(r)
r.HandleFunc("/hello", SayHello)
http.ListenAndServe(":6060", r)
}
Previous examples not really work on my side.
To use pprof in an existing golang project with gorrila/mux, try to add :
...previous code
func main() {
r := mux.NewRouter()
r.HandleFunc("/hello", SayHello)
go func() {
log.Fatal(http.ListenAndServe(":6061", http.DefaultServeMux))
}()
http.ListenAndServe(":6060", r)
}
then go to http://localhost:6061/debug/pprof/
I did something else, I added another native http server on a different port and it just works out of the box
package main
import (
"fmt"
"log"
"net/http"
_ "net/http/pprof"
)
func main() {
go func() {
log.Println(http.ListenAndServe(":6060", nil))
}()
log.Fatalln(http.ListenAndServe(":8080", route.Handlers()))
}
Now the pprof endpoint is at :
http://localhost:6060/debug/pprof/ and the applcation is running on port :8080
Just so:
r := mux.NewRouter()
r.PathPrefix("/debug").Handler(http.DefaultServeMux)
Im using https://github.com/julienschmidt/httprouter but i just got this answer from google search.
That's what i did
router := httprouter.New()
router.Handler("GET", "/debug/pprof/profile", http.DefaultServeMux)
router.Handler("GET", "/debug/pprof/heap", http.DefaultServeMux)
I only need this two routes.
This answer is combine of #damien and #user983716 answers.
The following should work:
import (
"net/http"
_ "net/http/pprof"
)
myrouter.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)