main.go 707 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "text/template"
  6. )
  7. type server struct {
  8. template *template.Template
  9. }
  10. func main() {
  11. t := template.Must(template.ParseFiles("templates/index.html"))
  12. s := server{template: t}
  13. http.HandleFunc("/", s.handleRoot)
  14. http.Handle("/resources/", http.FileServer(http.Dir(".")))
  15. log.Println("Listening on http://localhost:8080")
  16. err := http.ListenAndServe(":8080", 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; charset=utf8")
  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. }