server: replace go-bindata with stdlib embed.FS
This commit is contained in:
parent
ad95ac219d
commit
cb29cf83ac
9
Makefile
9
Makefile
@ -24,18 +24,9 @@ dist: \
|
|||||||
_dist/contented-$(VERSION)-src.zip
|
_dist/contented-$(VERSION)-src.zip
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
if [ -f ./staticResources.go ] ; then rm ./staticResources.go ; fi
|
|
||||||
if [ -d ./build ] ; then rm -r ./build ; fi
|
if [ -d ./build ] ; then rm -r ./build ; fi
|
||||||
if [ -f ./contented ] ; then rm ./contented ; fi
|
if [ -f ./contented ] ; then rm ./contented ; fi
|
||||||
|
|
||||||
#
|
|
||||||
# Generated files
|
|
||||||
#
|
|
||||||
|
|
||||||
staticResources.go: static/ static/*
|
|
||||||
go-bindata -o staticResources.go -prefix static -pkg contented static
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Release artefacts
|
# Release artefacts
|
||||||
#
|
#
|
||||||
|
23
Server.go
23
Server.go
@ -1,8 +1,9 @@
|
|||||||
package contented
|
package contented
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"embed"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
@ -14,6 +15,9 @@ import (
|
|||||||
bolt "go.etcd.io/bbolt"
|
bolt "go.etcd.io/bbolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed static
|
||||||
|
var staticAssets embed.FS
|
||||||
|
|
||||||
var SERVER_HEADER string = `contented/0.0.0-dev`
|
var SERVER_HEADER string = `contented/0.0.0-dev`
|
||||||
|
|
||||||
const DEFAULT_MAX_CONCURRENT_THUMBS = 16
|
const DEFAULT_MAX_CONCURRENT_THUMBS = 16
|
||||||
@ -49,6 +53,7 @@ type Server struct {
|
|||||||
startTime time.Time
|
startTime time.Time
|
||||||
thumbnailSem chan struct{}
|
thumbnailSem chan struct{}
|
||||||
metadataBucket []byte
|
metadataBucket []byte
|
||||||
|
staticDir fs.FS // interface
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(opts *ServerOptions) (*Server, error) {
|
func NewServer(opts *ServerOptions) (*Server, error) {
|
||||||
@ -63,6 +68,8 @@ func NewServer(opts *ServerOptions) (*Server, error) {
|
|||||||
log.Printf("Allowing %d concurrent thumbnails", s.opts.MaxConcurrentThumbs)
|
log.Printf("Allowing %d concurrent thumbnails", s.opts.MaxConcurrentThumbs)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.staticDir, _ = fs.Sub(staticAssets, `static`) // can't fail
|
||||||
|
|
||||||
// "fill" the thumbnailer semaphore
|
// "fill" the thumbnailer semaphore
|
||||||
s.thumbnailSem = make(chan struct{}, s.opts.MaxConcurrentThumbs)
|
s.thumbnailSem = make(chan struct{}, s.opts.MaxConcurrentThumbs)
|
||||||
for i := 0; i < s.opts.MaxConcurrentThumbs; i += 1 {
|
for i := 0; i < s.opts.MaxConcurrentThumbs; i += 1 {
|
||||||
@ -157,11 +164,17 @@ func (this *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
// Blanket allow (headers already set)
|
// Blanket allow (headers already set)
|
||||||
w.WriteHeader(200)
|
w.WriteHeader(200)
|
||||||
|
|
||||||
} else if r.Method == "GET" && r.URL.Path == `/` && this.opts.EnableHomepage {
|
} else if r.Method == "GET" {
|
||||||
http.Redirect(w, r, `/index.html`, http.StatusFound)
|
|
||||||
|
|
||||||
} else if static, err := Asset(r.URL.Path[1:]); err == nil && r.Method == "GET" && (this.opts.EnableHomepage || r.URL.Path != `/index.html`) {
|
// Conditionally block homepage access
|
||||||
http.ServeContent(w, r, r.URL.Path[1:], this.startTime, bytes.NewReader(static))
|
if !this.opts.EnableHomepage && (r.URL.Path == `/index.html` || r.URL.Path == `/`) {
|
||||||
|
http.Error(w, "Not found", 404)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve static html/css/js assets
|
||||||
|
// http.FileServer transparently redirects index.html->/ internally
|
||||||
|
http.FileServer(http.FS(this.staticDir)).ServeHTTP(w, r)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "Not found", 404)
|
http.Error(w, "Not found", 404)
|
||||||
|
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue
Block a user