init
This commit is contained in:
commit
9bc4847a3b
48
spa/handler.go
Normal file
48
spa/handler.go
Normal file
@ -0,0 +1,48 @@
|
||||
package spa
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
type handler struct {
|
||||
staticFS embed.FS
|
||||
indexPath string
|
||||
}
|
||||
|
||||
func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
reqPath := r.URL.Path
|
||||
f, err := h.staticFS.Open(fmt.Sprintf("ng-client/browser%s", reqPath))
|
||||
if f != nil {
|
||||
f.Close()
|
||||
}
|
||||
|
||||
if os.IsNotExist(err) {
|
||||
indexFile, indexErr := h.staticFS.Open(h.indexPath)
|
||||
if indexErr != nil {
|
||||
http.Error(w, "Index file not found", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
defer indexFile.Close()
|
||||
tryToDeliverFile(indexFile, h.indexPath, w, r)
|
||||
return
|
||||
} else if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
tryToDeliverFile(f, h.indexPath, w, r)
|
||||
}
|
||||
|
||||
func HandleFiles(path string, options HandlerOptions) {
|
||||
if options.AdditionalMimeTypeMapping != nil {
|
||||
for key, value := range options.AdditionalMimeTypeMapping {
|
||||
contentTypeMapping[key] = value
|
||||
}
|
||||
}
|
||||
http.Handle(path, handler{
|
||||
staticFS: options.Files,
|
||||
indexPath: options.IndexPath,
|
||||
})
|
||||
}
|
||||
47
spa/helper.go
Normal file
47
spa/helper.go
Normal file
@ -0,0 +1,47 @@
|
||||
package spa
|
||||
|
||||
import (
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func tryToDeliverFile(file fs.File, path string, w http.ResponseWriter, r *http.Request) {
|
||||
fileSeeker, ok := file.(io.ReadSeeker)
|
||||
if !ok {
|
||||
file.Close()
|
||||
http.Error(w, "Index file does not implement ReadSeeker", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
stats, statErr := file.Stat()
|
||||
if statErr != nil {
|
||||
http.Error(w, "Cannot stat index file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
contentType, contentTypeErr := detectContentType(file)
|
||||
if contentTypeErr != nil {
|
||||
contentType = "text/html"
|
||||
}
|
||||
contentType = getContentTypeDetail(contentType, stats)
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
http.ServeContent(w, r, path, stats.ModTime(), fileSeeker)
|
||||
}
|
||||
|
||||
func detectContentType(file fs.File) (string, error) {
|
||||
buffer := []byte{}
|
||||
_, err := file.Read(buffer)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return http.DetectContentType(buffer), nil
|
||||
}
|
||||
|
||||
func getContentTypeDetail(contentType string, info fs.FileInfo) string {
|
||||
for ending, realContentType := range contentTypeMapping {
|
||||
if strings.HasSuffix(info.Name(), ending) {
|
||||
return realContentType
|
||||
}
|
||||
}
|
||||
return contentType
|
||||
}
|
||||
9
spa/mime_type.go
Normal file
9
spa/mime_type.go
Normal file
@ -0,0 +1,9 @@
|
||||
package spa
|
||||
|
||||
var contentTypeMapping = map[string]string{
|
||||
".html": "text/html",
|
||||
".htm": "text/html",
|
||||
".js": "text/javascript",
|
||||
".json": "application/json",
|
||||
".css": "text/css",
|
||||
}
|
||||
9
spa/options.go
Normal file
9
spa/options.go
Normal file
@ -0,0 +1,9 @@
|
||||
package spa
|
||||
|
||||
import "embed"
|
||||
|
||||
type HandlerOptions struct {
|
||||
Files embed.FS
|
||||
IndexPath string
|
||||
AdditionalMimeTypeMapping map[string]string
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user