Embed template files in server binary
This commit is contained in:
parent
5adfeefa33
commit
1d6e8a9811
12
main.go
12
main.go
@ -3,17 +3,17 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"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"
|
"git.kealoha.me/lks/lenslocked/views"
|
||||||
)
|
)
|
||||||
|
|
||||||
func addStaticTemplate(r chi.Router, pattern string, filepath string) {
|
func addStaticTemplate(r chi.Router, pattern string, templatePath string) {
|
||||||
tpl := views.Must(views.FromFile(filepath))
|
tpl := views.Must(views.FromFS(templates.FS, templatePath))
|
||||||
|
|
||||||
var testWriter strings.Builder
|
var testWriter strings.Builder
|
||||||
err := tpl.ExecuteWriter(&testWriter, nil)
|
err := tpl.ExecuteWriter(&testWriter, nil)
|
||||||
@ -33,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)
|
||||||
addStaticTemplate(r, "/", filepath.Join("templates", "home.gohtml"))
|
addStaticTemplate(r, "/", "home.gohtml")
|
||||||
addStaticTemplate(r, "/contact", filepath.Join("templates", "contact.gohtml"))
|
addStaticTemplate(r, "/contact", "contact.gohtml")
|
||||||
addStaticTemplate(r, "/faq", filepath.Join("templates", "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)
|
||||||
|
|||||||
6
templates/fs.go
Normal file
6
templates/fs.go
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package templates
|
||||||
|
|
||||||
|
import "embed"
|
||||||
|
|
||||||
|
//go:embed *
|
||||||
|
var FS embed.FS
|
||||||
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
|
"io/fs"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@ -34,6 +35,15 @@ func FromFile(filepath string) (Template, error) {
|
|||||||
htmlTpl: tpl,
|
htmlTpl: tpl,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
func FromFS(fs fs.FS, pattern string) (Template, error) {
|
||||||
|
tpl, err := template.ParseFS(fs, pattern)
|
||||||
|
if err != nil {
|
||||||
|
return Template{}, fmt.Errorf("Error parsing template: %v", err)
|
||||||
|
}
|
||||||
|
return Template{
|
||||||
|
htmlTpl: tpl,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func Must(t Template, err error) Template {
|
func Must(t Template, err error) Template {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user