main.go 746 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "html/template"
  4. "log"
  5. "net/http"
  6. )
  7. type server struct {
  8. template *template.Template
  9. }
  10. func main() {
  11. t := template.Must(template.ParseFiles("templates/main.html"))
  12. s := server{template: t}
  13. http.HandleFunc("/", s.handleRoot)
  14. http.Handle("/resources/", http.FileServer(http.Dir(".")))
  15. log.Println("Listening on https://localhost:8443")
  16. err := http.ListenAndServeTLS(":8443", "certs/localhost.cert", "certs/localhost.key", nil)
  17. log.Fatal(err)
  18. }
  19. func (s *server) handleRoot(w http.ResponseWriter, r *http.Request) {
  20. w.Header().Set("Content-Type", "text/html")
  21. if r.RequestURI != "/" {
  22. http.NotFound(w, r)
  23. return
  24. }
  25. err := s.template.Execute(w, "Vrolijk pasen!")
  26. if err != nil {
  27. log.Println(err)
  28. }
  29. }