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.
This commit is contained in:
Gabriel Simmer 2021-05-23 17:31:57 +01:00
parent 8127aaa6a8
commit dad608edbc
3 changed files with 13 additions and 52 deletions

2
go.mod
View file

@ -1,6 +1,6 @@
module github.com/gmemstr/nas
go 1.13
go 1.16
require (
github.com/Nerzal/gocloak/v5 v5.1.0

View file

@ -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
}
}

View file

@ -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))
}