Skip to content
Snippets Groups Projects
main.go 895 B
Newer Older
gbe's avatar
gbe committed
package main

import (
	"embed"
	"html/template"
	"log"
	"net/http"
)

//go:embed templates/*.tpl
var templateFS embed.FS
var templates = template.Must(template.ParseFS(templateFS, "templates/*.tpl"))

//go:embed static/*
var staticFS embed.FS

func main() {
	http.Handle("/static/", http.FileServer(http.FS(staticFS)))

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		log.Println("handling", r.Method, r.URL, "from", r.RemoteAddr)

		if r.Method == "GET" {
			w.Header().Add("content-type", "text/html")

			err := templates.ExecuteTemplate(w, "index.tpl", nil)
			if err != nil {
				log.Println("can't execute index template:", err)
			}

			return
		}
	})

	const listenAddr = "127.0.0.1:7878"

	log.Printf("here we go, listening on http://%s", listenAddr)

	err := http.ListenAndServe(listenAddr, nil)
	if err != nil {
		log.Println("http handler failed:", err)
	}
}