First pass at bundling web app into service. Working.

This commit is contained in:
Gabe Kangas
2022-06-19 15:30:32 -07:00
parent 22ac8035fe
commit 78c6189c02
241 changed files with 437 additions and 178 deletions

View File

@@ -4,8 +4,6 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
@@ -36,16 +34,11 @@ type MetadataPage struct {
func IndexHandler(w http.ResponseWriter, r *http.Request) {
middleware.EnableCors(w)
// Treat recordings and schedule as index requests
pathComponents := strings.Split(r.URL.Path, "/")
pathRequest := pathComponents[1]
if pathRequest == "recordings" || pathRequest == "schedule" {
r.URL.Path = "index.html"
}
isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == ""
if isIndexRequest {
serveWeb(w, r)
return
}
// For search engine bots and social scrapers return a special
// server-rendered page.
if utils.IsUserAgentABot(r.UserAgent()) && isIndexRequest {
@@ -65,11 +58,11 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
}
// If this is a directory listing request then return a 404
info, err := os.Stat(path.Join(config.WebRoot, r.URL.Path))
if err != nil || (info.IsDir() && !isIndexRequest) {
w.WriteHeader(http.StatusNotFound)
return
}
// info, err := os.Stat(path.Join(config.WebRoot, r.URL.Path))
// if err != nil || (info.IsDir() && !isIndexRequest) {
// w.WriteHeader(http.StatusNotFound)
// return
// }
// Set a cache control max-age header
middleware.SetCachingHeaders(w, r)
@@ -77,7 +70,9 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) {
// Set our global HTTP headers
middleware.SetHeaders(w)
http.ServeFile(w, r, path.Join(config.WebRoot, r.URL.Path))
serveWeb(w, r)
// http.ServeFile(w, r, path.Join(config.WebRoot, r.URL.Path))
}
// Return a basic HTML page with server-rendered metadata from the config

View File

@@ -1,4 +1,4 @@
package admin
package controllers
import (
"bytes"
@@ -13,26 +13,28 @@ import (
log "github.com/sirupsen/logrus"
)
// ServeAdmin will return admin web assets.
func ServeAdmin(w http.ResponseWriter, r *http.Request) {
// serveWeb will serve web assets.
func serveWeb(w http.ResponseWriter, r *http.Request) {
// If the ETags match then return a StatusNotModified
if responseCode := middleware.ProcessEtags(w, r); responseCode != 0 {
w.WriteHeader(responseCode)
return
}
adminFiles := static.GetAdmin()
path := strings.TrimPrefix(r.URL.Path, "/")
webFiles := static.GetWeb()
path := "web/" + strings.TrimPrefix(r.URL.Path, "/")
// Determine if the requested path is a directory.
// If so, append index.html to the request.
if info, err := fs.Stat(adminFiles, path); err == nil && info.IsDir() {
if info, err := fs.Stat(webFiles, path); err == nil && info.IsDir() {
path = filepath.Join(path, "index.html")
} else if _, err := fs.Stat(adminFiles, path+"index.html"); err == nil {
} else if _, err := fs.Stat(webFiles, path+"index.html"); err == nil {
path = filepath.Join(path, "index.html")
} else if path == "" {
path = filepath.Join(path, "index.html")
}
f, err := adminFiles.Open(path)
f, err := webFiles.Open(path)
if os.IsNotExist(err) {
w.WriteHeader(http.StatusNotFound)
return
@@ -46,7 +48,7 @@ func ServeAdmin(w http.ResponseWriter, r *http.Request) {
// Set a cache control max-age header
middleware.SetCachingHeaders(w, r)
d, err := adminFiles.ReadFile(path)
d, err := webFiles.ReadFile(path)
if err != nil {
log.Errorln(err)
w.WriteHeader(http.StatusInternalServerError)