Serve Template and Static
This commit is contained in:
parent
e51abbcbf7
commit
b2761970ce
4 changed files with 85 additions and 8 deletions
|
@ -3,3 +3,5 @@ package constants
|
||||||
const SHARED_SECRET_HEADER = "morffix-secret"
|
const SHARED_SECRET_HEADER = "morffix-secret"
|
||||||
const NAME_HEADER = "morffix-name"
|
const NAME_HEADER = "morffix-name"
|
||||||
const UUID_HEADER = "morffix-uuid"
|
const UUID_HEADER = "morffix-uuid"
|
||||||
|
|
||||||
|
const INDEX_TEMPLATE_NAME = "index.tmpl"
|
||||||
|
|
10
main.go
10
main.go
|
@ -5,12 +5,20 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"embed"
|
||||||
|
|
||||||
"git.lastassault.de/speatzle/morffix/config"
|
"git.lastassault.de/speatzle/morffix/config"
|
||||||
"git.lastassault.de/speatzle/morffix/server"
|
"git.lastassault.de/speatzle/morffix/server"
|
||||||
"git.lastassault.de/speatzle/morffix/worker"
|
"git.lastassault.de/speatzle/morffix/worker"
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
//go:embed tmpl
|
||||||
|
var templates embed.FS
|
||||||
|
|
||||||
|
//go:embed static
|
||||||
|
var static embed.FS
|
||||||
|
|
||||||
var conf config.Config
|
var conf config.Config
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -31,7 +39,7 @@ func main() {
|
||||||
|
|
||||||
if *isserver {
|
if *isserver {
|
||||||
slog.Info("Starting Server...")
|
slog.Info("Starting Server...")
|
||||||
server.Start(conf)
|
server.Start(conf, templates, static)
|
||||||
} else {
|
} else {
|
||||||
slog.Info("Starting Worker...")
|
slog.Info("Starting Worker...")
|
||||||
worker.Start(conf)
|
worker.Start(conf)
|
||||||
|
|
|
@ -1,15 +1,34 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"bytes"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"git.lastassault.de/speatzle/morffix/constants"
|
||||||
"git.lastassault.de/speatzle/morffix/types"
|
"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) {
|
func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
data := "Connections:\n"
|
|
||||||
|
data := IndexData{
|
||||||
|
Counter: splitInt(count),
|
||||||
|
Workers: []IndexWorker{},
|
||||||
|
}
|
||||||
|
count++
|
||||||
func() {
|
func() {
|
||||||
WorkersMutex.Lock()
|
WorkersMutex.Lock()
|
||||||
defer WorkersMutex.Unlock()
|
defer WorkersMutex.Unlock()
|
||||||
|
@ -23,13 +42,43 @@ func handleIndex(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
slog.InfoContext(r.Context(), "Got Worker Status", "id", i, "status", status)
|
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.Workers = append(data.Workers, IndexWorker{
|
||||||
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)
|
Worker: *Workers[i],
|
||||||
|
Status: &status,
|
||||||
|
})
|
||||||
} else {
|
} 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
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,6 +2,9 @@ package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"embed"
|
||||||
|
"fmt"
|
||||||
|
"html/template"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,10 +16,25 @@ import (
|
||||||
|
|
||||||
var conf config.Config
|
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
|
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 := http.NewServeMux()
|
||||||
mux.HandleFunc("/worker", handleWorkerWebsocket)
|
mux.HandleFunc("/worker", handleWorkerWebsocket)
|
||||||
|
mux.Handle("/static/", fs)
|
||||||
mux.HandleFunc("/", handleIndex)
|
mux.HandleFunc("/", handleIndex)
|
||||||
|
|
||||||
server := &http.Server{
|
server := &http.Server{
|
||||||
|
|
Loading…
Add table
Reference in a new issue