Skip to content

Commit

Permalink
Use go:embed for html templates
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottminns committed Nov 7, 2024
1 parent 6c77aee commit 2933648
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
6 changes: 4 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ type App struct {
db *pgxpool.Pool
rdb *redis.Client
migrations fs.FS
templates fs.FS
}

func New(logger *slog.Logger, migrations fs.FS) *App {
func New(logger *slog.Logger, migrations fs.FS, templates fs.FS) *App {
router := http.NewServeMux()

redisAddr, exists := os.LookupEnv("REDIS_ADDR")
Expand All @@ -41,6 +42,7 @@ func New(logger *slog.Logger, migrations fs.FS) *App {
Addr: redisAddr,
}),
migrations: migrations,
templates: templates,
}

return app
Expand All @@ -54,7 +56,7 @@ func (a *App) Start(ctx context.Context) error {

a.db = db

tmpl := template.Must(template.New("").ParseGlob("./templates/*"))
tmpl := template.Must(template.New("").ParseFS(a.templates, "templates/*"))

a.loadRoutes(tmpl)

Expand Down
5 changes: 4 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ import (
//go:embed migrations/*.sql
var migrations embed.FS

//go:embed templates/*.html
var templates embed.FS

func main() {
godotenv.Load()

logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

a := app.New(logger, migrations)
a := app.New(logger, migrations, templates)

if err := a.Start(ctx); err != nil {
logger.Error("failed to start server", slog.Any("error", err))
Expand Down

0 comments on commit 2933648

Please sign in to comment.