StaticFiles - lucasmenendez/shgf GitHub Wiki
Statics are a specific type of request abstraction that expects a raw file as a response. To make the developer's work easier, shgf
allows them to serve whole folder child files or single file as static.
Serving a Folder
To register a route to serve static files from a folder, its necessary to instance a StaticFolder
with the folder path. The route must have any way to get this path. The path could be absolute or relative. Then, use StaticFolder.Serve
to return a response:
...
server.GET("assets/<string:file>", func (ctx *shgf.Context) *shgf.Response {
var err error
var sf *shgf.StaticFolder
if sf, err = shgf.NewStaticFolder("/root/path/of/assets"); err != nil {
return shgf.InternalServerErr(err)
}
var file interface{}
if file, err = ctx.Params.Get("file"); err != nil {
return shgf.InternalServerErr(err)
}
return sf.Serve(file.(string))
})
Serving a File
To register a route to serve a single static file, only its necessary to create a simple handler to return the result of shgf.StaticFile
, with the file path as argument:
...
server.Get("images/icon.png", func(ctx *shgf.Context) *shgf.Response {
return shgf.StaticFile("/path/to/icon.png")
})