initial
This commit is contained in:
commit
b51493546b
6 changed files with 111 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
htmx.*
|
5
Justfile
Normal file
5
Justfile
Normal file
|
@ -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
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module main
|
||||
|
||||
go 1.22.6
|
58
http-serve.go
Normal file
58
http-serve.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
25
views/index.html
Normal file
25
views/index.html
Normal file
|
@ -0,0 +1,25 @@
|
|||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<script src="htmx.min.js"></script>
|
||||
<title>{{ .Title }}</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>{{ .Title }}</h1>
|
||||
|
||||
{{ if .Name }}
|
||||
<p>Hello, {{ .Name }}!</p>
|
||||
{{ else }}
|
||||
<p>Hello, World!</p>
|
||||
{{ end }}
|
||||
|
||||
<form method="POST" action="/">
|
||||
<label for="name">Enter your name:</label>
|
||||
<input type="text" id="name" name="name">
|
||||
<input type="submit" value="Submit">
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
19
views/style.css
Normal file
19
views/style.css
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue