Skip to content

Commit

Permalink
refactor(server): embed templates - avoid relying on the container
Browse files Browse the repository at this point in the history
  • Loading branch information
tgragnato committed Jan 5, 2024
1 parent aee0bc8 commit 374d6dd
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 36 deletions.
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ RUN go build
FROM alpine:3.19
WORKDIR /tmp
COPY --from=builder /workspace/inca /usr/sbin/
COPY --from=builder /workspace/server/views /tmp/server/views
ENTRYPOINT ["/usr/sbin/inca"]
LABEL org.opencontainers.image.source=https://github.com/immobiliare/inca
35 changes: 8 additions & 27 deletions server/config/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,18 @@ import (
"gopkg.in/yaml.v2"
)

const (
defaultEnvironment = "development"
defaultTemplatesPath = "./server/views"
)

type Config struct {
Sentry string
Environment string
TemplatesPath string
Storage *storage.Storage
Providers []*provider.Provider
ACL map[string][]string
Sentry string
Storage *storage.Storage
Providers []*provider.Provider
ACL map[string][]string
}

type Wrapper struct {
Sentry string `yaml:"sentry"`
TemplatesPath string `yaml:"templates_path"`
Environment string `yaml:"environment"`
Storage map[string]interface{} `yaml:"storage"`
Providers []map[string]interface{} `yaml:"providers"`
ACL map[string][]string `yaml:"acl"`
Sentry string `yaml:"sentry"`
Storage map[string]interface{} `yaml:"storage"`
Providers []map[string]interface{} `yaml:"providers"`
ACL map[string][]string `yaml:"acl"`
}

func Parse(path string) (*Config, error) {
Expand Down Expand Up @@ -79,16 +70,6 @@ func Parse(path string) (*Config, error) {
cfg.Sentry = wrapper.Sentry
}

cfg.Environment = defaultEnvironment
if len(wrapper.Environment) > 0 {
cfg.Environment = wrapper.Environment
}

cfg.TemplatesPath = defaultTemplatesPath
if len(wrapper.TemplatesPath) > 0 {
cfg.TemplatesPath = wrapper.TemplatesPath
}

if len(wrapper.ACL) > 0 {
cfg.ACL = wrapper.ACL
}
Expand Down
14 changes: 8 additions & 6 deletions server/inca.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ type Inca struct {
//go:embed static/**
var embedStatic embed.FS

//go:embed views/**
var embedViews embed.FS

func Spinup(path string) (*Inca, error) {
cfg, err := config.Parse(path)
if err != nil {
Expand All @@ -53,16 +56,15 @@ func Spinup(path string) (*Inca, error) {
log.Info().Msg("sentry correctly initialized")
}

templateEngine := django.New(cfg.TemplatesPath, ".html.j2")
templateEngine.Reload(strings.EqualFold(cfg.Environment, "development"))
templateEngine.Debug(strings.EqualFold(cfg.Environment, "development"))

inca := &Inca{
fiber.New(
fiber.Config{
DisableStartupMessage: true,
Views: templateEngine,
// Views: html.NewFileSystem(http.Dir("./server/views"), ".html.j2"),
Views: django.NewPathForwardingFileSystem(
http.FS(embedViews),
"/views",
".html.j2",
),
},
),
cfg.Storage,
Expand Down
2 changes: 0 additions & 2 deletions server/inca_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const (
testingCACrtPath = ".testServerInca.crt.pem"
testingCAKeyPath = ".testServerInca.key.pem"
testingConfig = `bind: :65535
templates_path: ./views
environment: production
storage:
type: fs
path: ./
Expand Down

0 comments on commit 374d6dd

Please sign in to comment.