Replace go-bindata by go:embed

This commit is contained in:
nemunaire 2021-06-15 11:57:44 +02:00
parent 0d238738a2
commit 0e08ad84b9
4 changed files with 28 additions and 27 deletions

View File

@ -11,9 +11,9 @@ steps:
- name: build
image: golang:alpine
commands:
- apk add --no-cache git go-bindata pkgconf libexif-dev build-base
- go generate -v
- go get -v -d
- apk add --no-cache git pkgconf libexif-dev build-base
- go get -v
- go vet -v
- go build -v -o ohsnap
- name: docker

View File

@ -1,13 +1,12 @@
FROM golang:alpine as gobuild
RUN apk add --no-cache git go-bindata pkgconf libexif-dev build-base
RUN apk add --no-cache git pkgconf libexif-dev build-base
WORKDIR /go/src/git.nemunai.re/ohsnap
ADD . .
RUN go generate -v
RUN go get -v -d
RUN go get -v
RUN go build -v

View File

@ -81,6 +81,11 @@ func main() {
baseURL = &tmp
}
err = sanitizeStaticOptions()
if err != nil {
log.Fatal(err)
}
// Database connection
log.Println("Opening database...")
if err := DBInit(*dsn); err != nil {

View File

@ -3,36 +3,33 @@
package main
import (
"embed"
"io/fs"
"net/http"
"strings"
)
//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" static/...
//go:generate go fmt bindata.go
//go:embed static
var assets embed.FS
func init() {
mux.Handle("/favicon.ico", http.FileServer(http.Dir("./static/")))
mux.HandleFunc("/static/css/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
serveStaticAsset(w, r)
})
mux.HandleFunc("/static/js/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/javascript")
serveStaticAsset(w, r)
})
mux.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {
serveStaticAsset(w, r)
})
mux.HandleFunc("/favicon.ico", serveStaticAsset)
mux.HandleFunc("/static/css/", serveStaticAsset)
mux.HandleFunc("/static/js/", serveStaticAsset)
mux.HandleFunc("/static/", serveStaticAsset)
}
var staticFS http.FileSystem
func serveStaticAsset(w http.ResponseWriter, r *http.Request) {
if data, err := Asset(strings.TrimPrefix(r.URL.Path, "/")); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
http.FileServer(staticFS).ServeHTTP(w, r)
}
func sanitizeStaticOptions() error {
return nil
if sub, err := fs.Sub(assets, "static"); err != nil {
return err
} else {
staticFS = http.FS(sub)
return nil
}
}