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

@ -3,3 +3,5 @@ package constants
const SHARED_SECRET_HEADER = "morffix-secret"
const NAME_HEADER = "morffix-name"
const UUID_HEADER = "morffix-uuid"
const INDEX_TEMPLATE_NAME = "index.tmpl"

10
main.go
View file

@ -5,12 +5,20 @@ import (
"log/slog"
"os"
"embed"
"git.lastassault.de/speatzle/morffix/config"
"git.lastassault.de/speatzle/morffix/server"
"git.lastassault.de/speatzle/morffix/worker"
"github.com/BurntSushi/toml"
)
//go:embed tmpl
var templates embed.FS
//go:embed static
var static embed.FS
var conf config.Config
func main() {
@ -31,7 +39,7 @@ func main() {
if *isserver {
slog.Info("Starting Server...")
server.Start(conf)
server.Start(conf, templates, static)
} else {
slog.Info("Starting Worker...")
worker.Start(conf)

View file

@ -1,15 +1,34 @@
package server
import (
"fmt"
"bytes"
"log/slog"
"net/http"
"git.lastassault.de/speatzle/morffix/constants"
"git.lastassault.de/speatzle/morffix/types"
)
type IndexData struct {
Counter []int
Workers []IndexWorker
}
type IndexWorker struct {
Worker
Status *types.WorkerStatus
}
var count = 123456789
func handleIndex(w http.ResponseWriter, r *http.Request) {
data := "Connections:\n"
data := IndexData{
Counter: splitInt(count),
Workers: []IndexWorker{},
}
count++
func() {
WorkersMutex.Lock()
defer WorkersMutex.Unlock()
@ -23,13 +42,43 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
return
}
slog.InfoContext(r.Context(), "Got Worker Status", "id", i, "status", status)
data = data + fmt.Sprintf("ID: %v, Name: %v, Address: %v Connected %v\n", i, Workers[i].Name, Workers[i].Address, Workers[i].Connected)
data = data + fmt.Sprintf("### CPU Cores: %v, CPU Usage: %v, Memory Total: %v Memory Usage %v\n", status.CPUCount, status.CPUUsage, status.MemoryTotal, status.MemoryUsage)
data.Workers = append(data.Workers, IndexWorker{
Worker: *Workers[i],
Status: &status,
})
} else {
data = data + fmt.Sprintf("ID: %v, Name: %v, Address: %v Connected %v\n", i, Workers[i].Name, Workers[i].Address, Workers[i].Connected)
data.Workers = append(data.Workers, IndexWorker{
Worker: *Workers[i],
Status: nil,
})
}
}
}()
buf := bytes.Buffer{}
err := templates.ExecuteTemplate(&buf, constants.INDEX_TEMPLATE_NAME, data)
if err != nil {
slog.ErrorContext(r.Context(), "Executing index Template", "err", err)
http.Error(w, "Error Executing Template: "+err.Error(), http.StatusInternalServerError)
return
}
w.Write([]byte("Morffix\n" + data))
_, err = w.Write(buf.Bytes())
if err != nil {
slog.ErrorContext(r.Context(), "Writing http Response", "err", err)
}
}
func splitInt(n int) []int {
slc := []int{}
for n > 0 {
slc = append(slc, n%10)
n /= 10
}
result := []int{}
for i := range slc {
result = append(result, slc[len(slc)-1-i])
}
return result
}

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{