Create basic layout for web server

This commit is contained in:
Tim Van Baak 2025-01-23 16:18:41 -08:00
parent a84a464901
commit 13c2c64583
5 changed files with 51 additions and 4 deletions

View File

@ -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()
},
}

1
web/html/home.html Normal file
View File

@ -0,0 +1 @@
{{ define "content" }}<h1>Hello {{ . }}</h1>{{ end }}

20
web/html/html.go Normal file
View File

@ -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)
}

8
web/html/layout.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<head>
<title>Intake</title>
</head>
<body>
{{ template "content" . }}
</body>
</html>

19
web/root.go Normal file
View File

@ -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))
}