This is how my main.go looks like:
package main
import (
"fmt"
"net/http"
"github.com/go-chi/chi/v5"
"github.com/rabocse/lenslocked/controllers"
"github.com/rabocse/lenslocked/templates"
"github.com/rabocse/lenslocked/views"
)
func main() {
r := chi.NewRouter()
r.Get("/", controllers.StaticHandler(views.Must(views.ParseFS(templates.FS,
"home.gohtml",
"tailwind.gohtml"))))
r.Get("/contact", controllers.StaticHandler(views.Must(views.ParseFS(templates.FS,
"contact.gohtml",
"tailwind.gohtml"))))
r.Get("/faq", controllers.FAQ(views.Must(views.ParseFS(templates.FS,
"faq.gohtml",
"tailwind.gohtml"))))
r.NotFound(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Page Not Found", http.StatusNotFound)
})
fmt.Println("Starting the server on :3000...")
http.ListenAndServe(":3000", r)
}
Compared to the previous versions, now I am using a tailwind.gohtml instead of the layout-xxxx.gohtml files.
Here is how the .gohtml files look:
home.gohtml
{{template "header" .}}
<h1>Welcome to my site</hi>
{{template "footer" .}}
contact.gohtml
{{template "header" .}}
<h1> Contact Page </h1>
{{template "footer" .}}
FAQ.gohtml
{{template "header" .}}
<h1> FAQ Page </h1>
<ul>
{{range .}}
{{template "qa" . }}
{{end}}
</ul>
{{template "footer" .}}
{{define "qa"}}
<li><b> {{.Question}}</b> {{.Answer}} </li>
{{end}}
File Organization
Here is how the files are organized at this point:
├── app
├── cmd
│ └── exp
│ ├── hello.gohtml
│ └── main.go
├── controllers
│ └── static.go
├── go.mod
├── go.sum
├── lenslocked
├── main.go
├── modd.conf
├── templates
│ ├── contact.gohtml
│ ├── faq.gohtml
│ ├── fs.go
│ ├── home.gohtml
│ └── tailwind.gohtml
└── views
└── template.go