From a743f16470b835f798760e0fc67d2f6c3059b62f Mon Sep 17 00:00:00 2001 From: wj00037 <1292876134@qq.com> Date: Sat, 12 Oct 2024 16:09:47 +0800 Subject: [PATCH 1/4] mv main.go, add healthcheck --- go.mod | 7 +-- go.sum | 11 ++++ main.go | 127 +++++++++++++++++++++++++++++++++++++++++++++++ server/server.go | 18 +++++++ 4 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 main.go diff --git a/go.mod b/go.mod index 624b6cd..dd76018 100644 --- a/go.mod +++ b/go.mod @@ -10,8 +10,10 @@ require ( sigs.k8s.io/yaml v1.4.0 ) +require github.com/aws/aws-lambda-go v1.43.0 // indirect + require ( - github.com/davecgh/go-spew v1.1.1 // indirect + github.com/akrylysov/algnhsa v1.1.0 github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/goccy/go-json v0.10.3 // indirect @@ -19,11 +21,10 @@ require ( github.com/klauspost/compress v1.17.10 // indirect github.com/klauspost/cpuid/v2 v2.2.8 // indirect github.com/minio/md5-simd v1.1.2 // indirect - github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rs/xid v1.6.0 // indirect + github.com/sirupsen/logrus v1.9.3 golang.org/x/crypto v0.28.0 // indirect golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f986c95..82c9f83 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,8 @@ +github.com/akrylysov/algnhsa v1.1.0 h1:G0SoP16tMRyiism7VNc3JFA0wq/cVgEkp/ExMVnc6PQ= +github.com/akrylysov/algnhsa v1.1.0/go.mod h1:+bOweRs/WBu5awl+ifCoSYAuKVPAmoTk8XOMrZ1xwiw= +github.com/aws/aws-lambda-go v1.43.0 h1:Tdu7SnMB5bD+CbdnSq1Dg4sM68vEuGIDcQFZ+IjUfx0= +github.com/aws/aws-lambda-go v1.43.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= +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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= @@ -25,12 +30,17 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= +github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +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= golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= +golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= @@ -38,6 +48,7 @@ golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +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= sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= diff --git a/main.go b/main.go new file mode 100644 index 0000000..cce8ba3 --- /dev/null +++ b/main.go @@ -0,0 +1,127 @@ +package main + +import ( + "flag" + "fmt" + "log" + "net/http" + "os" + "strings" + + "github.com/akrylysov/algnhsa" + "github.com/metalogical/BigFiles/auth" + "github.com/metalogical/BigFiles/config" + "github.com/metalogical/BigFiles/server" + "github.com/sirupsen/logrus" +) + +type options struct { + service ServiceOptions + enableDebug bool +} + +type ServiceOptions struct { + ConfigFile string + RemoveCfg bool +} + +// Validate checks if the ServiceOptions are valid. +// It returns an error if the config file is missing. +func (o *ServiceOptions) Validate() error { + if o.ConfigFile == "" { + return fmt.Errorf("missing config-file") + } + + return nil +} + +// AddFlags adds flags for ServiceOptions to the provided FlagSet. +func (o *ServiceOptions) AddFlags(fs *flag.FlagSet) { + fs.StringVar(&o.ConfigFile, "config-file", "", "Path to config file.") + fs.BoolVar(&o.RemoveCfg, "rm-cfg", false, "whether remove the cfg file after initialized .") +} + +// Validate validates the options and returns an error if any validation fails. +func (o *options) Validate() error { + return o.service.Validate() +} + +func gatherOptions(fs *flag.FlagSet, args ...string) (options, error) { + var o options + o.service.AddFlags(fs) + + fs.BoolVar( + &o.enableDebug, "enable_debug", false, "whether to enable debug model.", + ) + + err := fs.Parse(args) + return o, err +} + +func main() { + o, err := gatherOptions( + flag.NewFlagSet(os.Args[0], flag.ExitOnError), + os.Args[1:]..., + ) + if err != nil { + logrus.Errorf("new options failed, err:%s", err.Error()) + + return + } + + if err := o.Validate(); err != nil { + logrus.Errorf("Invalid options, err:%s", err.Error()) + + return + } + + if o.enableDebug { + logrus.SetLevel(logrus.DebugLevel) + logrus.Debug("debug enable.") + } + + //cfg + cfg := new(config.Config) + + if err := config.LoadConfig(o.service.ConfigFile, cfg, o.service.RemoveCfg); err != nil { + logrus.Errorf("load config, err:%s", err.Error()) + + return + } + + if err := auth.Init(cfg); err != nil { + logrus.Errorf("load gitee config, err:%s", err.Error()) + + return + } + + bucket := cfg.LfsBucket + if bucket == "" { + bucket = os.Getenv("LFS_BUCKET") + if bucket == "" { + logrus.Errorf("LFS_BUCKET must be set") + } + } + + s, err := server.New(server.Options{ + Prefix: cfg.Prefix, + Bucket: bucket, + Endpoint: cfg.AwsRegion, + AccessKeyID: cfg.AwsAccessKeyId, + S3Accelerate: true, + IsAuthorized: auth.GiteeAuth(), + SecretAccessKey: cfg.AwsSecretAccessKey, + }) + if err != nil { + log.Fatalln(err) + } + + if strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda_") { + algnhsa.ListenAndServe(s, nil) + } else { + log.Println("serving on http://127.0.0.1:5000 ...") + if err := http.ListenAndServe("127.0.0.1:5000", s); err != nil { + log.Fatalln(err) + } + } +} diff --git a/server/server.go b/server/server.go index 746aa27..aac191a 100644 --- a/server/server.go +++ b/server/server.go @@ -96,10 +96,28 @@ func New(o Options) (http.Handler, error) { r := chi.NewRouter() r.Post("/{owner}/{repo}/objects/batch", s.handleBatch) + r.Get("/", s.healthCheck) return r, nil } +type SuccessResponse struct { + Message string `json:"message"` + Data interface{} `json:"data,omitempty"` // 可选数据字段 +} + +func (s *server) healthCheck(w http.ResponseWriter, r *http.Request) { + response := SuccessResponse{ + Message: "Success", + Data: "healthCheck success", // 替换为实际的数据 + } + + // 设置响应头 + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) // 200 + must(json.NewEncoder(w).Encode(response)) +} + type server struct { client *minio.Client bucket string From f03bf40ccf1e0c3a625e2d0627600484606f6641 Mon Sep 17 00:00:00 2001 From: wj00037 <1292876134@qq.com> Date: Sat, 12 Oct 2024 16:15:08 +0800 Subject: [PATCH 2/4] update Dockerfile --- DockerFile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/DockerFile b/DockerFile index 800c9d8..4f5a885 100644 --- a/DockerFile +++ b/DockerFile @@ -4,7 +4,7 @@ WORKDIR /home COPY . . RUN go get github.com/akrylysov/algnhsa && \ go get github.com/sirupsen/logrus && \ - go build ./BigFiles/main.go + go build ./main.go FROM openeuler/openeuler:22.03 LABEL maintainer="Zhou Yi 1123678689@qq.com" From 5535ef309907d8802777f70d081764ae9cdf1f6f Mon Sep 17 00:00:00 2001 From: wj00037 <1292876134@qq.com> Date: Sat, 12 Oct 2024 16:20:41 +0800 Subject: [PATCH 3/4] update healthcheck --- BigFiles/go.mod | 39 --------------- BigFiles/go.sum | 103 -------------------------------------- BigFiles/main.go | 127 ----------------------------------------------- batch/types.go | 5 ++ main.go | 4 +- server/server.go | 32 ++++++------ 6 files changed, 21 insertions(+), 289 deletions(-) delete mode 100644 BigFiles/go.mod delete mode 100644 BigFiles/go.sum delete mode 100644 BigFiles/main.go diff --git a/BigFiles/go.mod b/BigFiles/go.mod deleted file mode 100644 index 9e610a0..0000000 --- a/BigFiles/go.mod +++ /dev/null @@ -1,39 +0,0 @@ -module github.com/metalogical/BigFiles/BigFiles - -go 1.21 - -toolchain go1.23.1 - -require ( - github.com/akrylysov/algnhsa v1.1.0 - github.com/metalogical/BigFiles v0.0.0-20201103191605-ca95c8c717cc - github.com/sirupsen/logrus v1.9.3 -) - -require ( - github.com/aws/aws-lambda-go v1.47.0 // indirect - github.com/dustin/go-humanize v1.0.1 // indirect - github.com/go-chi/chi v4.1.2+incompatible // indirect - github.com/go-ini/ini v1.67.0 // indirect - github.com/goccy/go-json v0.10.3 // indirect - github.com/google/uuid v1.6.0 // indirect - github.com/klauspost/compress v1.17.10 // indirect - github.com/klauspost/cpuid/v2 v2.2.8 // indirect - github.com/minio/md5-simd v1.1.2 // indirect - github.com/minio/minio-go/v7 v7.0.77 // indirect - github.com/rs/xid v1.6.0 // indirect - golang.org/x/crypto v0.28.0 // indirect - golang.org/x/net v0.30.0 // indirect - golang.org/x/sys v0.26.0 // indirect - golang.org/x/text v0.19.0 // indirect - sigs.k8s.io/yaml v1.4.0 // indirect -) - -replace ( - github.com/metalogical/BigFiles => github.com/opensourceways/BigFiles v0.0.0-20240930093226-cec367139628 - golang.org/x/crypto => golang.org/x/crypto v0.28.0 - golang.org/x/net => golang.org/x/net v0.30.0 - golang.org/x/sys => golang.org/x/sys v0.26.0 - golang.org/x/text => golang.org/x/text v0.19.0 - gopkg.in/yaml.v3 => gopkg.in/yaml.v3 v3.0.1 -) diff --git a/BigFiles/go.sum b/BigFiles/go.sum deleted file mode 100644 index b04e55b..0000000 --- a/BigFiles/go.sum +++ /dev/null @@ -1,103 +0,0 @@ -github.com/akrylysov/algnhsa v1.1.0 h1:G0SoP16tMRyiism7VNc3JFA0wq/cVgEkp/ExMVnc6PQ= -github.com/akrylysov/algnhsa v1.1.0/go.mod h1:+bOweRs/WBu5awl+ifCoSYAuKVPAmoTk8XOMrZ1xwiw= -github.com/aws/aws-lambda-go v1.47.0 h1:0H8s0vumYx/YKs4sE7YM0ktwL2eWse+kfopsRI1sXVI= -github.com/aws/aws-lambda-go v1.47.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= -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/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= -github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= -github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec= -github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= -github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= -github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= -github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= -github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -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/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= -github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= -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/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= -github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= -github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= -github.com/klauspost/compress v1.17.10 h1:oXAz+Vh0PMUvJczoi+flxpnBEPxoER1IaAnU/NMPtT0= -github.com/klauspost/compress v1.17.10/go.mod h1:pMDklpSncoRMuLFrf1W9Ss9KT+0rH90U12bZKk7uwG0= -github.com/klauspost/cpuid v1.2.3/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek= -github.com/klauspost/cpuid v1.3.1/go.mod h1:bYW4mA6ZgKPob1/Dlai2LviZJO7KGI3uoWLd42rAQw4= -github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= -github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= -github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= -github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= -github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= -github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= -github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= -github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= -github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= -github.com/minio/minio-go/v7 v7.0.5/go.mod h1:TA0CQCjJZHM5SJj9IjqR0NmpmQJ6bCbXifAJ3mUU6Hw= -github.com/minio/minio-go/v7 v7.0.77 h1:GaGghJRg9nwDVlNbwYjSDJT1rqltQkBFDsypWX1v3Bw= -github.com/minio/minio-go/v7 v7.0.77/go.mod h1:AVM3IUN6WwKzmwBxVdjzhH8xq+f57JSbbvzqvUzR6eg= -github.com/minio/sha256-simd v0.1.1/go.mod h1:B5e1o+1/KgNmWrSQK08Y6Z1Vb5pwIktudl0J58iy0KM= -github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0= -github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= -github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= -github.com/opensourceways/BigFiles v0.0.0-20240930093226-cec367139628 h1:eGtc4dS4Sgs6ocFSMxleOVMFKXVYRblrE1mJ099jIMA= -github.com/opensourceways/BigFiles v0.0.0-20240930093226-cec367139628/go.mod h1:wC3lLX+rQwPVoY3U0IAAEJsxL5lraJC3e1SCDeddgSc= -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/rs/xid v1.2.1/go.mod h1:+uKXf+4Djp6Md1KODXJxgGQPKngRmWyn10oCKFzNHOQ= -github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= -github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= -github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= -github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= -github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= -github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -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/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= -golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= -golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= -golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= -golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= -golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE= -golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M= -golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= -golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= -golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= -golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= -golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58= -golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk= -golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/ini.v1 v1.57.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= -gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= -gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E= -sigs.k8s.io/yaml v1.4.0/go.mod h1:Ejl7/uTz7PSA4eKMyQCUTnhZYNmLIl+5c2lQPGR2BPY= diff --git a/BigFiles/main.go b/BigFiles/main.go deleted file mode 100644 index cce8ba3..0000000 --- a/BigFiles/main.go +++ /dev/null @@ -1,127 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "log" - "net/http" - "os" - "strings" - - "github.com/akrylysov/algnhsa" - "github.com/metalogical/BigFiles/auth" - "github.com/metalogical/BigFiles/config" - "github.com/metalogical/BigFiles/server" - "github.com/sirupsen/logrus" -) - -type options struct { - service ServiceOptions - enableDebug bool -} - -type ServiceOptions struct { - ConfigFile string - RemoveCfg bool -} - -// Validate checks if the ServiceOptions are valid. -// It returns an error if the config file is missing. -func (o *ServiceOptions) Validate() error { - if o.ConfigFile == "" { - return fmt.Errorf("missing config-file") - } - - return nil -} - -// AddFlags adds flags for ServiceOptions to the provided FlagSet. -func (o *ServiceOptions) AddFlags(fs *flag.FlagSet) { - fs.StringVar(&o.ConfigFile, "config-file", "", "Path to config file.") - fs.BoolVar(&o.RemoveCfg, "rm-cfg", false, "whether remove the cfg file after initialized .") -} - -// Validate validates the options and returns an error if any validation fails. -func (o *options) Validate() error { - return o.service.Validate() -} - -func gatherOptions(fs *flag.FlagSet, args ...string) (options, error) { - var o options - o.service.AddFlags(fs) - - fs.BoolVar( - &o.enableDebug, "enable_debug", false, "whether to enable debug model.", - ) - - err := fs.Parse(args) - return o, err -} - -func main() { - o, err := gatherOptions( - flag.NewFlagSet(os.Args[0], flag.ExitOnError), - os.Args[1:]..., - ) - if err != nil { - logrus.Errorf("new options failed, err:%s", err.Error()) - - return - } - - if err := o.Validate(); err != nil { - logrus.Errorf("Invalid options, err:%s", err.Error()) - - return - } - - if o.enableDebug { - logrus.SetLevel(logrus.DebugLevel) - logrus.Debug("debug enable.") - } - - //cfg - cfg := new(config.Config) - - if err := config.LoadConfig(o.service.ConfigFile, cfg, o.service.RemoveCfg); err != nil { - logrus.Errorf("load config, err:%s", err.Error()) - - return - } - - if err := auth.Init(cfg); err != nil { - logrus.Errorf("load gitee config, err:%s", err.Error()) - - return - } - - bucket := cfg.LfsBucket - if bucket == "" { - bucket = os.Getenv("LFS_BUCKET") - if bucket == "" { - logrus.Errorf("LFS_BUCKET must be set") - } - } - - s, err := server.New(server.Options{ - Prefix: cfg.Prefix, - Bucket: bucket, - Endpoint: cfg.AwsRegion, - AccessKeyID: cfg.AwsAccessKeyId, - S3Accelerate: true, - IsAuthorized: auth.GiteeAuth(), - SecretAccessKey: cfg.AwsSecretAccessKey, - }) - if err != nil { - log.Fatalln(err) - } - - if strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda_") { - algnhsa.ListenAndServe(s, nil) - } else { - log.Println("serving on http://127.0.0.1:5000 ...") - if err := http.ListenAndServe("127.0.0.1:5000", s); err != nil { - log.Fatalln(err) - } - } -} diff --git a/batch/types.go b/batch/types.go index 1962b2c..9d97f6e 100644 --- a/batch/types.go +++ b/batch/types.go @@ -14,6 +14,11 @@ type Request struct { } `json:"objects"` } +type SuccessResponse struct { + Message string `json:"message"` + Data interface{} `json:"data,omitempty"` +} + type ErrorResponse struct { Message string `json:"message"` DocURL string `json:"documentation_url,omitempty"` diff --git a/main.go b/main.go index cce8ba3..24328bc 100644 --- a/main.go +++ b/main.go @@ -119,8 +119,8 @@ func main() { if strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda_") { algnhsa.ListenAndServe(s, nil) } else { - log.Println("serving on http://127.0.0.1:5000 ...") - if err := http.ListenAndServe("127.0.0.1:5000", s); err != nil { + log.Println("serving on http://0.0.0.0:5000 ...") + if err := http.ListenAndServe("0.0.0.0:5000", s); err != nil { log.Fatalln(err) } } diff --git a/server/server.go b/server/server.go index aac191a..4e0f1f9 100644 --- a/server/server.go +++ b/server/server.go @@ -95,29 +95,13 @@ func New(o Options) (http.Handler, error) { } r := chi.NewRouter() - r.Post("/{owner}/{repo}/objects/batch", s.handleBatch) + r.Get("/", s.healthCheck) + r.Post("/{owner}/{repo}/objects/batch", s.handleBatch) return r, nil } -type SuccessResponse struct { - Message string `json:"message"` - Data interface{} `json:"data,omitempty"` // 可选数据字段 -} - -func (s *server) healthCheck(w http.ResponseWriter, r *http.Request) { - response := SuccessResponse{ - Message: "Success", - Data: "healthCheck success", // 替换为实际的数据 - } - - // 设置响应头 - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) // 200 - must(json.NewEncoder(w).Encode(response)) -} - type server struct { client *minio.Client bucket string @@ -155,6 +139,7 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) { userInRepo.Username = username userInRepo.Password = password err = s.isAuthorized(userInRepo) + // TODO: 若仓库无lfs服务权限,不能返回401,否则会继续提示输入用户名密码。返回403 if err != nil { err = fmt.Errorf("unauthorized: %w", err) } @@ -252,6 +237,17 @@ func (s *server) handleBatch(w http.ResponseWriter, r *http.Request) { must(json.NewEncoder(w).Encode(resp)) } +func (s *server) healthCheck(w http.ResponseWriter, r *http.Request) { + response := batch.SuccessResponse{ + Message: "Success", + Data: "healthCheck success", + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + must(json.NewEncoder(w).Encode(response)) +} + // -- func must(err error) { From 4898f4842cb7642eef967e47bdac13083ea3d73c Mon Sep 17 00:00:00 2001 From: wj00037 <1292876134@qq.com> Date: Wed, 16 Oct 2024 18:22:26 +0800 Subject: [PATCH 4/4] add timeout --- go.mod | 6 ++---- go.sum | 4 ---- main.go | 20 +++++++++++--------- 3 files changed, 13 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index dd76018..a157859 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/metalogical/BigFiles go 1.21 -toolchain go1.23.1 +toolchain go1.21.4 require ( github.com/go-chi/chi v4.1.2+incompatible @@ -10,10 +10,7 @@ require ( sigs.k8s.io/yaml v1.4.0 ) -require github.com/aws/aws-lambda-go v1.43.0 // indirect - require ( - github.com/akrylysov/algnhsa v1.1.0 github.com/dustin/go-humanize v1.0.1 // indirect github.com/go-ini/ini v1.67.0 // indirect github.com/goccy/go-json v0.10.3 // indirect @@ -27,4 +24,5 @@ require ( golang.org/x/net v0.30.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/text v0.19.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 82c9f83..ce38668 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,3 @@ -github.com/akrylysov/algnhsa v1.1.0 h1:G0SoP16tMRyiism7VNc3JFA0wq/cVgEkp/ExMVnc6PQ= -github.com/akrylysov/algnhsa v1.1.0/go.mod h1:+bOweRs/WBu5awl+ifCoSYAuKVPAmoTk8XOMrZ1xwiw= -github.com/aws/aws-lambda-go v1.43.0 h1:Tdu7SnMB5bD+CbdnSq1Dg4sM68vEuGIDcQFZ+IjUfx0= -github.com/aws/aws-lambda-go v1.43.0/go.mod h1:dpMpZgvWx5vuQJfBt0zqBha60q7Dd7RfgJv23DymV8A= 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= diff --git a/main.go b/main.go index 24328bc..ded11cd 100644 --- a/main.go +++ b/main.go @@ -6,9 +6,8 @@ import ( "log" "net/http" "os" - "strings" + "time" - "github.com/akrylysov/algnhsa" "github.com/metalogical/BigFiles/auth" "github.com/metalogical/BigFiles/config" "github.com/metalogical/BigFiles/server" @@ -112,16 +111,19 @@ func main() { IsAuthorized: auth.GiteeAuth(), SecretAccessKey: cfg.AwsSecretAccessKey, }) + srv := &http.Server{ + Addr: "0.0.0.0:5000", + Handler: s, + ReadTimeout: 10 * time.Second, + WriteTimeout: 10 * time.Second, + IdleTimeout: 30 * time.Second, + } if err != nil { log.Fatalln(err) } - if strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda_") { - algnhsa.ListenAndServe(s, nil) - } else { - log.Println("serving on http://0.0.0.0:5000 ...") - if err := http.ListenAndServe("0.0.0.0:5000", s); err != nil { - log.Fatalln(err) - } + log.Println("serving on http://0.0.0.0:5000 ...") + if err := srv.ListenAndServe(); err != nil { + log.Fatalln(err) } }