Skip to content

Commit

Permalink
fix: import static files and templates via embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandrolattao committed Dec 9, 2024
1 parent 4475db7 commit 93941bc
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
6 changes: 3 additions & 3 deletions internal/database/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func NewConnection(logger zerolog.Logger, env *environment.Environment) (*DB, er
}

func connectWithRetries(logger zerolog.Logger, env *environment.Environment, dsn string) (*sqlx.DB, error) {
maxRetries := 10
backoff := 3 * time.Second
maxRetries := 12
backoff := 5

var db *sqlx.DB
var err error
Expand All @@ -61,7 +61,7 @@ func connectWithRetries(logger zerolog.Logger, env *environment.Environment, dsn
}

logger.Error().Err(err).Msgf("Connection failed (attempt %d/%d), retrying in %d seconds...", attempts+1, maxRetries, backoff)
time.Sleep(backoff)
time.Sleep(time.Duration(backoff) * time.Second)
}

return nil, fmt.Errorf("all connection attempts failed: %w", err)
Expand Down
19 changes: 17 additions & 2 deletions internal/server/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package server
import (
"html/template"
"io"
"net/http"

"github.com/alessandrolattao/gosqladmin/web"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog"
)
Expand All @@ -15,14 +17,15 @@ type TemplateRenderer struct {
}

// NewTemplateRenderer initializes a new TemplateRenderer with the given logger.
// It loads HTML templates from the specified directory and adds custom functions.
// It loads HTML templates from the embedded filesystem and adds custom functions.
func NewTemplateRenderer(logger zerolog.Logger) *TemplateRenderer {
funcMap := template.FuncMap{
"add": func(a, b int) int { return a + b },
"subtract": func(a, b int) int { return a - b },
}

templates, err := template.New("").Funcs(funcMap).ParseGlob("web/templates/*.html")
// Parse templates from the embedded filesystem
templates, err := template.New("").Funcs(funcMap).ParseFS(web.TemplateFiles, "templates/*.html")
if err != nil {
logger.Fatal().Err(err).Msg("Error loading templates")
}
Expand All @@ -38,3 +41,15 @@ func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c
}
return err
}

// StaticFileHandler serves static files from the embedded filesystem.
func StaticFileHandler(logger zerolog.Logger) echo.HandlerFunc {
// Serve static files from the embedded filesystem
fileServer := http.FileServer(http.FS(web.StaticFiles))
return func(c echo.Context) error {
requestPath := c.Request().URL.Path
fileServer.ServeHTTP(c.Response(), c.Request())
logger.Info().Str("path", requestPath).Msg("Serving static file")
return nil
}
}
4 changes: 2 additions & 2 deletions internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func registerRoutes(e *echo.Echo, logger zerolog.Logger, db *database.DB, driver
e.POST("/table/:databasename/:tablename", handlers.TableHandler())
e.POST("/query/:databasename", handlers.QueryHandler(logger, db, driverName))

// Serve static files from the "web/static" directory
e.Static("/static", "web/static")
// Serve static files from the embedded filesystem
e.GET("/static/*", StaticFileHandler(logger))
}
9 changes: 9 additions & 0 deletions web/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package web

import "embed"

//go:embed static/*
var StaticFiles embed.FS

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

0 comments on commit 93941bc

Please sign in to comment.