From 1bfca0b962d3fb3667f1404ed77f0af221696bf7 Mon Sep 17 00:00:00 2001 From: timerzz Date: Tue, 27 Aug 2024 14:25:21 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E6=B7=BB=E5=8A=A0=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cmd/provider.go | 47 ++++++++++++++++++++++++++++- go.mod | 12 +++++++- go.sum | 32 ++++++++++++++++---- service/provider_svc.go | 67 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 148 insertions(+), 10 deletions(-) diff --git a/cmd/provider.go b/cmd/provider.go index 7905807..1d7fe6a 100644 --- a/cmd/provider.go +++ b/cmd/provider.go @@ -1,5 +1,50 @@ package main -func main() { +import ( + "os" + "gitea.timerzz.com/kedaya_haitao/common/pkg/database" + "gitea.timerzz.com/kedaya_haitao/common/pkg/web" + "gitea.timerzz.com/kedaya_haitao/common/structs/storage" + "gitea.timerzz.com/kedaya_haitao/provider/service" + "github.com/gofiber/fiber/v3" + "github.com/gofiber/fiber/v3/middleware/cors" + "github.com/gofiber/fiber/v3/middleware/recover" + + "github.com/golang/glog" +) + +func main() { + glog.Info(">>> BEGIN INIT<<<") + + // 初始化数据库 + db, err := database.InitDefaultDatabase() + if err != nil { + glog.Fatalf("init database failed: %v", err) + } + // 初始化服务 + r := fiber.New() + r.Use(cors.New(), recover.New()) + + stg := storage.NewStorage(db) + if err = stg.Provider().AutoMigrate(); err != nil { + glog.Fatalf("autoMigrate database failed: %v", err) + } + + svc := []web.Register{ + web.NewProbe(), + service.NewProvider(stg), + } + + for _, s := range svc { + s.Registry(r) + } + + port := os.Getenv("PORT") + if port == "" { + port = "80" + } + if err = r.Listen(":" + port); err != nil { + glog.Warningf("service over: %v", err) + } } diff --git a/go.mod b/go.mod index c4abf9d..014865d 100644 --- a/go.mod +++ b/go.mod @@ -3,24 +3,34 @@ module gitea.timerzz.com/kedaya_haitao/provider go 1.22.5 require ( - gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826081424-383f4844cf3b + gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240827061931-68e2a214029c github.com/gofiber/fiber/v3 v3.0.0-beta.3 + github.com/golang/glog v1.2.1 + github.com/henrylee2cn/ameda v1.4.10 ) require ( github.com/andybalholm/brotli v1.1.0 // indirect github.com/gofiber/utils/v2 v2.0.0-beta.4 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.4.3 // indirect github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/klauspost/compress v1.17.9 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/pkg/errors v0.9.1 // indirect github.com/samber/lo v1.39.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasthttp v1.55.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect + golang.org/x/crypto v0.24.0 // indirect golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect golang.org/x/sys v0.21.0 // indirect + golang.org/x/text v0.16.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect + gorm.io/driver/postgres v1.5.7 // indirect gorm.io/gorm v1.25.10 // indirect ) diff --git a/go.sum b/go.sum index 8c7248c..56f9253 100644 --- a/go.sum +++ b/go.sum @@ -1,19 +1,22 @@ -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826071504-baeca78c5f26 h1:pUzRWD5/EZ9dfO+LvX88xgEaURazjZNnwk17AYOA3i0= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826071504-baeca78c5f26/go.mod h1:uV586p6Z8LIq3I8O/pXZv+jIkIwnwBjAz0D7KrhB9JM= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826080643-651c11c544e1 h1:w/1+4QLi4TuG4k3Me9wcONqMd6A5XOFLFfVwkZWo5Q4= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826080643-651c11c544e1/go.mod h1:VvR9l3uJFxfHCC+cKeVtNgEz35R7tWs/f2mGspaoUpU= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826081424-383f4844cf3b h1:OdiDAWwgDIf8oVjr2TFkQ+5wdXC4mlo+d0wE8nGo6yg= -gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240826081424-383f4844cf3b/go.mod h1:VvR9l3uJFxfHCC+cKeVtNgEz35R7tWs/f2mGspaoUpU= +gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240827061931-68e2a214029c h1:P+iJCCbkkK/2OQKAoGy8oLaPWe1qQ43PbRVCY9huCZU= +gitea.timerzz.com/kedaya_haitao/common v0.0.0-20240827061931-68e2a214029c/go.mod h1:VvR9l3uJFxfHCC+cKeVtNgEz35R7tWs/f2mGspaoUpU= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/gofiber/fiber/v3 v3.0.0-beta.3 h1:7Q2I+HsIqnIEEDB+9oe7Gadpakh6ZLhXpTYz/L20vrg= github.com/gofiber/fiber/v3 v3.0.0-beta.3/go.mod h1:kcMur0Dxqk91R7p4vxEpJfDWZ9u5IfvrtQc8Bvv/JmY= github.com/gofiber/utils/v2 v2.0.0-beta.4 h1:1gjbVFFwVwUb9arPcqiB6iEjHBwo7cHsyS41NeIW3co= github.com/gofiber/utils/v2 v2.0.0-beta.4/go.mod h1:sdRsPU1FXX6YiDGGxd+q2aPJRMzpsxdzCXo9dz+xtOY= +github.com/golang/glog v1.2.1 h1:OptwRhECazUx5ix5TTWC3EZhsZEHWcYWY4FQHTIubm4= +github.com/golang/glog v1.2.1/go.mod h1:6AhwSGph0fcJtXVM/PEHPqZlFeoLxhs7/t5UDAwmO+w= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/henrylee2cn/ameda v1.4.10 h1:JdvI2Ekq7tapdPsuhrc4CaFiqw6QXFvZIULWJgQyCAk= +github.com/henrylee2cn/ameda v1.4.10/go.mod h1:liZulR8DgHxdK+MEwvZIylGnmcjzQ6N6f2PlWe7nEO4= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= @@ -26,15 +29,27 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= +github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= +github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= @@ -53,6 +68,11 @@ golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/postgres v1.5.7 h1:8ptbNJTDbEmhdr62uReG5BGkdQyeasu/FZHxI0IMGnM= diff --git a/service/provider_svc.go b/service/provider_svc.go index fe8e285..a1a6a63 100644 --- a/service/provider_svc.go +++ b/service/provider_svc.go @@ -3,7 +3,9 @@ package service import ( "gitea.timerzz.com/kedaya_haitao/common/pkg/web" "gitea.timerzz.com/kedaya_haitao/common/structs/storage" + v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" "github.com/gofiber/fiber/v3" + "github.com/henrylee2cn/ameda" ) type Provider struct { @@ -17,12 +19,17 @@ func NewProvider(storage *storage.Storage) *Provider { } func (s *Provider) Registry(r fiber.Router) { - r.Get("providers", s.ListProviders) + api := r.Group("/api/v2") + api.Get("providers", s.ListProviders) + api.Get("/providers/:id", s.GetProvider) + api.Post("providers", s.CreateProvider) + api.Put("providers", s.UpdateProvider) + api.Delete("/providers/:id", s.DeleteProvider) } func (s *Provider) ListProviders(c fiber.Ctx) error { query := storage.NewPageListQuery(&storage.ListProviderQuery{}) - if err := c.Bind().Query(&query); err != nil { + if err := c.Bind().Query(query); err != nil { return err } @@ -33,3 +40,59 @@ func (s *Provider) ListProviders(c fiber.Ctx) error { return c.JSON(web.NewResponse(web.NewListResponse(total, providers))) } + +func (s *Provider) CreateProvider(c fiber.Ctx) error { + var provider v2.Provider + if err := c.Bind().JSON(&provider); err != nil { + return err + } + if err := s.storage.Provider().Create(provider); err != nil { + return err + } + return c.JSON(web.NewResponse(provider)) +} + +func (s *Provider) UpdateProvider(c fiber.Ctx) error { + var provider v2.Provider + if err := c.Bind().JSON(&provider); err != nil { + return err + } + if err := s.storage.Provider().Update(provider); err != nil { + return err + } + return c.JSON(web.NewResponse(provider)) +} + +func (s *Provider) GetProvider(c fiber.Ctx) error { + sid := c.Params("id") + if sid == "" { + return fiber.ErrBadRequest + } + id, err := ameda.Atoi(sid) + if err != nil { + return fiber.ErrBadRequest + } + provider, err := s.storage.Provider().Get(uint(id)) + + if err != nil { + return err + } + + return c.JSON(web.NewResponse(provider)) +} + +func (s *Provider) DeleteProvider(c fiber.Ctx) error { + sid := c.Params("id") + if sid == "" { + return fiber.ErrBadRequest + } + id, err := ameda.Atoi(sid) + if err != nil { + return fiber.ErrBadRequest + } + provider, err := s.storage.Provider().Delete(uint(id)) + if err != nil { + return err + } + return c.JSON(web.NewResponse(provider)) +}