diff --git a/Dockerfile.v2 b/Dockerfile.v2 new file mode 100644 index 0000000..6952836 --- /dev/null +++ b/Dockerfile.v2 @@ -0,0 +1,23 @@ +FROM docker.hub.ipao.vip/jrottenberg/ffmpeg:4.4-alpine + +# Set timezone +RUN apk add --no-cache tzdata && \ + cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \ + echo "Asia/Shanghai" > /etc/timezone && \ + apk del tzdata && \ + mkdir -p /app/downloads /app/dist + +COPY backend_v1/build/app /app/app +COPY backend_v1/config.prod.toml /app/config.toml +COPY frontend/admin/dist /app/dist/admin +COPY frontend/wechat/dist /app/dist/wechat + +WORKDIR /app + +ENTRYPOINT ["/app/app"] + +EXPOSE 9888 + +VOLUME ["/app/downloads"] + +CMD [ "serve" ] diff --git a/backend_v1/app/commands/http/http.go b/backend_v1/app/commands/http/http.go index e5c1072..c7c96cd 100644 --- a/backend_v1/app/commands/http/http.go +++ b/backend_v1/app/commands/http/http.go @@ -2,6 +2,8 @@ package http import ( "context" + "mime" + "path/filepath" "quyun/v2/app/commands" "quyun/v2/app/errorx" @@ -21,7 +23,9 @@ import ( "go.ipao.vip/atom/container" "go.ipao.vip/atom/contracts" + "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/favicon" + "github.com/rogeecn/fabfile" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" "go.uber.org/dig" @@ -86,6 +90,38 @@ func Serve(cmd *cobra.Command, args []string) error { route.Register(group) } + svc.Http.Engine.Get("/admin*", checkStaticFile(svc.App.DistAdmin)) + svc.Http.Engine.Get("/*", checkStaticFile(svc.App.DistWeChat)) + return svc.Http.Serve(ctx) }) } + +func checkStaticFile(rootPath string) func(ctx fiber.Ctx) error { + return func(ctx fiber.Ctx) error { + f := ctx.Params("*") + if f == "/" || f == "" { + f = "index.html" + } + + checkFiles := []string{f, "index.html"} + for _, checkFile := range checkFiles { + filePath := filepath.Join(rootPath, checkFile) + log.Infof("check file: %s", filePath) + file, err := fabfile.Find(filePath) + if err != nil { + log.Warnf("file not found: %s", filePath) + continue + } + + ext := filepath.Ext(filePath) + mime := mime.TypeByExtension(ext) + + log.Infof("mime type: %s %s", ext, mime) + ctx.Set(fiber.HeaderContentType, mime) + + return ctx.SendFile(file) + } + return ctx.SendStatus(fiber.StatusNotFound) + } +} diff --git a/backend_v1/app/http/dto/post.go b/backend_v1/app/http/dto/post.go index 8238934..a6940d0 100644 --- a/backend_v1/app/http/dto/post.go +++ b/backend_v1/app/http/dto/post.go @@ -7,6 +7,6 @@ import ( type PostListQuery struct { *requests.Pagination - Keyword *string `query:"keyword"` - Status fields.PostStatus `query:"status"` + Keyword *string `query:"keyword"` + Status *fields.PostStatus `query:"status"` } diff --git a/backend_v1/app/http/posts.go b/backend_v1/app/http/posts.go index d64bfad..cfe55e7 100644 --- a/backend_v1/app/http/posts.go +++ b/backend_v1/app/http/posts.go @@ -44,8 +44,7 @@ type posts struct { // @Bind query query // @Bind user local func (ctl *posts) List(ctx fiber.Ctx, query *dto.PostListQuery, user *models.User) (*requests.Pager, error) { - query.Status = fields.PostStatusPublished - + query.Status = lo.ToPtr(fields.PostStatusPublished) pager, err := services.Posts.List(ctx, query) if err != nil { log.WithError(err).Errorf("post list err: %v", err) diff --git a/backend_v1/app/services/posts.go b/backend_v1/app/services/posts.go index 3431be4..52598b3 100644 --- a/backend_v1/app/services/posts.go +++ b/backend_v1/app/services/posts.go @@ -42,6 +42,11 @@ func (m *posts) List(ctx context.Context, filter *dto.PostListQuery) (*requests. query = query.Where(tbl.Title.Like(keyword)) } + // filter status + if filter.Status != nil { + query = query.Where(tbl.Status.Eq(*filter.Status)) + } + items, cnt, err := query.FindByPage(int(filter.Offset()), int(filter.Limit)) if err != nil { return nil, errors.Wrap(err, "list post failed") diff --git a/backend_v1/providers/http/engine.go b/backend_v1/providers/http/engine.go index 8a81bd4..b2cf760 100644 --- a/backend_v1/providers/http/engine.go +++ b/backend_v1/providers/http/engine.go @@ -15,7 +15,6 @@ import ( "github.com/gofiber/fiber/v3" "github.com/gofiber/fiber/v3/middleware/compress" "github.com/gofiber/fiber/v3/middleware/cors" - "github.com/gofiber/fiber/v3/middleware/helmet" "github.com/gofiber/fiber/v3/middleware/logger" "github.com/gofiber/fiber/v3/middleware/recover" "github.com/gofiber/fiber/v3/middleware/requestid" @@ -115,17 +114,17 @@ func Provide(opts ...opt.Option) error { }, })) - // basic security + compression - engine.Use(helmet.New()) engine.Use(compress.New(compress.Config{Level: compress.LevelDefault})) + // basic security + compression + // engine.Use(helmet.New()) // optional CORS based on config - if config.Cors != nil { - corsCfg := buildCORSConfig(config.Cors) - if corsCfg != nil { - engine.Use(cors.New(*corsCfg)) - } - } + // if config.Cors != nil { + // corsCfg := buildCORSConfig(config.Cors) + // if corsCfg != nil { + // engine.Use(cors.New(*corsCfg)) + // } + // } // logging with request id and latency engine.Use(logger.New(logger.Config{ diff --git a/build.v2.sh b/build.v2.sh index 48e183d..6a62c49 100755 --- a/build.v2.sh +++ b/build.v2.sh @@ -56,7 +56,7 @@ build_backend() { build_image() { log "Building Docker image ${DOCKER_IMAGE}:v2" - sudo docker build -f Dockerfile -t "${DOCKER_IMAGE}:v2" "$ROOT_DIR" + sudo docker build -f Dockerfile.v2 -t "${DOCKER_IMAGE}:v2" "$ROOT_DIR" } export_image() {