From 6e1995364b0ccd0c9e8496b302eb17b98784dcbe Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 13 Jun 2021 22:59:50 +0800 Subject: [PATCH 1/6] [feat] first version --- .gitignore | 1 + cmd/svc/main.go | 148 +++++++++++++++++++++++++++++++++++++++++++ config.yml.sample | 4 ++ go.mod | 32 ++++++++++ go.sum | 97 ++++++++++++++++++++++++++++ pkg/config/config.go | 92 +++++++++++++++++++++++++++ pkg/logger/logger.go | 24 +++++++ pkg/server/server.go | 97 ++++++++++++++++++++++++++++ pkg/set/set.go | 30 +++++++++ pkg/tools/tools.go | 69 ++++++++++++++++++++ 10 files changed, 594 insertions(+) create mode 100644 cmd/svc/main.go create mode 100644 config.yml.sample create mode 100644 go.mod create mode 100644 go.sum create mode 100644 pkg/config/config.go create mode 100644 pkg/logger/logger.go create mode 100644 pkg/server/server.go create mode 100644 pkg/set/set.go create mode 100644 pkg/tools/tools.go diff --git a/.gitignore b/.gitignore index f4d432a..6c8287b 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,4 @@ # Dependency directories (remove the comment below to include it) # vendor/ +config.yml diff --git a/cmd/svc/main.go b/cmd/svc/main.go new file mode 100644 index 0000000..52f4a55 --- /dev/null +++ b/cmd/svc/main.go @@ -0,0 +1,148 @@ +package main + +import ( + "context" + "deploy/pkg/config" + "deploy/pkg/logger" + "deploy/pkg/server" + "deploy/pkg/tools" + "flag" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/sirupsen/logrus" + "gopkg.in/fsnotify.v1" +) + +var cfgPath string = "./config.yml" + +func main() { + + flag.StringVar(&cfgPath, "c", "./config.yml", "config yaml path") + flag.Parse() + + if !tools.CheckExists(cfgPath, false) { + log.Fatal(fmt.Errorf("config file not exists")) + } + + // lock for service running + // buffer count 5 + reloadChan := make(chan struct{}, 5) + // lock for main lock + lock := make(chan os.Signal, 1) + signal.Notify(lock, syscall.SIGTERM, syscall.SIGINT, syscall.SIGKILL) + + watcher, err := fsnotify.NewWatcher() + if err != nil { + log.Fatal(err) + } + defer watcher.Close() + + go func() { + for { + select { + case event, ok := <-watcher.Events: + if !ok { + // channel closed + return + } + if event.Op&fsnotify.Write == fsnotify.Write { + // send reload channel + reloadChan <- struct{}{} + } + case err, ok := <-watcher.Errors: + if !ok { + // channel closed + return + } + log.Fatal(err) + } + } + }() + + if err := watcher.Add(cfgPath); err != nil { + log.Fatal(err) + } + + go runService(reloadChan) + + <-lock + + close(reloadChan) +} + +func runService(reload <-chan struct{}) { + lastReload := time.Now() + + log := logger.NewLogger(nil) + + var svc *http.Server + + // infinity loop + for { + if svc == nil { + svc = startServer(log) + } + + select { + case _, ok := <-reload: + if !ok { + // channel closed exit program + if err := stopServer(svc); err != nil { + log.Fatal(err) + return + } + } + if time.Since(lastReload).Seconds() < float64(2*time.Second) { + break + } + + lastReload = time.Now() + if err := stopServer(svc); err != nil { + log.Fatal(err) + } + } + } +} + +func startServer(log *logrus.Logger) *http.Server { + cfg, err := config.Load(cfgPath) + if err != nil { + log.Fatal(err) + } + + engine := server.NewServer(log) + // set routes + if err := engine.SetRoutes(cfg.Listens); err != nil { + log.Fatal(err) + } + + svc := &http.Server{ + Addr: fmt.Sprintf(":%d", cfg.Server.Port), + Handler: engine.Engine, + } + + go func() { + if err := svc.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Fatal(err) + } + }() + + return svc +} + +func stopServer(svc *http.Server) error { + ctx, cancel := context.WithTimeout(context.Background(), time.Second*1) + defer cancel() + + if err := svc.Shutdown(ctx); err != nil { + return err + } + + return nil +} diff --git a/config.yml.sample b/config.yml.sample new file mode 100644 index 0000000..cedbb4d --- /dev/null +++ b/config.yml.sample @@ -0,0 +1,4 @@ +server: + port: 10230 + +listens: [] diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..e221af5 --- /dev/null +++ b/go.mod @@ -0,0 +1,32 @@ +module deploy + +go 1.17 + +require ( + github.com/gin-gonic/gin v1.7.2 + github.com/otakukaze/config-loader v1.0.2 + github.com/pkg/errors v0.9.1 +) + +require ( + github.com/BurntSushi/toml v0.3.1 // indirect + github.com/gin-contrib/sse v0.1.0 // indirect + github.com/go-playground/locales v0.13.0 // indirect + github.com/go-playground/universal-translator v0.17.0 // indirect + github.com/go-playground/validator/v10 v10.6.1 // indirect + github.com/golang/protobuf v1.5.2 // indirect + github.com/json-iterator/go v1.1.11 // indirect + github.com/leodido/go-urn v1.2.1 // indirect + github.com/mattn/go-isatty v0.0.13 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.1 // indirect + github.com/otakukaze/envconfig v1.0.4 // indirect + github.com/sirupsen/logrus v1.8.1 // indirect + github.com/ugorji/go/codec v1.2.6 // indirect + golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect + golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect + golang.org/x/text v0.3.6 // indirect + google.golang.org/protobuf v1.26.0 // indirect + gopkg.in/fsnotify.v1 v1.4.7 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..338c914 --- /dev/null +++ b/go.sum @@ -0,0 +1,97 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +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/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= +github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= +github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= +github.com/gin-gonic/gin v1.7.2/go.mod h1:jD2toBW3GZUr5UMcdrwQA10I7RuaFOl/SGeDjXkfUtY= +github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= +github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= +github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8= +github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no= +github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA= +github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4= +github.com/go-playground/validator/v10 v10.6.1 h1:W6TRDXt4WcWp4c4nf/G+6BkGdhiIo0k417gfr+V6u4I= +github.com/go-playground/validator/v10 v10.6.1/go.mod h1:xm76BBt941f7yWdGnI2DVPFFg1UK3YY04qifoXU3lOk= +github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= +github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ= +github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII= +github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= +github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA= +github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +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 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/otakukaze/config-loader v1.0.2 h1:gmLd58HBNMCzhgsgoiGY8KYZSdenGNQgiZIpoI4nfnY= +github.com/otakukaze/config-loader v1.0.2/go.mod h1:9NtjfLY8DnBBZACYbjX9TO9868mwx8rWaRrSGvIFx6E= +github.com/otakukaze/envconfig v1.0.4 h1:/rZ8xq1vFpgWzqsqUkk61doDGNv9pIXqrog/mCvSx8Y= +github.com/otakukaze/envconfig v1.0.4/go.mod h1:v2dNv5NX1Lakw3FTAkbxYURyaiOy68M8QpMTZz+ogfs= +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/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= +github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +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.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/ugorji/go v1.1.7/go.mod h1:kZn38zHttfInRq0xu/PH0az30d+z6vm202qpg1oXVMw= +github.com/ugorji/go v1.2.6 h1:tGiWC9HENWE2tqYycIqFTNorMmFRVhNwCpDOpWqnk8E= +github.com/ugorji/go v1.2.6/go.mod h1:anCg0y61KIhDlPZmnH+so+RQbysYVyDko0IMgJv0Nn0= +github.com/ugorji/go/codec v1.1.7/go.mod h1:Ax+UKWsSmolVDwsd+7N3ZtXu+yMGCf907BLYF3GoBXY= +github.com/ugorji/go/codec v1.2.6 h1:7kbGefxLoDBuYXOms4yD7223OpNMMPNPZxXk5TvFcyQ= +github.com/ugorji/go/codec v1.2.6/go.mod h1:V6TCNZ4PHqoHGFZuSG1W8nrCzzdgA2DozYxWFFpvxTw= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc= +golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 h1:faDu4veV+8pcThn4fewv6TVlNCezafGoC1gM/mxQLbQ= +golang.org/x/sys v0.0.0-20210611083646-a4fc73990273/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= +golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.26.0 h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk= +google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +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/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +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.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkg/config/config.go b/pkg/config/config.go new file mode 100644 index 0000000..3931f42 --- /dev/null +++ b/pkg/config/config.go @@ -0,0 +1,92 @@ +package config + +import ( + "deploy/pkg/set" + "deploy/pkg/tools" + "fmt" + "strings" + + errors "github.com/pkg/errors" + + confLoader "github.com/otakukaze/config-loader" +) + +type Config struct { + Server Server `yaml:"server"` + Listens []Listen `yaml:"listens"` +} + +type Server struct { + Port int `yaml:"port" env:"SERVER_PORT" default:"10230"` +} + +type Listen struct { + HTTP *HTTPListen `yaml:"http"` +} + +type HTTPListen struct { + Method string `yaml:"method"` + Path string `yaml:"path"` + Headers map[string]string `yaml:"headers"` + Script string `yaml:"script"` +} + +func Load(p ...string) (*Config, error) { + + cfg := &Config{} + + opts := &confLoader.LoadOptions{ + FromEnv: true, + } + + // check path exists + if len(p) > 0 && p[0] != "" { + if !tools.CheckExists(p[0], false) { + return nil, errors.New("config not exist") + } + + opts.ConfigFile = &confLoader.ConfigFile{ + Type: confLoader.ConfigFileTypeYAML, + Path: tools.ParsePath(p[0]), + } + } + + if err := confLoader.Load(&cfg, opts); err != nil { + return nil, err + } + + // check all listen path and script + pathSet := set.NewSet() + + for _, v := range cfg.Listens { + if v.HTTP == nil { + continue + } + + key := fmt.Sprintf("%s-%s", v.HTTP.Method, v.HTTP.Path) + + if pathSet.Has(key) { + return nil, errors.WithStack(fmt.Errorf("path is duplicate: %s\n", v.HTTP.Path)) + } + + v.HTTP.Method = strings.ToUpper(v.HTTP.Method) + + // check method support + switch v.HTTP.Method { + case "GET": + case "POST": + break + default: + return nil, errors.WithStack(fmt.Errorf("http method (%s) not support", v.HTTP.Method)) + } + + pathSet.Add(key) + + // check script exists + if !tools.CheckExists(v.HTTP.Script, false) { + return nil, errors.WithStack(fmt.Errorf("path (%s) script not exists: %s\n", v.HTTP.Path, v.HTTP.Script)) + } + } + + return cfg, nil +} diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go new file mode 100644 index 0000000..550c144 --- /dev/null +++ b/pkg/logger/logger.go @@ -0,0 +1,24 @@ +package logger + +import ( + "io" + "os" + + "github.com/sirupsen/logrus" +) + +func NewLogger(output io.Writer) *logrus.Logger { + l := logrus.New() + + l.SetFormatter(&logrus.TextFormatter{}) + + l.SetLevel(logrus.DebugLevel) + + if output != nil { + l.SetOutput(output) + } else { + l.SetOutput(os.Stdout) + } + + return l +} diff --git a/pkg/server/server.go b/pkg/server/server.go new file mode 100644 index 0000000..4d69cf2 --- /dev/null +++ b/pkg/server/server.go @@ -0,0 +1,97 @@ +package server + +import ( + "bufio" + "deploy/pkg/config" + "os/exec" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" +) + +type HTTPServer struct { + Engine *gin.Engine + Logger *logrus.Logger +} + +func NewServer(logger *logrus.Logger) *HTTPServer { + e := gin.Default() + + s := &HTTPServer{ + Engine: e, + Logger: logger, + } + + return s +} + +func (s *HTTPServer) setHandler(check map[string]string, script string) gin.HandlerFunc { + return func(c *gin.Context) { + if len(check) > 0 { + for k, v := range check { + if c.GetHeader(k) != v { + s.Logger.WithFields(logrus.Fields{ + "method": c.Request.Method, + "path": c.Request.URL.Path, + "header": k, + }).Warnf("header value not match\n") + + c.AbortWithStatus(403) + return + } + } + } + + // ctx := c.Copy() + go func() { + cmd := exec.Command(script) + + // r, w := io.Pipe() + nlog := s.Logger.WithFields(logrus.Fields{"script": script}) + reader, err := cmd.StdoutPipe() + if err != nil { + nlog.Warnf("setup pipe out fail: %+v\n", err) + } + + if err := cmd.Start(); err != nil { + nlog.Warnf("start script fail: %+v\n", err) + } + + in := bufio.NewScanner(reader) + for in.Scan() { + nlog.Debugf(in.Text()) + } + if err := in.Err(); err != nil { + nlog.Warnf("script error: %+v\n", err) + } + + }() + + c.String(200, "ok") + } +} + +func (s *HTTPServer) SetRoutes(listens []config.Listen) error { + if len(listens) == 0 { + return nil + } + + for _, v := range listens { + if v.HTTP == nil { + continue + } + + switch v.HTTP.Method { + case "POST": + s.Engine.POST(v.HTTP.Path, s.setHandler(v.HTTP.Headers, v.HTTP.Script)) + break + case "GET": + s.Engine.GET(v.HTTP.Path, s.setHandler(v.HTTP.Headers, v.HTTP.Script)) + break + default: + break + } + } + + return nil +} diff --git a/pkg/set/set.go b/pkg/set/set.go new file mode 100644 index 0000000..6cd08c8 --- /dev/null +++ b/pkg/set/set.go @@ -0,0 +1,30 @@ +package set + +type Set map[interface{}]struct{} + +func NewSet() Set { + return make(map[interface{}]struct{}) +} + +func (s Set) Add(i interface{}) { + s[i] = struct{}{} +} + +func (s Set) Has(i interface{}) bool { + _, ok := s[i] + return ok +} + +func (s Set) Size() int { + return len(s) +} + +func (s Set) Keys() []interface{} { + keys := make([]interface{}, 0) + + for k, _ := range s { + keys = append(keys, k) + } + + return keys +} diff --git a/pkg/tools/tools.go b/pkg/tools/tools.go new file mode 100644 index 0000000..ba8b77f --- /dev/null +++ b/pkg/tools/tools.go @@ -0,0 +1,69 @@ +package tools + +import ( + "os" + "path" + "runtime" + "strings" +) + +// ParsePath - parse file path to absPath +func ParsePath(dst string) string { + wd, err := os.Getwd() + if err != nil { + wd = "" + } + + if []rune(dst)[0] == '~' { + home := UserHomeDir() + if len(home) > 0 { + dst = strings.Replace(dst, "~", home, -1) + } + } + + if path.IsAbs(dst) { + dst = path.Clean(dst) + return dst + } + + str := path.Join(wd, dst) + str = path.Clean(str) + return str +} + +// UserHomeDir - get user home directory +func UserHomeDir() string { + env := "HOME" + if runtime.GOOS == "windows" { + env = "USERPROFILE" + } else if runtime.GOOS == "plan9" { + env = "home" + } + return os.Getenv(env) +} + +// CheckExists - check file exists +func CheckExists(filePath string, allowDir bool) bool { + filePath = ParsePath(filePath) + stat, err := os.Stat(filePath) + if err != nil && !os.IsExist(err) { + return false + } + if !allowDir && stat.IsDir() { + return false + } + return true +} + +// IsDir - +func IsDir(filePath string) bool { + filePath = ParsePath(filePath) + stat, err := os.Stat(filePath) + if err != nil && !os.IsExist(err) { + return false + } + if !stat.IsDir() { + return false + } + return true +} From f623c4f9c3a6035645a92d3c6d9e65633d54c3db Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 13 Jun 2021 23:03:52 +0800 Subject: [PATCH 2/6] [feat] add readme --- README.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/README.md b/README.md index 4b118dd..85fd27a 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,20 @@ # auto-deploy-service +## Config struct + +```yaml +server: + port: 10230 # server listen port +listens: + - http: + method: get # http method only support get and post + path: /test # http register path + headers: # header check map[string]string + x-validate-key: '123' + script: /tmp/run.sh # script location required execute permission + - http: + method: post + path: /test-post + headers: {} + script: /tmp/run.sh +``` From 89626645655757cd19f5d549876cbb874a2dbc76 Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 13 Jun 2021 23:08:34 +0800 Subject: [PATCH 3/6] [feat] add makefile --- .gitignore | 1 + Makefile | 10 ++++++++++ 2 files changed, 11 insertions(+) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 6c8287b..0ddd24d 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ # vendor/ config.yml +deploy diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3baf594 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +.PHONY: clean build + +BIN_NAME := deploy + +build: + go build -buildmode=pie -ldflags '-w -s' -o $(BIN_NAME) ./cmd/svc/main.go + +clean: + rm $(BIN_NAME) + go clean From 3c5c6019c3a781e686ab4bf15d291ca2adc9405e Mon Sep 17 00:00:00 2001 From: Jay Date: Sun, 13 Jun 2021 23:09:26 +0800 Subject: [PATCH 4/6] [feat] add sample config --- config.yml.sample | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/config.yml.sample b/config.yml.sample index cedbb4d..02386f6 100644 --- a/config.yml.sample +++ b/config.yml.sample @@ -1,4 +1,9 @@ server: port: 10230 -listens: [] +listens: + - http: + method: get + path: /t + headers: {} + script: /tmp/run.sh From 869354fd73d8f4c9d698bac048cc9f3b9233061e Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 14 Jun 2021 13:35:12 +0800 Subject: [PATCH 5/6] [feat] add service file --- contrib/systemd/deploy.serice | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 contrib/systemd/deploy.serice diff --git a/contrib/systemd/deploy.serice b/contrib/systemd/deploy.serice new file mode 100644 index 0000000..5b8fe64 --- /dev/null +++ b/contrib/systemd/deploy.serice @@ -0,0 +1,11 @@ +[Unit] +Description=Auto Deploy Service + +[Service] +Type=sample +WorkingDirectory=/etc/auto-deploy-service +ExecStart=/usr/local/bin/deploy +Restart=always + +[Install] +WantedBy=multi-user.target From 169311f265a6ba5345559aef8492b34392ea57d8 Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 14 Jun 2021 13:47:07 +0800 Subject: [PATCH 6/6] update mod --- go.mod | 5 +++-- go.sum | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index e221af5..051b937 100644 --- a/go.mod +++ b/go.mod @@ -6,10 +6,13 @@ require ( github.com/gin-gonic/gin v1.7.2 github.com/otakukaze/config-loader v1.0.2 github.com/pkg/errors v0.9.1 + github.com/sirupsen/logrus v1.8.1 + gopkg.in/fsnotify.v1 v1.4.7 ) require ( github.com/BurntSushi/toml v0.3.1 // indirect + github.com/fsnotify/fsnotify v1.4.9 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.13.0 // indirect github.com/go-playground/universal-translator v0.17.0 // indirect @@ -21,12 +24,10 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.1 // indirect github.com/otakukaze/envconfig v1.0.4 // indirect - github.com/sirupsen/logrus v1.8.1 // indirect github.com/ugorji/go/codec v1.2.6 // indirect golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a // indirect golang.org/x/sys v0.0.0-20210611083646-a4fc73990273 // indirect golang.org/x/text v0.3.6 // indirect google.golang.org/protobuf v1.26.0 // indirect - gopkg.in/fsnotify.v1 v1.4.7 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect ) diff --git a/go.sum b/go.sum index 338c914..f7d8b09 100644 --- a/go.sum +++ b/go.sum @@ -3,6 +3,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03 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/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= +github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.7.2 h1:Tg03T9yM2xa8j6I3Z3oqLaQRSmKvxPd6g/2HJ6zICFA= @@ -68,6 +70,7 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=