Serve Template and Static

This commit is contained in:
Samuel Lorch 2024-05-04 01:31:24 +02:00
parent e51abbcbf7
commit b2761970ce
4 changed files with 85 additions and 8 deletions

View file

@ -2,6 +2,9 @@ package server
import (
"context"
"embed"
"fmt"
"html/template"
"log/slog"
"net/http"
"os"
@ -13,10 +16,25 @@ import (
var conf config.Config
func Start(_conf config.Config) {
var templates *template.Template
func Start(_conf config.Config, tmplFS embed.FS, staticFS embed.FS) {
conf = _conf
// Static Files
staticServer := http.FS(staticFS)
fs := http.FileServer(staticServer)
// Templates
t, err := template.ParseFS(tmplFS, "tmpl/*.tmpl")
if err != nil {
panic(fmt.Errorf("Parsing templates: %w", err))
}
templates = t
mux := http.NewServeMux()
mux.HandleFunc("/worker", handleWorkerWebsocket)
mux.Handle("/static/", fs)
mux.HandleFunc("/", handleIndex)
server := &http.Server{