scaffolding
This commit is contained in:
commit
9defacc932
17
cmd/web/error-handlers.go
Normal file
17
cmd/web/error-handlers.go
Normal file
@ -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)
|
||||||
|
}
|
7
cmd/web/handlers.go
Normal file
7
cmd/web/handlers.go
Normal file
@ -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{})
|
||||||
|
}
|
38
cmd/web/main.go
Normal file
38
cmd/web/main.go
Normal file
@ -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)
|
||||||
|
}
|
30
cmd/web/renderer.go
Normal file
30
cmd/web/renderer.go
Normal file
@ -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)
|
||||||
|
}
|
14
cmd/web/routes.go
Normal file
14
cmd/web/routes.go
Normal file
@ -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
|
||||||
|
}
|
45
cmd/web/templates.go
Normal file
45
cmd/web/templates.go
Normal file
@ -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
|
||||||
|
}
|
3
go.mod
Normal file
3
go.mod
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module gitea.federated.computer/ross/federated-dash.git
|
||||||
|
|
||||||
|
go 1.22.4
|
23
ui/html/base.tmpl.html
Normal file
23
ui/html/base.tmpl.html
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
{{define "base"}}
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Federated Core Dashboard</title>
|
||||||
|
<link rel="stylesheet" href="/static/css/styles.css" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<header>
|
||||||
|
<h1>Federated Computer</h1>
|
||||||
|
</header>
|
||||||
|
{{template "nav" .}}
|
||||||
|
<main>
|
||||||
|
{{template "main" .}}
|
||||||
|
</main>
|
||||||
|
<footer>
|
||||||
|
Powered by Federated Computer
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
{{end}}
|
5
ui/html/pages/home.tmpl.html
Normal file
5
ui/html/pages/home.tmpl.html
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{{define "title"}}Home{{end}}
|
||||||
|
|
||||||
|
{{define "main"}}
|
||||||
|
yo
|
||||||
|
{{end}}
|
5
ui/html/partials/nav.tmpl.html
Normal file
5
ui/html/partials/nav.tmpl.html
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
{{define "nav"}}
|
||||||
|
<nav>
|
||||||
|
<a href="/">Home</a>
|
||||||
|
</nav>
|
||||||
|
{{end}}
|
0
ui/static/css/styles.css
Normal file
0
ui/static/css/styles.css
Normal file
Loading…
Reference in New Issue
Block a user