diff --git a/Dockerfile b/Dockerfile index 969cf1a..7cf7c5c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -51,7 +51,6 @@ COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ # Set working directory and copy application files WORKDIR /app COPY --from=builder /app/ackify-ce /app/ackify-ce -COPY --from=builder /app/web /app/web # Use non-root user (already set in distroless image) # USER 65532:65532 diff --git a/cmd/migrate/main.go b/cmd/migrate/main.go index 7c13a8b..3b9445c 100644 --- a/cmd/migrate/main.go +++ b/cmd/migrate/main.go @@ -6,11 +6,11 @@ import ( "log" "os" + "database/sql" "github.com/golang-migrate/migrate/v4" "github.com/golang-migrate/migrate/v4/database/postgres" _ "github.com/golang-migrate/migrate/v4/source/file" _ "github.com/lib/pq" - "database/sql" ) func main() { @@ -101,4 +101,4 @@ func printUsage() { fmt.Println(" go run cmd/migrate/main.go up") fmt.Println(" go run cmd/migrate/main.go down 2") fmt.Println(" go run cmd/migrate/main.go version") -} \ No newline at end of file +} diff --git a/internal/infrastructure/database/connection.go b/internal/infrastructure/database/connection.go index a20e154..51729e8 100644 --- a/internal/infrastructure/database/connection.go +++ b/internal/infrastructure/database/connection.go @@ -31,4 +31,3 @@ func InitDB(ctx context.Context, config Config) (*sql.DB, error) { return db, nil } - diff --git a/internal/presentation/templates/templates.go b/internal/presentation/templates/templates.go deleted file mode 100644 index 5609925..0000000 --- a/internal/presentation/templates/templates.go +++ /dev/null @@ -1,30 +0,0 @@ -package templates - -import ( - "fmt" - "html/template" - "path/filepath" -) - -// InitTemplates initializes the HTML templates from files -func InitTemplates() (*template.Template, error) { - // Get the templates directory path relative to the binary - templatesDir := "web/templates" - - // Parse the base template first - tmpl, err := template.New("base").ParseFiles(filepath.Join(templatesDir, "base.html.tpl")) - if err != nil { - return nil, fmt.Errorf("failed to parse base template: %w", err) - } - - // Parse the additional templates - additionalTemplates := []string{"index.html.tpl", "sign.html.tpl", "signatures.html.tpl", "embed.html.tpl"} - for _, templateFile := range additionalTemplates { - _, err = tmpl.ParseFiles(filepath.Join(templatesDir, templateFile)) - if err != nil { - return nil, fmt.Errorf("failed to parse template %s: %w", templateFile, err) - } - } - - return tmpl, nil -} diff --git a/pkg/web/server.go b/pkg/web/server.go index f768888..483e4db 100644 --- a/pkg/web/server.go +++ b/pkg/web/server.go @@ -14,8 +14,8 @@ import ( "github.com/btouchard/ackify-ce/internal/infrastructure/config" "github.com/btouchard/ackify-ce/internal/infrastructure/database" "github.com/btouchard/ackify-ce/internal/presentation/handlers" - "github.com/btouchard/ackify-ce/internal/presentation/templates" "github.com/btouchard/ackify-ce/pkg/crypto" + "github.com/btouchard/ackify-ce/webtemplates" ) // Server represents the Ackify CE web server @@ -123,7 +123,7 @@ func initInfrastructure(ctx context.Context) (*config.Config, *sql.DB, *template } // Initialize templates - tmpl, err := templates.InitTemplates() + tmpl, err := webtemplates.InitTemplates() if err != nil { return nil, nil, nil, nil, fmt.Errorf("failed to initialize templates: %w", err) } diff --git a/webtemplates/embed.go b/webtemplates/embed.go new file mode 100644 index 0000000..b628ce2 --- /dev/null +++ b/webtemplates/embed.go @@ -0,0 +1,30 @@ +package webtemplates + +import ( + "embed" + "fmt" + "html/template" +) + +//go:embed templates/*.tpl +var TemplatesFS embed.FS + +// InitTemplates initializes the HTML templates from embedded files +func InitTemplates() (*template.Template, error) { + // Parse the base template first + tmpl, err := template.New("base").ParseFS(TemplatesFS, "templates/base.html.tpl") + if err != nil { + return nil, fmt.Errorf("failed to parse base template: %w", err) + } + + // Parse the additional templates + additionalTemplates := []string{"templates/index.html.tpl", "templates/sign.html.tpl", "templates/signatures.html.tpl", "templates/embed.html.tpl"} + for _, templateFile := range additionalTemplates { + _, err = tmpl.ParseFS(TemplatesFS, templateFile) + if err != nil { + return nil, fmt.Errorf("failed to parse template %s: %w", templateFile, err) + } + } + + return tmpl, nil +} diff --git a/webtemplates/embed_test.go b/webtemplates/embed_test.go new file mode 100644 index 0000000..344c506 --- /dev/null +++ b/webtemplates/embed_test.go @@ -0,0 +1,54 @@ +package webtemplates + +import ( + "testing" +) + +func TestTemplatesFS(t *testing.T) { + // Test that the embedded filesystem contains the expected files + expectedFiles := []string{ + "templates/base.html.tpl", + "templates/index.html.tpl", + "templates/sign.html.tpl", + "templates/signatures.html.tpl", + "templates/embed.html.tpl", + } + + for _, file := range expectedFiles { + data, err := TemplatesFS.ReadFile(file) + if err != nil { + t.Errorf("Failed to read embedded file %s: %v", file, err) + } + if len(data) == 0 { + t.Errorf("Embedded file %s is empty", file) + } + } +} + +func TestInitTemplates(t *testing.T) { + // Test that InitTemplates works correctly + tmpl, err := InitTemplates() + if err != nil { + t.Fatalf("InitTemplates failed: %v", err) + } + + if tmpl == nil { + t.Fatal("InitTemplates returned nil template") + } + + // Test that all expected templates are parsed + expectedTemplateNames := []string{ + "base", + "base.html.tpl", + "index.html.tpl", + "sign.html.tpl", + "signatures.html.tpl", + "embed.html.tpl", + } + + for _, name := range expectedTemplateNames { + if tmpl.Lookup(name) == nil { + t.Errorf("Template %s not found in parsed templates", name) + } + } +} diff --git a/web/templates/base.html.tpl b/webtemplates/templates/base.html.tpl similarity index 100% rename from web/templates/base.html.tpl rename to webtemplates/templates/base.html.tpl diff --git a/web/templates/embed.html.tpl b/webtemplates/templates/embed.html.tpl similarity index 100% rename from web/templates/embed.html.tpl rename to webtemplates/templates/embed.html.tpl diff --git a/web/templates/index.html.tpl b/webtemplates/templates/index.html.tpl similarity index 100% rename from web/templates/index.html.tpl rename to webtemplates/templates/index.html.tpl diff --git a/web/templates/sign.html.tpl b/webtemplates/templates/sign.html.tpl similarity index 100% rename from web/templates/sign.html.tpl rename to webtemplates/templates/sign.html.tpl diff --git a/web/templates/signatures.html.tpl b/webtemplates/templates/signatures.html.tpl similarity index 100% rename from web/templates/signatures.html.tpl rename to webtemplates/templates/signatures.html.tpl