Add basic html pages and css.
This commit is contained in:
parent
ffa70e8f55
commit
b8c985db04
4 changed files with 101 additions and 2 deletions
33
main.go
33
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue