Compare commits
No commits in common. "920e7972afbe8994b11807be9d9787713bd8ed80" and "3b42227b81b7f609875769ffa0fbd8505b9ecba6" have entirely different histories.
920e7972af
...
3b42227b81
@ -1,48 +0,0 @@
|
|||||||
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,12 +3,27 @@ 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 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) {
|
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)
|
||||||
@ -18,9 +33,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("/", ctrlrs.StaticTemplate("home.gohtml"))
|
addStaticTemplate(r, "/", "home.gohtml")
|
||||||
r.Get("/contact", ctrlrs.StaticTemplate("contact.gohtml"))
|
addStaticTemplate(r, "/contact", "contact.gohtml")
|
||||||
r.Get("/faq", ctrlrs.FAQ("faq.gohtml"))
|
addStaticTemplate(r, "/faq", "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)
|
||||||
|
|||||||
@ -1,10 +1,6 @@
|
|||||||
<h1>FAQ</h1>
|
<h1>FAQ</h1>
|
||||||
<hr>
|
<hr>
|
||||||
{{range .}}
|
<h3>Is this a real website?</h3>
|
||||||
{{template "qa" .}}
|
<p>No.</p>
|
||||||
{{end}}
|
<h3>I Can Has Cheezburger?</h3>
|
||||||
|
<p>No.</p>
|
||||||
{{define "qa"}}
|
|
||||||
<h3>{{.Question}}</h3>
|
|
||||||
<p>{{.Answer}}</p>
|
|
||||||
{{end}}
|
|
||||||
|
|||||||
@ -1,15 +1 @@
|
|||||||
<h1>Welcome to my awesome site!</h1>
|
<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