From 0a8ce4c331376784ce59892c0bafcfff8e4768d7 Mon Sep 17 00:00:00 2001 From: Suphon Thanakornpakapong <8080853+suphon-t@users.noreply.github.com> Date: Sun, 21 Jan 2024 17:18:20 +0700 Subject: [PATCH] chore: remove unused code --- go.mod | 4 +- go.sum | 4 - internal/daemon/nginx.go | 278 --------------------------------------- 3 files changed, 1 insertion(+), 285 deletions(-) delete mode 100644 internal/daemon/nginx.go diff --git a/go.mod b/go.mod index ad8b754..0bb5300 100644 --- a/go.mod +++ b/go.mod @@ -7,8 +7,8 @@ require ( github.com/dchest/uniuri v1.2.0 github.com/samber/lo v1.39.0 github.com/spf13/cobra v1.8.0 - github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436 go.uber.org/zap v1.26.0 + golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 golang.org/x/sys v0.14.0 google.golang.org/grpc v1.60.1 google.golang.org/protobuf v1.32.0 @@ -101,7 +101,6 @@ require ( go.uber.org/mock v0.3.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.14.0 // indirect - golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 // indirect golang.org/x/mod v0.11.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/term v0.13.0 // indirect @@ -111,6 +110,5 @@ require ( google.golang.org/genproto/googleapis/rpc v0.0.0-20231016165738-49dd2c1f3d0b // indirect gopkg.in/square/go-jose.v2 v2.6.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - gotest.tools/v3 v3.5.1 // indirect howett.net/plist v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 9c943d8..f50119e 100644 --- a/go.sum +++ b/go.sum @@ -556,8 +556,6 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl github.com/tailscale/tscert v0.0.0-20230806124524-28a91b69a046 h1:8rUlviSVOEe7TMk7W0gIPrW8MqEzYfZHpsNWSf8s2vg= github.com/tailscale/tscert v0.0.0-20230806124524-28a91b69a046/go.mod h1:kNGUQ3VESx3VZwRwA9MSCUegIl6+saPL8Noq82ozCaU= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436 h1:i9TLbw23bUawnhimf5SghqkLrDRdpa65vw0hUqYhCB0= -github.com/tufanbarisyildirim/gonginx v0.0.0-20231222202608-ba16e88a9436/go.mod h1:4fTjBxMoWGOIVnGFSTS9GAZ0yMyiGzTdATQS0krQv18= github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0= @@ -821,8 +819,6 @@ gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= 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= -gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU= -gotest.tools/v3 v3.5.1/go.mod h1:isy3WKz7GK6uNw/sbHzfKBLvlvXwUyV06n6brMxxopU= honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/internal/daemon/nginx.go b/internal/daemon/nginx.go deleted file mode 100644 index 06d409e..0000000 --- a/internal/daemon/nginx.go +++ /dev/null @@ -1,278 +0,0 @@ -package daemon - -import ( - "bufio" - "context" - "errors" - "fmt" - "io" - "os" - "os/exec" - "path" - "strconv" - "strings" - "sync" - "syscall" - - "github.com/samber/lo" - "github.com/softnetics/dotlocal/internal" - "github.com/softnetics/dotlocal/internal/util" - "github.com/tufanbarisyildirim/gonginx" - "github.com/tufanbarisyildirim/gonginx/parser" - "go.uber.org/zap" -) - -type Nginx struct { - logger *zap.Logger - configFile string - cmd *exec.Cmd - mappings []internal.Mapping -} - -func NewNginx(logger *zap.Logger) (*Nginx, error) { - configFile, err := util.CreateTmpFile() - if err != nil { - return nil, err - } - return &Nginx{ - logger: logger, - configFile: configFile, - cmd: nil, - mappings: nil, - }, nil -} - -func (n *Nginx) Start(ctx context.Context) error { - err := n.killExistingProcess() - if err != nil { - return err - } - - n.writeConfig() - n.logger.Debug("Starting nginx") - - fmt.Printf("nginx -c %s\n", n.configFile) - cmd := exec.Command("nginx", "-c", n.configFile) - stdout, err := cmd.StdoutPipe() - cmd.Stderr = os.Stderr - if err != nil { - return err - } - var wg sync.WaitGroup - wg.Add(1) - nginxStarted := false - go func() { - func() { - defer wg.Done() - scanner := bufio.NewScanner(stdout) - for scanner.Scan() { - line := scanner.Text() - println(line) - if strings.Contains(line, "start worker processes") { - nginxStarted = true - return - } - } - }() - io.Copy(os.Stdout, stdout) - }() - - go func() { - <-ctx.Done() - cmd.Process.Signal(syscall.SIGTERM) - }() - - err = cmd.Start() - if err != nil { - return err - } - - wg.Wait() - if !nginxStarted { - err := cmd.Wait() - if err != nil { - return err - } - return errors.New("nginx failed to start") - } - n.cmd = cmd - - n.logger.Info("Ready") - - return nil -} - -func (n *Nginx) SetMappings(mappings []internal.Mapping) error { - n.mappings = mappings - n.logger.Debug("Setting mappings", zap.Any("mappings", mappings)) - err := n.writeConfig() - if err != nil { - return err - } - return n.reloadConfig() -} - -func (n *Nginx) Stop() error { - n.logger.Info("Stopping nginx") - err := n.cmd.Process.Signal(syscall.SIGTERM) - if err != nil { - return err - } - n.cmd.Wait() - return nil -} - -func (n *Nginx) writeConfig() error { - p := parser.NewStringParser(` - daemon off; - error_log /dev/stdout info; - events { - worker_connections 1024; - } - http { - } - `) - - conf, err := p.Parse() - if err != nil { - return err - } - - http := conf.FindDirectives("http")[0].GetBlock().(*gonginx.HTTP) - hosts := lo.GroupBy(n.mappings, func(m internal.Mapping) string { - return m.Host - }) - for host, hostMappings := range hosts { - directives := []gonginx.IDirective{ - &gonginx.Directive{ - Name: "listen", - Parameters: []string{"127.0.0.1"}, - }, - &gonginx.Directive{ - Name: "server_name", - Parameters: []string{host}, - }, - } - locations := lo.SliceToMap(hostMappings, func(mapping internal.Mapping) (string, []gonginx.IDirective) { - return mapping.PathPrefix, []gonginx.IDirective{ - &gonginx.Directive{ - Name: "proxy_pass", - Parameters: []string{mapping.Target}, - }, - &gonginx.Directive{ - Name: "proxy_http_version", - Parameters: []string{"1.1"}, - }, - &gonginx.Directive{ - Name: "proxy_set_header", - Parameters: []string{"Upgrade", "$http_upgrade"}, - }, - &gonginx.Directive{ - Name: "proxy_set_header", - Parameters: []string{"Connection", "\"Upgrade\""}, - }, - &gonginx.Directive{ - Name: "proxy_set_header", - Parameters: []string{"Host", "$host"}, - }, - &gonginx.Directive{ - Name: "proxy_set_header", - Parameters: []string{"X-Forwarded-For", "$remote_addr"}, - }, - } - }) - _, hasRoot := locations["/"] - if !hasRoot { - locations["/"] = []gonginx.IDirective{ - &gonginx.Directive{ - Name: "return", - Parameters: []string{"404"}, - }, - } - } - for pathPrefix, locationDirectives := range locations { - directives = append(directives, &gonginx.Directive{ - Name: "location", - Parameters: []string{pathPrefix}, - Block: &gonginx.LuaBlock{ - Directives: locationDirectives, - }, - }) - } - http.Directives = append(http.Directives, &gonginx.Directive{ - Name: "server", - Parameters: []string{}, - Block: &gonginx.LuaBlock{ - Directives: directives, - }, - }) - } - http.Directives = append(http.Directives, &gonginx.Directive{ - Name: "server", - Parameters: []string{}, - Block: &gonginx.LuaBlock{ - Directives: []gonginx.IDirective{ - &gonginx.Directive{ - Name: "listen", - Parameters: []string{"127.0.0.1", "default_server"}, - }, - &gonginx.Directive{ - Name: "return", - Parameters: []string{"404"}, - }, - }, - }, - }) - - configString := gonginx.DumpBlock(conf, gonginx.IndentedStyle) - // println(configString) - n.logger.Debug("Writing nginx config", zap.String("config", configString)) - err = os.WriteFile(n.configFile, []byte(configString), 0644) - if err != nil { - return err - } - - return nil -} - -func (n *Nginx) reloadConfig() error { - err := n.cmd.Process.Signal(syscall.SIGHUP) - if err != nil { - return err - } - n.logger.Info("Reloaded nginx config") - return nil -} - -func (n *Nginx) killExistingProcess() error { - pidBytes, err := os.ReadFile(path.Join(util.GetDotlocalPath(), "nginx.pid")) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - return nil - } - return err - } - pidString := strings.TrimSpace(strings.Split(string(pidBytes), "\n")[0]) - pid, err := strconv.Atoi(pidString) - if err != nil { - return err - } - n.logger.Info("Killing existing process", zap.Int("pid", pid)) - - process, err := os.FindProcess(pid) - if err != nil { - return err - } - _ = process.Signal(syscall.SIGTERM) - - err = os.Remove(util.GetPidPath()) - if err != nil { - return err - } - err = os.Remove(util.GetApiSocketPath()) - if err != nil { - return err - } - - return nil -}