idk
This commit is contained in:
parent
fb22f599ad
commit
e62044cb34
3 changed files with 16 additions and 17 deletions
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
_ "embed"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
|
|
@ -25,7 +26,7 @@ var config struct {
|
|||
func loadConfig() {
|
||||
_, err := toml.Decode(defaultConfig, &config)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
_, _ = toml.DecodeFile("config.toml", &config)
|
||||
}
|
||||
|
|
|
|||
19
main.go
19
main.go
|
|
@ -5,7 +5,6 @@ import (
|
|||
"embed"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
|
@ -33,7 +32,7 @@ func main() {
|
|||
// load database
|
||||
db, err := store.LoadStore(config.Database.File)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
r := chi.NewRouter()
|
||||
|
|
@ -44,7 +43,10 @@ func main() {
|
|||
r.Use(middleware.Timeout(config.Server.RequestTimeout))
|
||||
|
||||
r.Get("/", http.RedirectHandler("/orders", http.StatusSeeOther).ServeHTTP)
|
||||
r.Get("/static/", http.FileServerFS(static).ServeHTTP)
|
||||
|
||||
// Serve static files from the "./assets" directory when accessing "/assets/*"
|
||||
fileServer := http.StripPrefix("/static/", http.FileServer(http.FS(static)))
|
||||
r.Handle("/static/*", fileServer)
|
||||
|
||||
r.Route("/orders", func(r chi.Router) {
|
||||
r.Use(middleware.WithValue(dbContextKey, db))
|
||||
|
|
@ -73,7 +75,7 @@ func OrderCtx(next http.Handler) http.Handler {
|
|||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
db := r.Context().Value(dbContextKey).(*gorm.DB)
|
||||
if db == nil {
|
||||
panic(errNoContextValue)
|
||||
log.Fatal(errNoContextValue)
|
||||
}
|
||||
var order *models.Order
|
||||
var err error
|
||||
|
|
@ -103,7 +105,7 @@ func getOrder(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
db = r.Context().Value(dbContextKey).(*gorm.DB)
|
||||
if db == nil {
|
||||
panic(errNoContextValue)
|
||||
log.Fatal(errNoContextValue)
|
||||
}
|
||||
|
||||
err = db.Find(&catalogue).Error
|
||||
|
|
@ -114,8 +116,11 @@ func getOrder(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
order = r.Context().Value(orderContextKey).(*models.Order)
|
||||
if order == nil {
|
||||
panic(errNoContextValue)
|
||||
log.Fatal(errNoContextValue)
|
||||
}
|
||||
|
||||
fmt.Fprint(w, "order: ", order, "\ncatalogue: ", catalogue)
|
||||
templates["order.gohtml"].ExecuteTemplate(w, "layout", struct {
|
||||
Order models.Order
|
||||
Catalogue []models.Product
|
||||
}{*order, catalogue})
|
||||
}
|
||||
|
|
|
|||
11
template.go
11
template.go
|
|
@ -3,8 +3,6 @@ package main
|
|||
import (
|
||||
"embed"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
|
|
@ -17,7 +15,7 @@ var pages embed.FS
|
|||
var templates map[string]*template.Template
|
||||
|
||||
func loadTemplates() {
|
||||
// Read all page template files
|
||||
templates = make(map[string]*template.Template)
|
||||
pageFiles, err := pages.ReadDir("templates/pages")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
|
@ -28,22 +26,17 @@ func loadTemplates() {
|
|||
continue
|
||||
}
|
||||
|
||||
// Extract template name from filename (without extension)
|
||||
templateName := strings.TrimSuffix(pageFile.Name(), filepath.Ext(pageFile.Name()))
|
||||
|
||||
// Create template with layout and specific page
|
||||
tmpl, err := template.ParseFS(layout, "templates/layout/*.gohtml")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Parse the specific page template
|
||||
pagePattern := "templates/pages/" + pageFile.Name()
|
||||
tmpl, err = tmpl.ParseFS(pages, pagePattern)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
templates[templateName] = tmpl
|
||||
templates[pageFile.Name()] = tmpl
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue