Use go embed
This commit is contained in:
parent
1ad302a790
commit
dfcde910b2
12
.drone.yml
12
.drone.yml
@ -8,12 +8,6 @@ platform:
|
|||||||
arch: arm
|
arch: arm
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: generate frontend
|
|
||||||
image: golang:alpine
|
|
||||||
commands:
|
|
||||||
- apk --no-cache add go-bindata
|
|
||||||
- go generate -v
|
|
||||||
|
|
||||||
- name: vet
|
- name: vet
|
||||||
image: golang:alpine
|
image: golang:alpine
|
||||||
commands:
|
commands:
|
||||||
@ -50,12 +44,6 @@ platform:
|
|||||||
arch: arm64
|
arch: arm64
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: generate frontend
|
|
||||||
image: golang:alpine
|
|
||||||
commands:
|
|
||||||
- apk --no-cache add go-bindata
|
|
||||||
- go generate -v
|
|
||||||
|
|
||||||
- name: vet
|
- name: vet
|
||||||
image: golang:alpine
|
image: golang:alpine
|
||||||
commands:
|
commands:
|
||||||
|
32
assets-dev.go
Normal file
32
assets-dev.go
Normal 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
28
assets.go
Normal 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
2
go.mod
@ -1,6 +1,6 @@
|
|||||||
module git.nemunai.re/atsebay.t
|
module git.nemunai.re/atsebay.t
|
||||||
|
|
||||||
go 1.15
|
go 1.16
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/coreos/go-oidc v2.2.1+incompatible
|
github.com/coreos/go-oidc v2.2.1+incompatible
|
||||||
|
@ -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
117
static.go
@ -1,108 +1,31 @@
|
|||||||
// +build !dev
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:generate go-bindata -ignore "\\.go|\\.less" -pkg "main" -o "bindata.go" htdocs/...
|
func serveOrReverse(forced_url string) func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
//go:generate go fmt bindata.go
|
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() {
|
func init() {
|
||||||
Router().GET("/", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
Router().GET("/", serveOrReverse(""))
|
||||||
if data, err := Asset("htdocs/index.html"); err != nil {
|
Router().GET("/auth", serveOrReverse("/"))
|
||||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
Router().GET("/grades", serveOrReverse("/"))
|
||||||
} else {
|
Router().GET("/surveys", serveOrReverse("/"))
|
||||||
w.Write(data)
|
Router().GET("/surveys/*_", serveOrReverse("/"))
|
||||||
}
|
Router().GET("/users", serveOrReverse("/"))
|
||||||
})
|
Router().GET("/users/*_", serveOrReverse("/"))
|
||||||
Router().GET("/auth", func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
Router().GET("/css/*_", serveOrReverse(""))
|
||||||
if data, err := Asset("htdocs/index.html"); err != nil {
|
Router().GET("/fonts/*_", serveOrReverse(""))
|
||||||
fmt.Fprintf(w, "{\"errmsg\":%q}", err)
|
Router().GET("/img/*_", serveOrReverse(""))
|
||||||
} else {
|
Router().GET("/js/*_", serveOrReverse(""))
|
||||||
w.Write(data)
|
Router().GET("/views/*_", serveOrReverse(""))
|
||||||
}
|
|
||||||
})
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user