From 9defacc93235fc24a811cb14cd2d66d557100a64 Mon Sep 17 00:00:00 2001 From: Ross Trottier Date: Tue, 25 Jun 2024 11:35:30 -0600 Subject: [PATCH] scaffolding --- cmd/web/error-handlers.go | 17 +++++++++++++ cmd/web/handlers.go | 7 ++++++ cmd/web/main.go | 38 ++++++++++++++++++++++++++++ cmd/web/renderer.go | 30 +++++++++++++++++++++++ cmd/web/routes.go | 14 +++++++++++ cmd/web/templates.go | 45 ++++++++++++++++++++++++++++++++++ go.mod | 3 +++ ui/html/base.tmpl.html | 23 +++++++++++++++++ ui/html/pages/home.tmpl.html | 5 ++++ ui/html/partials/nav.tmpl.html | 5 ++++ ui/static/css/styles.css | 0 11 files changed, 187 insertions(+) create mode 100644 cmd/web/error-handlers.go create mode 100644 cmd/web/handlers.go create mode 100644 cmd/web/main.go create mode 100644 cmd/web/renderer.go create mode 100644 cmd/web/routes.go create mode 100644 cmd/web/templates.go create mode 100644 go.mod create mode 100644 ui/html/base.tmpl.html create mode 100644 ui/html/pages/home.tmpl.html create mode 100644 ui/html/partials/nav.tmpl.html create mode 100644 ui/static/css/styles.css diff --git a/cmd/web/error-handlers.go b/cmd/web/error-handlers.go new file mode 100644 index 0000000..898e161 --- /dev/null +++ b/cmd/web/error-handlers.go @@ -0,0 +1,17 @@ +package main + +import ( + "net/http" + "runtime/debug" +) + +func (app *application) serverError(writer http.ResponseWriter, request *http.Request, err error) { + var( + method = request.Method + uri = request.URL.RequestURI() + trace = string(debug.Stack()) + ) + + app.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace) + http.Error(writer, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) +} \ No newline at end of file diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go new file mode 100644 index 0000000..507b907 --- /dev/null +++ b/cmd/web/handlers.go @@ -0,0 +1,7 @@ +package main + +import "net/http" + +func (app *application) home(writer http.ResponseWriter, request *http.Request) { + app.render(writer, request, http.StatusOK, "home.tmpl.html", templateData{}) +} \ No newline at end of file diff --git a/cmd/web/main.go b/cmd/web/main.go new file mode 100644 index 0000000..eea8c94 --- /dev/null +++ b/cmd/web/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "html/template" + "log/slog" + "net/http" + "os" + "flag" +) + +type application struct { + logger *slog.Logger + templateCache map[string]*template.Template +} + +func main() { + //Get env variables + addr := flag.String("addr", ":4000", "HTTP network address") + //Set up logger + logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + AddSource: true, + })) + //Set up template cache + templateCache, err := newTemplateCache() + if err != nil { + logger.Error(err.Error()) + os.Exit(1) + } + //Set up application data + app := application { + logger: logger, + templateCache: templateCache, + } + + err = http.ListenAndServe(*addr, app.routes()) + logger.Error(err.Error()) + os.Exit(1) +} \ No newline at end of file diff --git a/cmd/web/renderer.go b/cmd/web/renderer.go new file mode 100644 index 0000000..131adb2 --- /dev/null +++ b/cmd/web/renderer.go @@ -0,0 +1,30 @@ +package main + +import ( + "bytes" + "fmt" + "net/http" +) + +func (app *application) render(writer http.ResponseWriter, request *http.Request, status int, page string, data templateData) { + // Get template + ts, ok := app.templateCache[page] + if !ok { + err := fmt.Errorf("the template %s does not exist", page) + //Should this be a 404? + app.serverError(writer, request, err) + return + } + + // Write buffer + templateBuffer := new(bytes.Buffer) + + err := ts.ExecuteTemplate(templateBuffer, "base", data) + if err != nil { + app.serverError(writer, request, err) + return + } + // If no error, write to the response writer + writer.WriteHeader(status) + templateBuffer.WriteTo(writer) +} \ No newline at end of file diff --git a/cmd/web/routes.go b/cmd/web/routes.go new file mode 100644 index 0000000..318b241 --- /dev/null +++ b/cmd/web/routes.go @@ -0,0 +1,14 @@ +package main + +import "net/http" + +func (app *application) routes() *http.ServeMux { + mux := http.NewServeMux() + //Set up static file server + fileServer := http.FileServer(http.Dir("./ui/static")) + mux.Handle("GET /static/", http.StripPrefix("/static", fileServer)) + //Set up routes + mux.HandleFunc("GET /{$}", app.home) + + return mux +} \ No newline at end of file diff --git a/cmd/web/templates.go b/cmd/web/templates.go new file mode 100644 index 0000000..0a43678 --- /dev/null +++ b/cmd/web/templates.go @@ -0,0 +1,45 @@ +package main + +import ( + "html/template" + "path/filepath" +) + +type templateData struct { + BaseUri string +} + +func newTemplateCache() (map[string]*template.Template, error) { + cache := map[string]*template.Template{} + + pages, err := filepath.Glob("./ui/html/pages/*.tmpl.html") + if err != nil { + return nil, err + } + + for _, page := range pages { + //Get name of page + name := filepath.Base(page) + + //Parse main template file + ts, err := template.ParseFiles("./ui/html/base.tmpl.html") + if err != nil { + return nil, err + } + //Parse partials + ts, err = ts.ParseGlob("./ui/html/partials/*.tmpl.html") + if err != nil { + return nil, err + } + //Parse page + ts, err = ts.ParseFiles(page) + if err != nil { + return nil, err + } + + //set in cache + cache[name] = ts + } + + return cache, nil +} \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8259b4a --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module gitea.federated.computer/ross/federated-dash.git + +go 1.22.4 diff --git a/ui/html/base.tmpl.html b/ui/html/base.tmpl.html new file mode 100644 index 0000000..4242fb9 --- /dev/null +++ b/ui/html/base.tmpl.html @@ -0,0 +1,23 @@ +{{define "base"}} + + + + + + Federated Core Dashboard + + + +
+

Federated Computer

+
+ {{template "nav" .}} +
+ {{template "main" .}} +
+ + + +{{end}} \ No newline at end of file diff --git a/ui/html/pages/home.tmpl.html b/ui/html/pages/home.tmpl.html new file mode 100644 index 0000000..fd6a007 --- /dev/null +++ b/ui/html/pages/home.tmpl.html @@ -0,0 +1,5 @@ +{{define "title"}}Home{{end}} + +{{define "main"}} + yo +{{end}} \ No newline at end of file diff --git a/ui/html/partials/nav.tmpl.html b/ui/html/partials/nav.tmpl.html new file mode 100644 index 0000000..c3fffde --- /dev/null +++ b/ui/html/partials/nav.tmpl.html @@ -0,0 +1,5 @@ +{{define "nav"}} + +{{end}} \ No newline at end of file diff --git a/ui/static/css/styles.css b/ui/static/css/styles.css new file mode 100644 index 0000000..e69de29