only load .env file when not in prod
This commit is contained in:
parent
48e6a851fa
commit
0f95094014
1 changed files with 23 additions and 21 deletions
44
cmd/main.go
44
cmd/main.go
|
|
@ -10,39 +10,41 @@ import (
|
||||||
"github.com/labstack/echo/v4"
|
"github.com/labstack/echo/v4"
|
||||||
"github.com/labstack/echo/v4/middleware"
|
"github.com/labstack/echo/v4/middleware"
|
||||||
|
|
||||||
"github.com/juancwu/potoforio/pkg/pages"
|
"github.com/juancwu/potoforio/pkg/pages"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TemplateRenderer struct {
|
type TemplateRenderer struct {
|
||||||
templates *template.Template
|
templates *template.Template
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Load connection string from .env file
|
// Load connection string from .env file
|
||||||
err := godotenv.Load()
|
if os.Getenv("GO_ENV") != "production" {
|
||||||
if err != nil {
|
err := godotenv.Load()
|
||||||
log.Fatal("failed to load env", err)
|
if err != nil {
|
||||||
}
|
log.Fatal("failed to load env", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
templates, err := template.New("").ParseGlob("public/views/*.html")
|
templates, err := template.New("").ParseGlob("public/views/*.html")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Error initializing templates: %v", err)
|
log.Fatalf("Error initializing templates: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.Renderer = &TemplateRenderer{
|
e.Renderer = &TemplateRenderer{
|
||||||
templates: templates,
|
templates: templates,
|
||||||
}
|
}
|
||||||
|
|
||||||
e.Use(middleware.Logger())
|
e.Use(middleware.Logger())
|
||||||
e.Static("/static", "static")
|
e.Static("/static", "static")
|
||||||
|
|
||||||
e.GET("/", pages.Index)
|
e.GET("/", pages.Index)
|
||||||
|
|
||||||
e.Logger.Fatal(e.Start(":5173"))
|
e.Logger.Fatal(e.Start(":5173"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
func (t *TemplateRenderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
|
||||||
return t.templates.ExecuteTemplate(w, name, data)
|
return t.templates.ExecuteTemplate(w, name, data)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue