How to serve static files from all routes except one in Gin? [duplicate] - go

This question already has answers here:
Gin router: path segment conflicts with existing wildcard
(2 answers)
Closed 9 months ago.
As the title says, consider a Gin router where I want to serve static files from all routes except one. Let's say this one route is /api. A first attempt might look like this:
package main
import (
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.StaticFS("/", gin.Dir("static", false))
r.GET("/api/v1/foo", func(c *gin.Context) { c.JSON(200, gin.H{"foo": true}) })
r.Run(":9955")
}
The RouterGroup.StaticFS (and Static too) under the hood joins the relative path with a wildcard path param: path.Join(relativePath, "/*filepath"). When relativePath is the root path /, it will panic with:
panic: '/api/v1/foo' in new path '/api/v1/foo' conflicts with existing wildcard '/*filepath' in existing prefix '/*filepath'
This is due to Gin's http router implementation: routing matches on the path prefix, so a wildcard on root will conflict with every other route. More details about this behavior can be found here — which is where this question was raised.
Another possible solution is to prefix the static file route, so that it doesn't conflict with /api:
r.StaticFS("/static", gin.Dir("static", false))
but this doesn't allow me to serve assets from root too. How can I have a wildcard, or equivalent, on root and still match on one specific path?

There is a package called static for this.
https://github.com/gin-contrib/static#canonical-example
just modify the example as follows
package main
import (
"github.com/gin-contrib/static"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
// server a directory called static
r.Use(static.Serve("/", static.LocalFile("static", false)))
// And any route as usual
r.GET("/api/ping", func(c *gin.Context) {
c.String(200, "ping")
})
r.Run(":9955")
}

Related

Golang fasthttp router custom logger

I'm playing with fasthttp and it's router, I have no issues with basic things, I have a working server and a router, that is the easy part.
The issue is with the logger, I would like to be able to customize that one, but it does not seem possible with the ctx.Logger() as it only takes a Printf argument and the format is not what I'm looking for.
Does anyone knows where in the documentation I can find a working example of what I want to do?
Example of code I currently have:
package server
import (
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
// Router will manage the routes of our API server
func Router() *router.Router {
r := router.New()
r.GET("/", index)
return r
}
func index(ctx *fasthttp.RequestCtx) {
ctx.Logger().Printf("/")
ctx.WriteString("Welcome!")
}
As I'm still trying my hand with the web servers and I still don't understand some things with it and Go also. So An example would be welcome.
For example I would like to be able to do something like that using a logger define in the main package:
package server
import (
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
"go.uber.org/zap"
)
// Router will manage the routes of our API server
func Router(loger *zap.Logger) *router.Router {
r := router.New()
r.GET("/", index)
return r
}
func index(ctx *fasthttp.RequestCtx) {
ctx.Logger().Printf("/") // Here should print in the zap format of my choice.
ctx.WriteString("Welcome!")
}
If you look at the source code, it's apparent that all you have is the ability to write standard Go-formatted strings:
func (cl *ctxLogger) Printf(format string, args ...interface{}) {
msg := fmt.Sprintf(format, args...)
ctxLoggerLock.Lock()
cl.logger.Printf("%.3f %s - %s", time.Since(cl.ctx.ConnTime()).Seconds(), cl.ctx.String(), msg)
ctxLoggerLock.Unlock()
}
The logger simply adds some additional information from the context. So further cutomisation beyond the standard Go formatting does not seem possible. I'm not sure what "zap format of my choice" is, so I can't say if there's a workaround or even if standard Go formatting options will serve for you here.

passing named parameters to index template while using httprouter golang package

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.

Create Routing Modules Go/Echo RestAPI

I just started learning Go and want to create my own REST API.
The problem is simple:
I want to have the routes of my api in a different file for example: routes/users.go that then I include in the "main" function and register those routes.
There are a high number of examples of restAPI's in Echo/Go but all of them have the routes in the main() function.
I checked a few examples/github starter kits but it seems that I cannot find a solution that I like.
func main() {
e := echo.New()
e.GET("/", func(c echo.Context) error {
responseJSON := &JSResp{Msg: "Hello World!"}
return c.JSON(http.StatusOK, responseJSON)
})
//I want to get rid of this
e.GET("users", UserController.CreateUser)
e.POST("users", UserController.UpdateUser)
e.DELETE("users", UserController.DeleteUser)
//would like something like
// UserRoutes.initRoutes(e)
e.Logger.Fatal(e.Start(":1323"))
}
//UserController.go
//CreateUser
func CreateUser(c echo.Context) error {
responseJSON := &JSResp{Msg: "Create User!"}
return c.JSON(http.StatusOK, responseJSON)
}
//UserRoutes.go
func initRoutes(e) { //this is probably e* echo or something like that
//UserController is a package in this case that exports the CreateUser function
e.GET("users", UserController.CreateUser)
return e;
}
Is there an easy way to make this? Coming from node.js and still having some syntax errors of course, will solve them, but I am struggling with the architecture of my code at the moment.
I want to have the routes of my api in a different file for example:
routes/users.go that then I include in the "main" function and
register those routes.
This is possible, simply have your files in the routes package declare functions that take an instance of *echo.Echo and have them register the handlers.
// routes/users.go
func InitUserRoutes(e *echo.Echo) {
e.GET("users", UserController.CreateUser)
e.POST("users", UserController.UpdateUser)
e.DELETE("users", UserController.DeleteUser)
}
// routes/posts.go
func InitPostRoutes(e *echo.Echo) {
e.GET("posts", PostController.CreatePost)
e.POST("posts", PostController.UpdatePost)
e.DELETE("posts", PostController.DeletePost)
}
and then in main.go
import (
"github.com/whatever/echo"
"package/path/to/routes"
)
func main() {
e := echo.New()
routes.InitUserRoutes(e)
routes.InitPostRoutes(e)
// ...
}
Note that the InitXxx functions need to start with an upper case letter as opposed to your initRoutes example which has its first letter in lower case. This is because identifiers with lower case first letters are unexported, which makes them inaccessible from outside their own package. Put another way, for you to be able to reference an imported identifier you have to export it by having it start with an upper case letter.
More here: https://golang.org/ref/spec#Exported_identifiers

Golang best way of calling/invoking sub project

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")
}

How to make templates work with gin framework?

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 :)

Resources