generated from templates/go-generic
67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"embed"
|
||
|
"flag"
|
||
|
"fmt"
|
||
|
"io/fs"
|
||
|
"log/slog"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
//go:embed flow/output/*
|
||
|
var webFS embed.FS
|
||
|
|
||
|
func main() {
|
||
|
var address string
|
||
|
|
||
|
flag.StringVar(&address, "address", "0.0.0.0:8080", "The address that the web server will listen on")
|
||
|
flag.Parse()
|
||
|
|
||
|
setupLogging()
|
||
|
|
||
|
mux, err := routes()
|
||
|
if err != nil {
|
||
|
slog.Error("Unable to create the Mux", "error", err)
|
||
|
}
|
||
|
|
||
|
server := http.Server{
|
||
|
Addr: address,
|
||
|
Handler: mux,
|
||
|
ReadHeaderTimeout: 1 * time.Second,
|
||
|
ReadTimeout: 5 * time.Second,
|
||
|
}
|
||
|
|
||
|
slog.Info("Starting the Flow website.", "address", address)
|
||
|
|
||
|
if err := server.ListenAndServe(); err != nil {
|
||
|
slog.Error("Failed to run the website", "error", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func routes() (*http.ServeMux, error) {
|
||
|
mux := http.NewServeMux()
|
||
|
|
||
|
rootFS, err := fs.Sub(webFS, "flow/output")
|
||
|
if err != nil {
|
||
|
return nil, fmt.Errorf("unable to create the static root file system; %w", err)
|
||
|
}
|
||
|
|
||
|
fileserver := http.FileServer(http.FS(rootFS))
|
||
|
mux.Handle("/", fileserver)
|
||
|
|
||
|
return mux, nil
|
||
|
}
|
||
|
|
||
|
func setupLogging() {
|
||
|
opts := slog.HandlerOptions{
|
||
|
AddSource: false,
|
||
|
}
|
||
|
|
||
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &opts))
|
||
|
|
||
|
slog.SetDefault(logger)
|
||
|
}
|