From 13c2c645831fdfaa73c155528e83a9f9225d60f9 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Thu, 23 Jan 2025 16:18:41 -0800 Subject: [PATCH] Create basic layout for web server --- cmd/serve.go | 7 +++---- web/html/home.html | 1 + web/html/html.go | 20 ++++++++++++++++++++ web/html/layout.html | 8 ++++++++ web/root.go | 19 +++++++++++++++++++ 5 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 web/html/home.html create mode 100644 web/html/html.go create mode 100644 web/html/layout.html create mode 100644 web/root.go diff --git a/cmd/serve.go b/cmd/serve.go index 812747e..e5cefbd 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -1,18 +1,17 @@ package cmd import ( - "log" - + "github.com/Jaculabilis/intake/web" "github.com/spf13/cobra" ) var serveCmd = &cobra.Command{ Use: "serve", Short: "Serve the web interface", - Long: ` + Long: `Serve the intake web interface. `, Run: func(cmd *cobra.Command, args []string) { - log.Fatal("not implemented") + web.RunServer() }, } diff --git a/web/html/home.html b/web/html/home.html new file mode 100644 index 0000000..881e323 --- /dev/null +++ b/web/html/home.html @@ -0,0 +1 @@ +{{ define "content" }}

Hello {{ . }}

{{ end }} diff --git a/web/html/html.go b/web/html/html.go new file mode 100644 index 0000000..62d1904 --- /dev/null +++ b/web/html/html.go @@ -0,0 +1,20 @@ +package html + +import ( + "embed" + "html/template" + "io" +) + +//go:embed *.html +var templates embed.FS + +func load(file string) *template.Template { + return template.Must(template.New("layout.html").ParseFS(templates, "layout.html", file)) +} + +var home = load("home.html") + +func Home(writer io.Writer, data any) error { + return home.Execute(writer, data) +} diff --git a/web/html/layout.html b/web/html/layout.html new file mode 100644 index 0000000..f827cde --- /dev/null +++ b/web/html/layout.html @@ -0,0 +1,8 @@ + + +Intake + + +{{ template "content" . }} + + diff --git a/web/root.go b/web/root.go new file mode 100644 index 0000000..c5d5ed1 --- /dev/null +++ b/web/root.go @@ -0,0 +1,19 @@ +package web + +import ( + "log" + "net/http" + + "github.com/Jaculabilis/intake/web/html" +) + +func rootHandler(writer http.ResponseWriter, req *http.Request) { + log.Printf("%s %s", req.Method, req.URL.Path) + html.Home(writer, "world") +} + +func RunServer() { + http.HandleFunc("/", rootHandler) + + log.Fatal(http.ListenAndServe("localhost:8081", nil)) +}