improved loading performances, changed function name in static package

This commit is contained in:
Paul 2020-02-11 19:34:36 +01:00
parent ad68e8ddb5
commit f7081cd518
2 changed files with 20 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"net/http"
_ "git.paulbsd.com/paulbsd/dip/packrd"
"git.paulbsd.com/paulbsd/dip/src/page"
@ -10,13 +11,21 @@ import (
"git.paulbsd.com/paulbsd/dip/utils"
"github.com/gobuffalo/packr/v2"
"github.com/labstack/echo/v4"
//"github.com/markbates/pkger"
"github.com/labstack/echo/v4/middleware"
)
func main() {
e := echo.New()
e.HideBanner = true
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"*"},
AllowMethods: []string{http.MethodGet, http.MethodPut, http.MethodPost, http.MethodDelete},
}))
var host, port string
var p page.Page
p.Title = "Public IP Address Service"
@ -31,8 +40,9 @@ func main() {
e.GET("/", p.Index)
e.GET("/static/*", func(c echo.Context) error {
return static.Static(staticbox, c)
return static.GetStatic(staticbox, c)
})
e.OPTIONS("/", echo.MethodNotAllowedHandler)
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%s", host, port)))
}

View File

@ -7,8 +7,8 @@ import (
"strings"
)
// Static ...
func Static(box *packr.Box, c echo.Context) (err error) {
// GetStatic returns static file content
func GetStatic(box *packr.Box, c echo.Context) (err error) {
name := c.Param("*")
cnt, _ := box.FindString(name)
if strings.HasSuffix(name, ".js") {
@ -18,3 +18,9 @@ func Static(box *packr.Box, c echo.Context) (err error) {
}
return c.String(http.StatusOK, cnt)
}
// Static defines static file
type Static struct {
Name string
Content string
}