From b51493546b92f6d972078714960f5ab75ce09c5b Mon Sep 17 00:00:00 2001 From: Imbus <> Date: Tue, 10 Sep 2024 02:07:53 +0200 Subject: [PATCH] initial --- .gitignore | 1 + Justfile | 5 +++++ go.mod | 3 +++ http-serve.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++ views/index.html | 25 +++++++++++++++++++++ views/style.css | 19 ++++++++++++++++ 6 files changed, 111 insertions(+) create mode 100644 .gitignore create mode 100644 Justfile create mode 100644 go.mod create mode 100644 http-serve.go create mode 100644 views/index.html create mode 100644 views/style.css diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3c5e06 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +htmx.* diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..12cadfb --- /dev/null +++ b/Justfile @@ -0,0 +1,5 @@ +get-htmx: + curl -O -lSSL https://unpkg.com/htmx.org@2.0.2/dist/htmx.min.js + +run: + go run ./http-serve.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c0459b2 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module main + +go 1.22.6 diff --git a/http-serve.go b/http-serve.go new file mode 100644 index 0000000..0683088 --- /dev/null +++ b/http-serve.go @@ -0,0 +1,58 @@ +package main + +import ( + "html/template" + "net/http" + _ "embed" +) + +// Struct to hold data to be passed to the template +type PageData struct { + Title string + Name string +} + +//go:embed views/index.html +var tmpl string + +//go:embed views/style.css +var style string + +//go:embed htmx.min.js +var htmx string + +func main() { + // Parse the template from the string literal + t := template.Must(template.New("webpage").Parse(tmpl)) + + // Handler for the root URL + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + data := PageData{ + Title: "Hello Page", + } + + // Check if a name is provided via the form + if r.Method == http.MethodPost { + data.Name = r.FormValue("name") + } + + // Execute the template, passing in the data + t.Execute(w, data) + }) + + http.HandleFunc("/style.css", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/css") + w.Write([]byte(style)) + }) + + http.HandleFunc("/htmx.min.js", func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/javascript") + w.Write([]byte(htmx)) + }) + + // Start the server on port 8080 + println("Listening on http://localhost:8080") + if err := http.ListenAndServe(":8080", nil); err != nil { + panic(err) + } +} diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..0ddd73e --- /dev/null +++ b/views/index.html @@ -0,0 +1,25 @@ + + +
+ + + + +Hello, {{ .Name }}!
+ {{ else }} +Hello, World!
+ {{ end }} + + + + diff --git a/views/style.css b/views/style.css new file mode 100644 index 0000000..72c83f3 --- /dev/null +++ b/views/style.css @@ -0,0 +1,19 @@ +html { + font-family: Arial, sans-serif; + background-color: #f4f4f4; + color: #333; +} + +body { + width: 80%; + margin: 0 auto; + padding: 20px; + background: #fff; + border-radius: 8px; + box-shadow: 0 0 10px rgba(0, 0, 0, 0.1); +} + +h1 { + font-size: 80px; + color: blue !important; +}