From c0c2a5ffcc42f4b0f7abeb352364bf16eb51a3d0 Mon Sep 17 00:00:00 2001 From: Dan Jones Date: Fri, 16 May 2025 14:39:17 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Add=20front-end=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front-end/cmd/web/main.go | 46 +++++++++++++++++++ .../cmd/web/templates/base.layout.gohtml | 22 +++++++++ .../cmd/web/templates/footer.partial.gohtml | 10 ++++ .../cmd/web/templates/header.partial.gohtml | 13 ++++++ front-end/cmd/web/templates/test.page.gohtml | 36 +++++++++++++++ 5 files changed, 127 insertions(+) create mode 100644 front-end/cmd/web/main.go create mode 100644 front-end/cmd/web/templates/base.layout.gohtml create mode 100644 front-end/cmd/web/templates/footer.partial.gohtml create mode 100644 front-end/cmd/web/templates/header.partial.gohtml create mode 100644 front-end/cmd/web/templates/test.page.gohtml diff --git a/front-end/cmd/web/main.go b/front-end/cmd/web/main.go new file mode 100644 index 0000000..c374828 --- /dev/null +++ b/front-end/cmd/web/main.go @@ -0,0 +1,46 @@ +package main + +import ( + "fmt" + "html/template" + "log" + "net/http" +) + +func main() { + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + render(w, "test.page.gohtml") + }) + + fmt.Println("Starting front end service on port 80") + err := http.ListenAndServe(":80", nil) + if err != nil { + log.Panic(err) + } +} + +func render(w http.ResponseWriter, t string) { + + partials := []string{ + "./front-end/cmd/web/templates/base.layout.gohtml", + "./front-end/cmd/web/templates/header.partial.gohtml", + "./front-end/cmd/web/templates/footer.partial.gohtml", + } + + var templateSlice []string + templateSlice = append(templateSlice, fmt.Sprintf("./front-end/cmd/web/templates/%s", t)) + + for _, x := range partials { + templateSlice = append(templateSlice, x) + } + + tmpl, err := template.ParseFiles(templateSlice...) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if err := tmpl.Execute(w, nil); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } +} diff --git a/front-end/cmd/web/templates/base.layout.gohtml b/front-end/cmd/web/templates/base.layout.gohtml new file mode 100644 index 0000000..7396530 --- /dev/null +++ b/front-end/cmd/web/templates/base.layout.gohtml @@ -0,0 +1,22 @@ +{{define "base" }} + + + + {{template "header" .}} + + + + {{block "content" .}} + + {{end}} + + {{block "js" .}} + + {{end}} + + {{template "footer" .}} + + + + +{{end}} \ No newline at end of file diff --git a/front-end/cmd/web/templates/footer.partial.gohtml b/front-end/cmd/web/templates/footer.partial.gohtml new file mode 100644 index 0000000..2d02e2a --- /dev/null +++ b/front-end/cmd/web/templates/footer.partial.gohtml @@ -0,0 +1,10 @@ +{{define "footer"}} +
+
+
+
+ Copyright © GoCode.ca +
+
+
+{{end}} \ No newline at end of file diff --git a/front-end/cmd/web/templates/header.partial.gohtml b/front-end/cmd/web/templates/header.partial.gohtml new file mode 100644 index 0000000..86a5f42 --- /dev/null +++ b/front-end/cmd/web/templates/header.partial.gohtml @@ -0,0 +1,13 @@ +{{define "header"}} + + + + + + Microservices in Go + + + + +{{end}} \ No newline at end of file diff --git a/front-end/cmd/web/templates/test.page.gohtml b/front-end/cmd/web/templates/test.page.gohtml new file mode 100644 index 0000000..8319358 --- /dev/null +++ b/front-end/cmd/web/templates/test.page.gohtml @@ -0,0 +1,36 @@ +{{template "base" .}} + +{{define "content" }} +
+
+
+

Test microservices

+
+ +
+ Output shows here... +
+
+
+
+
+

Sent

+
+
Nothing sent yet...
+
+
+
+

Received

+
+
Nothing received yet...
+
+
+
+
+{{end}} + +{{define "js"}} + +{{end}}