feat(frontend): add frontend files and handling

This commit is contained in:
2026-05-28 12:16:23 +02:00
parent 7347565cda
commit 2dfc206fea
6 changed files with 3490 additions and 4 deletions
+3
View File
@@ -14,6 +14,7 @@ var (
generateTokenFlag bool
deleteTokenFlag bool
datapath string
frontendkey string
)
func PrepareCommandLine() {
@@ -21,6 +22,7 @@ func PrepareCommandLine() {
flag.BoolVar(&generateTokenFlag, "tokengen", false, "Generate token with name")
flag.BoolVar(&deleteTokenFlag, "tokendel", false, "Delete token with name")
flag.StringVar(&datapath, "data", "/var/lib/svg-templater", "Override data directory")
flag.StringVar(&frontendkey, "frontendkey", "", "Specify the api key the frontend should use")
}
func HandleCommandline() {
@@ -45,6 +47,7 @@ func HandleCommandline() {
} else {
svg.Storage = svg.NewFileStorage(datapath, "public", "fonts")
server.PrepareHTTP()
server.PrepareFrontend(frontendkey)
server.Start()
}
}
+41 -4
View File
@@ -1,10 +1,13 @@
package server
import (
"fmt"
"io/fs"
"log"
"net/http"
"os"
"text/template"
svgtemplater "tomatentum.net/svg-templater"
"tomatentum.net/svg-templater/internal/routes"
"tomatentum.net/svg-templater/pkg/auth"
"tomatentum.net/svg-templater/pkg/svg"
@@ -14,9 +17,6 @@ var mux http.ServeMux
func PrepareHTTP() {
mux = *http.NewServeMux()
registerAuthorizedFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "You are authorized!")
})
registerAuthorizedFunc("/svg/", func(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "POST":
@@ -71,6 +71,43 @@ func PrepareHTTP() {
mux.Handle("/public/", http.StripPrefix("/public/", http.FileServer(svg.Storage.GetPublicDir())))
}
func PrepareFrontend(key string) {
webFS, _ := fs.Sub(svgtemplater.Frontend, "frontend")
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/app.js" {
http.FileServer(http.FS(webFS)).ServeHTTP(w, r)
return
}
tmplData, err := fs.ReadFile(webFS, "app.js")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tmpl, err := template.New("index").Delims("[[", "]]").Parse(string(tmplData))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Execute with environment variables
data := map[string]string{
"APIKey": os.Getenv("API_KEY"),
"APIUrl": "",
}
if err := tmpl.Execute(w, data); err != nil {
http.Error(w, "Coudln't render template", http.StatusInternalServerError)
return
}
})
}
func Start() {
log.Println("Starting http server on :3000")
handler := corsMiddleware(&mux)