Compare commits
4 Commits
3b42227b81
...
920e7972af
| Author | SHA1 | Date | |
|---|---|---|---|
| 920e7972af | |||
| 13bf91ba7d | |||
| 140230d89b | |||
| 9a59acfd2d |
48
controllers/static.go
Normal file
48
controllers/static.go
Normal file
@ -0,0 +1,48 @@
|
||||
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) }
|
||||
}
|
||||
|
||||
func FAQ(templatePath string) http.HandlerFunc {
|
||||
questions := []struct {
|
||||
Question string
|
||||
Answer string
|
||||
}{
|
||||
{
|
||||
Question: "Is this a real website?",
|
||||
Answer: "No.",
|
||||
},
|
||||
{
|
||||
Question: "I Can Has Cheezburger?",
|
||||
Answer: "No.",
|
||||
},
|
||||
}
|
||||
|
||||
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, questions)
|
||||
}
|
||||
}
|
||||
23
main.go
23
main.go
@ -3,27 +3,12 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
ctrlrs "git.kealoha.me/lks/lenslocked/controllers"
|
||||
"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)
|
||||
@ -33,9 +18,9 @@ func notFoundHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func main() {
|
||||
r := chi.NewRouter()
|
||||
r.Use(middleware.Logger)
|
||||
addStaticTemplate(r, "/", "home.gohtml")
|
||||
addStaticTemplate(r, "/contact", "contact.gohtml")
|
||||
addStaticTemplate(r, "/faq", "faq.gohtml")
|
||||
r.Get("/", ctrlrs.StaticTemplate("home.gohtml"))
|
||||
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml"))
|
||||
r.Get("/faq", ctrlrs.FAQ("faq.gohtml"))
|
||||
r.NotFound(notFoundHandler)
|
||||
fmt.Println("Starting the server on :3000...")
|
||||
http.ListenAndServe(":3000", r)
|
||||
|
||||
@ -1,6 +1,10 @@
|
||||
<h1>FAQ</h1>
|
||||
<hr>
|
||||
<h3>Is this a real website?</h3>
|
||||
<p>No.</p>
|
||||
<h3>I Can Has Cheezburger?</h3>
|
||||
<p>No.</p>
|
||||
{{range .}}
|
||||
{{template "qa" .}}
|
||||
{{end}}
|
||||
|
||||
{{define "qa"}}
|
||||
<h3>{{.Question}}</h3>
|
||||
<p>{{.Answer}}</p>
|
||||
{{end}}
|
||||
|
||||
@ -1 +1,15 @@
|
||||
<h1>Welcome to my awesome site!</h1>
|
||||
{{template "lorem-ipsum"}}
|
||||
{{template "lorem-ipsum"}}
|
||||
{{template "lorem-ipsum"}}
|
||||
|
||||
{{define "lorem-ipsum"}}
|
||||
<p>
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
|
||||
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
|
||||
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
||||
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore
|
||||
eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt
|
||||
in culpa qui officia deserunt mollit anim id est laborum.
|
||||
</p>
|
||||
{{end}}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user