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

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