Use go embed

This commit is contained in:
nemunaire 2021-10-01 17:52:44 +02:00
parent 1ad302a790
commit dfcde910b2
6 changed files with 81 additions and 181 deletions

View File

@ -8,12 +8,6 @@ platform:
arch: arm
steps:
- name: generate frontend
image: golang:alpine
commands:
- apk --no-cache add go-bindata
- go generate -v
- name: vet
image: golang:alpine
commands:
@ -50,12 +44,6 @@ platform:
arch: arm64
steps:
- name: generate frontend
image: golang:alpine
commands:
- apk --no-cache add go-bindata
- go generate -v
- name: vet
image: golang:alpine
commands:

32
assets-dev.go Normal file
View File

@ -0,0 +1,32 @@
//go:build dev
// +build dev
package main
import (
"flag"
"net/http"
"os"
"path/filepath"
)
var (
Assets http.FileSystem
StaticDir string = "htdocs/"
)
func init() {
flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files")
}
func sanitizeStaticOptions() error {
StaticDir, _ = filepath.Abs(StaticDir)
if _, err := os.Stat(StaticDir); os.IsNotExist(err) {
StaticDir, _ = filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "htdocs"))
if _, err := os.Stat(StaticDir); os.IsNotExist(err) {
return err
}
}
Assets = http.Dir(StaticDir)
return nil
}

28
assets.go Normal file
View File

@ -0,0 +1,28 @@
//go:build !dev
// +build !dev
package main
import (
"embed"
"io/fs"
"log"
"net/http"
)
//go:embed htdocs
var _assets embed.FS
var Assets http.FileSystem
func init() {
sub, err := fs.Sub(_assets, "htdocs")
if err != nil {
log.Fatal("Unable to cd to htdocs/ directory:", err)
}
Assets = http.FS(sub)
}
func sanitizeStaticOptions() error {
return nil
}

2
go.mod
View File

@ -1,6 +1,6 @@
module git.nemunai.re/atsebay.t
go 1.15
go 1.16
require (
github.com/coreos/go-oidc v2.2.1+incompatible

View File

@ -1,71 +0,0 @@
// +build dev
package main
import (
"flag"
"net/http"
"os"
"path"
"path/filepath"
"github.com/julienschmidt/httprouter"
)
var StaticDir string = "htdocs/"
func init() {
Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/grades", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/surveys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/surveys/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/users", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/users/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
http.ServeFile(w, r, path.Join(StaticDir, "index.html"))
})
Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
serveStaticAsset(w, r)
})
flag.StringVar(&StaticDir, "static", StaticDir, "Directory containing static files")
}
func serveStaticAsset(w http.ResponseWriter, r *http.Request) {
http.StripPrefix("/", http.FileServer(http.Dir(StaticDir))).ServeHTTP(w, r)
}
func sanitizeStaticOptions() error {
StaticDir, _ := filepath.Abs(StaticDir)
if _, err := os.Stat(StaticDir); os.IsNotExist(err) {
StaticDir, _ = filepath.Abs(filepath.Join(filepath.Dir(os.Args[0]), "htdocs"))
if _, err := os.Stat(StaticDir); os.IsNotExist(err) {
return err
}
}
return nil
}

117
static.go
View File

@ -1,108 +1,31 @@
// +build !dev
package main
import (
"fmt"
"net/http"
"path"
"github.com/julienschmidt/httprouter"
)
//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/...
//go:generate go fmt bindata.go
func serveOrReverse(forced_url string) func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if forced_url != "" {
r.URL.Path = forced_url
}
http.FileServer(Assets).ServeHTTP(w, r)
}
}
func init() {
Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/grades", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/surveys", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/surveys/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/users", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/users/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset("htdocs/index.html"); err != nil {
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
} else {
w.Write(data)
}
})
Router().GET("/css/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/css")
if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
})
Router().GET("/fonts/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
})
Router().GET("/img/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
})
Router().GET("/js/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/javascript")
if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
})
Router().GET("/views/*_", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
w.Header().Set("Content-Type", "text/html")
if data, err := Asset(path.Join("htdocs", r.URL.Path)); err != nil {
http.NotFound(w, r)
} else {
w.Write(data)
}
})
}
func sanitizeStaticOptions() error {
return nil
Router().GET("/", serveOrReverse(""))
Router().GET("/auth", serveOrReverse("/"))
Router().GET("/grades", serveOrReverse("/"))
Router().GET("/surveys", serveOrReverse("/"))
Router().GET("/surveys/*_", serveOrReverse("/"))
Router().GET("/users", serveOrReverse("/"))
Router().GET("/users/*_", serveOrReverse("/"))
Router().GET("/css/*_", serveOrReverse(""))
Router().GET("/fonts/*_", serveOrReverse(""))
Router().GET("/img/*_", serveOrReverse(""))
Router().GET("/js/*_", serveOrReverse(""))
Router().GET("/views/*_", serveOrReverse(""))
}