Skip to content

Commit

Permalink
Merge pull request #260 from xuewenG/feature/init-static-files
Browse files Browse the repository at this point in the history
简化代码结构,移除 main_prod.go
  • Loading branch information
xuewenG authored Jan 23, 2025
2 parents d065a41 + 83970c3 commit 676ce4c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 103 deletions.
13 changes: 13 additions & 0 deletions backend/init_static_files_prod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//go:build prod
package main

import (
"embed"
)

//go:embed public/*
var embedPublicFiles embed.FS

func init() {
staticFiles = &embedPublicFiles
}
21 changes: 19 additions & 2 deletions backend/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
//go:build !prod

package main

import (
"embed"
"fmt"
"net/http"
"strings"

"github.com/ilyakaznacheev/cleanenv"
_ "github.com/joho/godotenv/autoload"
Expand All @@ -14,6 +15,7 @@ import (
myMiddleware "github.com/kingwrcy/moments/middleware"
"github.com/kingwrcy/moments/vo"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/rs/zerolog"
"github.com/samber/do/v2"
_ "github.com/swaggo/echo-swagger"
Expand All @@ -23,6 +25,8 @@ import (
var version string
var commitId string

var staticFiles *embed.FS

func newEchoEngine(_ do.Injector) (*echo.Echo, error) {
e := echo.New()
return e, nil
Expand Down Expand Up @@ -71,6 +75,19 @@ func main() {

setupRouter(injector)

if staticFiles != nil {
e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Root: "public",
HTML5: true,
IgnoreBase: false,
Browse: false,
Filesystem: http.FS(staticFiles),
Skipper: func(c echo.Context) bool {
return strings.HasPrefix(c.Request().URL.Path, "/swagger/")
},
}))
}

migrateTo3(tx, myLogger)

e.HideBanner = true
Expand Down
101 changes: 0 additions & 101 deletions backend/main_prod.go

This file was deleted.

0 comments on commit 676ce4c

Please sign in to comment.