add frontend code

This commit is contained in:
vinchent 2024-08-27 21:24:24 +02:00
commit 52a5fc0f3c
7 changed files with 138 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
**/c.out
**/c.html
database.yml
docker/docker-compose.yml
cred.txt
dist/
.air.toml
tmp/

46
front-end/cmd/web/main.go Normal file
View File

@ -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{
"./cmd/web/templates/base.layout.gohtml",
"./cmd/web/templates/header.partial.gohtml",
"./cmd/web/templates/footer.partial.gohtml",
}
var templateSlice []string
templateSlice = append(templateSlice, fmt.Sprintf("./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)
}
}

View File

@ -0,0 +1,22 @@
{{define "base" }}
<!doctype html>
<html lang="en">
{{template "header" .}}
<body>
{{block "content" .}}
{{end}}
{{block "js" .}}
{{end}}
{{template "footer" .}}
</body>
</html>
{{end}}

View File

@ -0,0 +1,10 @@
{{define "footer"}}
<div class="container">
<div class="row">
<div class="col text-center">
<hr>
<small class="text-muted">Copyright &copy; GoCode.ca</small>
</div>
</div>
</div>
{{end}}

View File

@ -0,0 +1,13 @@
{{define "header"}}
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Microservices in Go</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
{{end}}

View File

@ -0,0 +1,36 @@
{{template "base" .}}
{{define "content" }}
<div class="container">
<div class="row">
<div class="col">
<h1 class="mt-5">Test microservices</h1>
<hr>
<div id="output" class="mt-5" style="outline: 1px solid silver; padding: 2em;">
<span class="text-muted">Output shows here...</span>
</div>
</div>
</div>
<div class="row">
<div class="col">
<h4 class="mt-5">Sent</h4>
<div class="mt-1" style="outline: 1px solid silver; padding: 2em;">
<pre id="payload"><span class="text-muted">Nothing sent yet...</span></pre>
</div>
</div>
<div class="col">
<h4 class="mt-5">Received</h4>
<div class="mt-1" style="outline: 1px solid silver; padding: 2em;">
<pre id="received"><span class="text-muted">Nothing received yet...</span></pre>
</div>
</div>
</div>
</div>
{{end}}
{{define "js"}}
<script>
</script>
{{end}}

3
front-end/go.mod Normal file
View File

@ -0,0 +1,3 @@
module frontend
go 1.18