Merge pull request #14 from gmemstr/embed-assets

This commit is contained in:
Gabriel Simmer 2021-05-23 17:35:52 +01:00 committed by GitHub
commit db7bc98ba2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 57 deletions

View file

@ -4,12 +4,12 @@ orbs:
jobs: jobs:
build: build:
docker: docker:
- image: cimg/go:1.14 - image: cimg/go:1.16
steps: steps:
- checkout - checkout
- restore_cache: - restore_cache:
keys: keys:
- go-mod-{{ checksum "go.sum" }}-v2 - go-mod-{{ checksum "go.sum" }}-v3
- go-mod-{{ checksum "go.sum" }} - go-mod-{{ checksum "go.sum" }}
- go-mod - go-mod
- upx/install - upx/install
@ -18,17 +18,17 @@ jobs:
- store_artifacts: - store_artifacts:
path: build path: build
- save_cache: - save_cache:
key: go-mod-{{ checksum "go.sum" }}-v2 key: go-mod-{{ checksum "go.sum" }}-v3
paths: paths:
- /home/circleci/go/pkg/mod - /home/circleci/go/pkg/mod
test: test:
docker: docker:
- image: cimg/go:1.14 - image: cimg/go:1.16
steps: steps:
- checkout - checkout
- restore_cache: - restore_cache:
keys: keys:
- go-mod-{{ checksum "go.sum" }}-v2 - go-mod-{{ checksum "go.sum" }}-v3
- go-mod-{{ checksum "go.sum" }} - go-mod-{{ checksum "go.sum" }}
- go-mod - go-mod
- run: - run:

2
go.mod
View file

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

View file

@ -1,12 +1,9 @@
package router package router
import ( import (
"fmt"
"io"
"log" "log"
"net/http" "net/http"
"os" "io/fs"
"strconv"
"github.com/gorilla/mux" "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. // 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() 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 // File & Provider API
r.Handle("/api/providers", handle( r.Handle("/api/providers", handle(
requiresAuth(), requiresAuth(),
@ -61,7 +47,7 @@ func Init() *mux.Router {
r.Handle(`/api/files/{provider:[a-zA-Z0-9]+\/*}`, handle( r.Handle(`/api/files/{provider:[a-zA-Z0-9]+\/*}`, handle(
requiresAuth(), requiresAuth(),
handleProvider(), handleProvider(),
)).Methods("GET", "POST") )).Methods("GET", "POST", "DELETE")
r.Handle(`/api/files/{provider:[a-zA-Z0-9]+}/{file:.+}`, handle( r.Handle(`/api/files/{provider:[a-zA-Z0-9]+}/{file:.+}`, handle(
requiresAuth(), requiresAuth(),
@ -73,38 +59,7 @@ func Init() *mux.Router {
callbackAuth(), callbackAuth(),
)).Methods("GET", "POST") )).Methods("GET", "POST")
r.PathPrefix("/").Handler(http.StripPrefix("/", http.FileServer(http.FS(sc))))
return r 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 package main
import ( import (
"embed"
"fmt" "fmt"
"io/fs"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "net/http"
@ -12,6 +14,9 @@ import (
"github.com/go-yaml/yaml" "github.com/go-yaml/yaml"
) )
//go:embed assets/web/*
var sc embed.FS
// Main function that defines routes // Main function that defines routes
func main() { func main() {
// Initialize file providers. // Initialize file providers.
@ -39,7 +44,8 @@ func main() {
fmt.Println("Keycloak configured") 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") fmt.Println("Your sliproad instance is live on port :3000")
log.Fatal(http.ListenAndServe(":3000", r)) log.Fatal(http.ListenAndServe(":3000", r))
} }