Compare commits

..

3 Commits

Author SHA1 Message Date
Fred Boniface bfc14ce717 Update web handling 2023-09-03 21:41:25 +01:00
Fred Boniface 00aa052a61 Tidy up template 2023-09-03 20:59:33 +01:00
Fred Boniface d27625f112 Modularise templates 2023-09-03 20:52:51 +01:00
7 changed files with 47 additions and 19 deletions

BIN
static/favicon.ico Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

BIN
static/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.5 KiB

17
templates/base.html Normal file
View File

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta name="charset" content="utf-8" />
<title>barcodes</title>
<link rel="icon" type="image/png" href="/favicon.png" />
<!-- CSS and JS Inclusions -->
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="author" content="Frederick Boniface" />
<meta name="description" content="Easily create a variety of barcodes" />
</head>
<body>
<h1>{{ template "title" }}</h1>
{{ template "body" . }}
</body>
</html>

View File

@ -1,12 +1,5 @@
<!DOCTYPE html> {{ define "title" }}API Documentation{{ end }}
<html lang="en"> {{ define "body" }}
<head>
<meta charset="UTF-8">
<title>API Documentation</title>
</head>
<body>
<h1>API Requests</h1>
<p>API Requests can be made by making a POST request to /generate</p> <p>API Requests can be made by making a POST request to /generate</p>
<p>The request body should be JSON similar to the below</p> <p>The request body should be JSON similar to the below</p>
@ -37,5 +30,4 @@
<p><code>content</code> should be the value you wish the barcode to display. This should be a string and may not accept all characters - that is dependent on the barcode type.</p> <p><code>content</code> should be the value you wish the barcode to display. This should be a string and may not accept all characters - that is dependent on the barcode type.</p>
<p>The response will be a PNG image</p> <p>The response will be a PNG image</p>
</body> {{ end }}
</html>

View File

@ -1,6 +1,7 @@
package web package web
import ( import (
"fmt"
"html/template" "html/template"
"net/http" "net/http"
@ -22,11 +23,14 @@ func helpTemplate(w http.ResponseWriter, r *http.Request) {
BarcodeOptions: barcodeOptions, BarcodeOptions: barcodeOptions,
} }
tmpl := template.Must(template.New("help.html").ParseFiles("templates/help.html")) tmpl, err := template.ParseFiles("templates/base.html", "templates/help.html")
err := tmpl.Execute(w, data)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) fmt.Println("Error parsing templates: ", err)
return http.Error(w, "Unable to parse templates", 500)
}
err = tmpl.Execute(w, data)
if err != nil {
fmt.Println("Error rendering templates: ", err)
http.Error(w, "Unable to render templates", 500)
} }
} }

14
web/page.route.go Normal file
View File

@ -0,0 +1,14 @@
package web
import (
"fmt"
"net/http"
"strings"
)
func mainHandler(w http.ResponseWriter, r *http.Request) {
fmt.Println("Request URL: ", r.URL.Path)
requestFile := strings.TrimRight(r.URL.Path, "/")
requestFile = strings.TrimRight(requestFile, ".html")
fmt.Println("Template File: ", requestFile)
}

View File

@ -6,9 +6,10 @@ import (
) )
func StartServer() { func StartServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { // What I want to do here is server from /static
fmt.Fprintf(w, "Barcodes") // if the resource exists, else try to render from /templates
}) // if the template exists. Else return 404.
http.HandleFunc("/", mainHandler)
http.HandleFunc("/help", helpTemplate) http.HandleFunc("/help", helpTemplate)