Move static template helper func to controllers package
This commit is contained in:
parent
140230d89b
commit
13bf91ba7d
20
controllers/static.go
Normal file
20
controllers/static.go
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
package controllers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.kealoha.me/lks/lenslocked/templates"
|
||||||
|
"git.kealoha.me/lks/lenslocked/views"
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func StaticTemplate(templatePath string) http.HandlerFunc {
|
||||||
|
tpl := views.Must(views.FromFS(templates.FS, templatePath))
|
||||||
|
|
||||||
|
var testWriter strings.Builder
|
||||||
|
err := tpl.ExecuteWriter(&testWriter, nil)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) }
|
||||||
|
}
|
||||||
23
main.go
23
main.go
@ -3,27 +3,12 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
|
ctrlrs "git.kealoha.me/lks/lenslocked/controllers"
|
||||||
"github.com/go-chi/chi/v5"
|
"github.com/go-chi/chi/v5"
|
||||||
"github.com/go-chi/chi/v5/middleware"
|
"github.com/go-chi/chi/v5/middleware"
|
||||||
|
|
||||||
"git.kealoha.me/lks/lenslocked/templates"
|
|
||||||
"git.kealoha.me/lks/lenslocked/views"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func parseStaticTemplate(templatePath string) http.HandlerFunc {
|
|
||||||
tpl := views.Must(views.FromFS(templates.FS, templatePath))
|
|
||||||
|
|
||||||
var testWriter strings.Builder
|
|
||||||
err := tpl.ExecuteWriter(&testWriter, nil)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return func(w http.ResponseWriter, r *http.Request) { tpl.Execute(w, nil) }
|
|
||||||
}
|
|
||||||
|
|
||||||
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
w.Header().Set("Content-Type", "text/html; charset=utf8")
|
||||||
w.WriteHeader(http.StatusNotFound)
|
w.WriteHeader(http.StatusNotFound)
|
||||||
@ -33,9 +18,9 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
func main() {
|
func main() {
|
||||||
r := chi.NewRouter()
|
r := chi.NewRouter()
|
||||||
r.Use(middleware.Logger)
|
r.Use(middleware.Logger)
|
||||||
r.Get("/", parseStaticTemplate("home.gohtml"))
|
r.Get("/", ctrlrs.StaticTemplate("home.gohtml"))
|
||||||
r.Get("/contact", parseStaticTemplate("contact.gohtml"))
|
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml"))
|
||||||
r.Get("/faq", parseStaticTemplate("faq.gohtml"))
|
r.Get("/faq", ctrlrs.StaticTemplate("faq.gohtml"))
|
||||||
r.NotFound(notFoundHandler)
|
r.NotFound(notFoundHandler)
|
||||||
fmt.Println("Starting the server on :3000...")
|
fmt.Println("Starting the server on :3000...")
|
||||||
http.ListenAndServe(":3000", r)
|
http.ListenAndServe(":3000", r)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user