|
|
@ -3,14 +3,39 @@ package main
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"html/template"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//"io"
|
|
|
|
"log/slog"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
|
|
//"os"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
files := []string{
|
|
|
|
|
|
|
|
"./ui/html/base.go.tmpl",
|
|
|
|
|
|
|
|
"./ui/html/index.go.tmpl",
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ts, err := template.ParseFiles(files...)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
slog.Error("error parsing files", "error", err)
|
|
|
|
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = ts.ExecuteTemplate(w, "base", nil)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
slog.Error("error executing template", "error", err)
|
|
|
|
|
|
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func SSEHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
func SSEHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
|
|
|
|
|
|
@ -23,26 +48,31 @@ func SSEHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func routes() http.Handler {
|
|
|
|
func routes() http.Handler {
|
|
|
|
// TODO add static server and an HTML index with `new EventSource` JS that writes the data message contents into an element on the page
|
|
|
|
fileServer := http.FileServer(http.Dir("./ui/static/"))
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/sse", SSEHandler)
|
|
|
|
mux.Handle("GET /static/", http.StripPrefix("/static", fileServer))
|
|
|
|
|
|
|
|
mux.HandleFunc("GET /sse", SSEHandler)
|
|
|
|
|
|
|
|
mux.HandleFunc("GET /{$}", IndexHandler)
|
|
|
|
return mux
|
|
|
|
return mux
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func run(ctx context.Context, w io.Writer, args []string) error {
|
|
|
|
// TODO add args slice and process flags
|
|
|
|
|
|
|
|
// TODO add io.Writer and pass that to the logger
|
|
|
|
|
|
|
|
func run(ctx context.Context) error {
|
|
|
|
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", "0.0.0.0", 8888)
|
|
|
|
addr := fmt.Sprintf("%s:%d", "0.0.0.0", 8888)
|
|
|
|
srv := &http.Server{
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: addr,
|
|
|
|
Addr: addr,
|
|
|
|
Handler: routes(),
|
|
|
|
Handler: routes(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
slog.Info("Listing on: http://0.0.0.0:8888")
|
|
|
|
slog.Info("Listing on: http://0.0.0.0:8888/")
|
|
|
|
return srv.ListenAndServe()
|
|
|
|
return srv.ListenAndServe()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
func main() {
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx := context.Background()
|
|
|
|
if err := run(ctx, os.Stdout, os.Args); err != nil {
|
|
|
|
// TODO add os.Args
|
|
|
|
|
|
|
|
if err := run(ctx); err != nil {
|
|
|
|
slog.Error("an error occurred", "error", err)
|
|
|
|
slog.Error("an error occurred", "error", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|