From dad608edbcaf35bd1240745c8278a0aa3751e8ab Mon Sep 17 00:00:00 2001 From: Gabriel Simmer Date: Sun, 23 May 2021 17:31:57 +0100 Subject: [PATCH] Embed web assets into binary. Theorectically, this means that the entire binary is now self contained, minus the need for the config file. Ripped out the redundant static file directives and root handler while I was at it. --- go.mod | 2 +- router/router.go | 55 +++++------------------------------------------- webserver.go | 8 ++++++- 3 files changed, 13 insertions(+), 52 deletions(-) diff --git a/go.mod b/go.mod index a40e545..7de359e 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gmemstr/nas -go 1.13 +go 1.16 require ( github.com/Nerzal/gocloak/v5 v5.1.0 diff --git a/router/router.go b/router/router.go index 3ad6d88..bbf4958 100644 --- a/router/router.go +++ b/router/router.go @@ -1,12 +1,9 @@ package router import ( - "fmt" - "io" "log" "net/http" - "os" - "strconv" + "io/fs" "github.com/gorilla/mux" ) @@ -37,21 +34,10 @@ func handle(handlers ...handler) http.Handler { } // Init initializes the main router and all routes for the application. -func Init() *mux.Router { +func Init(sc fs.FS) *mux.Router { r := mux.NewRouter() - // "Static" paths - r.PathPrefix("/javascript/").Handler(http.StripPrefix("/javascript/", http.FileServer(http.Dir("assets/web/javascript")))) - r.PathPrefix("/css/").Handler(http.StripPrefix("/css/", http.FileServer(http.Dir("assets/web/css")))) - r.PathPrefix("/icons/").Handler(http.StripPrefix("/icons/", http.FileServer(http.Dir("assets/web/icons")))) - - // Paths that require specific handlers - r.Handle("/", handle( - requiresAuth(), - rootHandler(), - )).Methods("GET") - // File & Provider API r.Handle("/api/providers", handle( requiresAuth(), @@ -61,7 +47,7 @@ func Init() *mux.Router { r.Handle(`/api/files/{provider:[a-zA-Z0-9]+\/*}`, handle( requiresAuth(), handleProvider(), - )).Methods("GET", "POST") + )).Methods("GET", "POST", "DELETE") r.Handle(`/api/files/{provider:[a-zA-Z0-9]+}/{file:.+}`, handle( requiresAuth(), @@ -73,38 +59,7 @@ func Init() *mux.Router { callbackAuth(), )).Methods("GET", "POST") + r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.FS(sc)))) + return r } - -// Handles serving index page. -func rootHandler() handler { - return func(context *requestContext, w http.ResponseWriter, r *http.Request) *httpError { - f, err := os.Open("assets/web/index.html") - if err != nil { - return &httpError{ - Message: fmt.Sprintf("error serving index page from assets/web"), - StatusCode: http.StatusInternalServerError, - } - } - - defer f.Close() - stats, err := f.Stat() - if err != nil { - return &httpError{ - Message: fmt.Sprintf("error serving index page from assets/web"), - StatusCode: http.StatusInternalServerError, - } - } else { - w.Header().Add("Content-Length", strconv.FormatInt(stats.Size(), 10)) - } - - _, err = io.Copy(w, f) - if err != nil { - return &httpError{ - Message: fmt.Sprintf("error serving index page from assets/web"), - StatusCode: http.StatusInternalServerError, - } - } - return nil - } -} diff --git a/webserver.go b/webserver.go index 978bee6..613bf35 100644 --- a/webserver.go +++ b/webserver.go @@ -1,7 +1,9 @@ package main import ( + "embed" "fmt" + "io/fs" "io/ioutil" "log" "net/http" @@ -12,6 +14,9 @@ import ( "github.com/go-yaml/yaml" ) +//go:embed assets/web/* +var sc embed.FS + // Main function that defines routes func main() { // Initialize file providers. @@ -39,7 +44,8 @@ func main() { fmt.Println("Keycloak configured") } - r := router.Init() + fsys, err := fs.Sub(sc, "assets/web") + r := router.Init(fsys) fmt.Println("Your sliproad instance is live on port :3000") log.Fatal(http.ListenAndServe(":3000", r)) }