Browse Source

fix: compilation: switched to packr2

dependabot/npm_and_yarn/web/prismjs-1.21.0
Max Schmitt 7 years ago
parent
commit
7c77642280
  1. 3
      .gitignore
  2. 5
      Makefile
  3. 25
      internal/handlers/handlers.go

3
.gitignore

@ -15,6 +15,7 @@
.glide/ .glide/
debug debug
debug.test debug.test
main
*.db *.db
*.lock *.lock
@ -24,7 +25,7 @@ debug.test
/releases /releases
/docker_releases /docker_releases
data data
*-packr.go
/deployments/cloudfoundry /deployments/cloudfoundry
!/deployments/cloudfoundry/README.md !/deployments/cloudfoundry/README.md
!/deployments/cloudfoundry/config.yaml !/deployments/cloudfoundry/config.yaml

5
Makefile

@ -9,12 +9,11 @@ buildNodeFrontend:
cd web && rm build/static/**/*.map cd web && rm build/static/**/*.map
embedFrontend: embedFrontend:
cd internal/handlers/tmpls && esc -o tmpls.go -pkg tmpls -include ^*\.html . packr2
cd internal/handlers && esc -o static.go -pkg handlers -prefix ../../web/build ../../web/build
getCMDDependencies: getCMDDependencies:
go get -v github.com/mattn/goveralls go get -v github.com/mattn/goveralls
go get -v github.com/mjibson/esc go get -v github.com/gobuffalo/packr/v2/packr2
go get -v github.com/mitchellh/gox go get -v github.com/mitchellh/gox
getGoDependencies: getGoDependencies:

25
internal/handlers/handlers.go

@ -2,6 +2,7 @@
package handlers package handlers
import ( import (
"fmt"
"html/template" "html/template"
"net/http" "net/http"
"time" "time"
@ -9,7 +10,7 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/mxschmitt/golang-url-shortener/internal/handlers/tmpls" "github.com/gobuffalo/packr/v2"
"github.com/mxschmitt/golang-url-shortener/internal/stores" "github.com/mxschmitt/golang-url-shortener/internal/stores"
"github.com/mxschmitt/golang-url-shortener/internal/util" "github.com/mxschmitt/golang-url-shortener/internal/util"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -30,6 +31,8 @@ type loggerEntryWithFields interface {
WithFields(fields logrus.Fields) *logrus.Entry WithFields(fields logrus.Fields) *logrus.Entry
} }
var templateBox = packr.New("Templates", "./tmpls")
// Ginrus returns a gin.HandlerFunc (middleware) that logs requests using logrus. // Ginrus returns a gin.HandlerFunc (middleware) that logs requests using logrus.
// //
// Requests with errors are logged using logrus.Error(). // Requests with errors are logged using logrus.Error().
@ -111,7 +114,7 @@ func New(store stores.Store) (*Handler, error) {
func (h *Handler) addTemplatesFromFS(files []string) error { func (h *Handler) addTemplatesFromFS(files []string) error {
var t *template.Template var t *template.Template
for _, file := range files { for _, file := range files {
fileContent, err := tmpls.FSString(false, "/"+file) fileContent, err := templateBox.FindString("/" + file)
if err != nil { if err != nil {
return errors.Wrap(err, "could not read template file") return errors.Wrap(err, "could not read template file")
} }
@ -165,6 +168,22 @@ func (h *Handler) setHandlers() error {
h.engine.GET("/d/:id/:hash", h.handleDelete) h.engine.GET("/d/:id/:hash", h.handleDelete)
h.engine.GET("/ok", h.handleHealthcheck) h.engine.GET("/ok", h.handleHealthcheck)
assetBox := packr.New("Assets", "../../web/build")
h.engine.GET("/", func(c *gin.Context) {
f, err := assetBox.Open("index.html")
if err != nil {
http.Error(c.Writer, fmt.Sprintf("could not open index.html: %v", err), http.StatusInternalServerError)
return
}
fi, err := f.Stat()
if err != nil {
http.Error(c.Writer, fmt.Sprintf("could not stat index.html: %v", err), http.StatusInternalServerError)
return
}
http.ServeContent(c.Writer, c.Request, fi.Name(), fi.ModTime(), f)
})
// Handling the shorted URLs, if no one exists, it checks // Handling the shorted URLs, if no one exists, it checks
// in the filesystem and sets headers for caching // in the filesystem and sets headers for caching
h.engine.NoRoute( h.engine.NoRoute(
@ -176,7 +195,7 @@ func (h *Handler) setHandlers() error {
}, },
// Pass down to the embedded FS, but let 404s escape via // Pass down to the embedded FS, but let 404s escape via
// the interceptHandler. // the interceptHandler.
gin.WrapH(interceptHandler(http.FileServer(FS(false)), customErrorHandler)), gin.WrapH(interceptHandler(http.FileServer(assetBox), customErrorHandler)),
// not in FS; redirect to root with customURL target filled out // not in FS; redirect to root with customURL target filled out
func(c *gin.Context) { func(c *gin.Context) {
// if we get to this point we should not let the client cache // if we get to this point we should not let the client cache

Loading…
Cancel
Save