From b8c985db04d32e4f3fc4362e8dafd815ace24611 Mon Sep 17 00:00:00 2001 From: CronyAkatsuki Date: Tue, 31 Dec 2024 19:14:02 +0100 Subject: [PATCH] Add basic html pages and css. --- main.go | 33 +++++++++++++++++++++++++++++++-- static/css/index.css | 29 +++++++++++++++++++++++++++++ templates/index.html | 20 ++++++++++++++++++++ templates/result.html | 21 +++++++++++++++++++++ 4 files changed, 101 insertions(+), 2 deletions(-) create mode 100644 static/css/index.css create mode 100644 templates/index.html create mode 100644 templates/result.html diff --git a/main.go b/main.go index 7db0ad8..a503292 100644 --- a/main.go +++ b/main.go @@ -5,8 +5,10 @@ import ( "encoding/json" "errors" "fmt" + "html/template" "io" "net/http" + "path" ) type Result struct { @@ -15,10 +17,15 @@ type Result struct { } func main() { + // Setup handlers mux := http.NewServeMux() mux.HandleFunc("/", getRoot) mux.HandleFunc("/result", getResult) + // Server static files (css) + mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) + + // Setup the server err := http.ListenAndServe(":3333", mux) if errors.Is(err, http.ErrServerClosed) { fmt.Println("server closed") @@ -66,10 +73,32 @@ func getResult(w http.ResponseWriter, r *http.Request) { fmt.Println("got /result request") fmt.Println(result.Message) - io.WriteString(w, result.Message) + + // Get the html template and serve it + fp := path.Join("templates", "result.html") + tmpl, err := template.ParseFiles(fp) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + if err := tmpl.Execute(w, result); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + } } func getRoot(w http.ResponseWriter, r *http.Request) { fmt.Println("got / request") - io.WriteString(w, "This is my website!") + + // Get the html template and serve it + fp := path.Join("templates", "index.html") + tmpl, err := template.ParseFiles(fp) + 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/static/css/index.css b/static/css/index.css new file mode 100644 index 0000000..3ea223d --- /dev/null +++ b/static/css/index.css @@ -0,0 +1,29 @@ +body{ + overflow: hidden; + position: absolute; + width: 100%; + height: 100%; + display: flex; + justify-content: center; + align-items: center; + background-color: #303446; + color: #c6d0f5; +} + +input[type=text] { + display: block; + margin: 0 auto; + height: 40px; + font-size: 20px; + width: 100%; + border: none; + background-color: #414559; + color: #c6d0f5; +} + +.main { + display: block; + margin: auto; + text-align: center; + width: 50%; +} diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..a6e3c30 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,20 @@ + + + + + + + + Goffy + + + +
+

Provjerite jeli stranica lazna.

+
+ +
+
+ + + diff --git a/templates/result.html b/templates/result.html new file mode 100644 index 0000000..b4b0c80 --- /dev/null +++ b/templates/result.html @@ -0,0 +1,21 @@ + + + + + + + + Goffy + + + +
+

{{ .Message }}

+
+ +
+
+ + +