43 lines
1.0 KiB
Go
43 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/go-chi/chi/v5/middleware"
|
|
|
|
"git.kealoha.me/lks/lenslocked/templates"
|
|
"git.kealoha.me/lks/lenslocked/views"
|
|
)
|
|
|
|
func addStaticTemplate(r chi.Router, pattern string, templatePath string) {
|
|
tpl := views.Must(views.FromFS(templates.FS, templatePath))
|
|
|
|
var testWriter strings.Builder
|
|
err := tpl.ExecuteWriter(&testWriter, nil)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
r.Get(pattern, func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) })
|
|
}
|
|
|
|
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
|
w.WriteHeader(http.StatusNotFound)
|
|
fmt.Fprint(w, "404 page not found")
|
|
}
|
|
|
|
func main() {
|
|
r := chi.NewRouter()
|
|
r.Use(middleware.Logger)
|
|
addStaticTemplate(r, "/", "home.gohtml")
|
|
addStaticTemplate(r, "/contact", "contact.gohtml")
|
|
addStaticTemplate(r, "/faq", "faq.gohtml")
|
|
r.NotFound(notFoundHandler)
|
|
fmt.Println("Starting the server on :3000...")
|
|
http.ListenAndServe(":3000", r)
|
|
}
|