[feat] first version
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user