Skip to content

Commit

Permalink
Remove dep on main fortio package (#4)
Browse files Browse the repository at this point in the history
* Remove dep on main fortio package

* update dflag, log

* add a timeout to avoid Slowloris

* use dflag version with matching log, flags version
  • Loading branch information
ldemailly authored Feb 25, 2023
1 parent 1319f37 commit b81106f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
49 changes: 41 additions & 8 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,29 @@ package scli // import "fortio.org/scli"

import (
"flag"
"net"
"net/http"
"strings"
"time"

"fortio.org/cli"
"fortio.org/dflag/configmap"
"fortio.org/dflag/dynloglevel"
"fortio.org/dflag/endpoint"
"fortio.org/fortio/fhttp"
"fortio.org/log"
"fortio.org/version"
)

// NormalizePort parses port and returns host:port if port is in the form
// of host:port already or :port if port is only a port (doesn't contain :).
// Copied from fortio.org/fnet.NormalizePort to avoid dependency loop.
func NormalizePort(port string) string {
if strings.ContainsAny(port, ":") {
return port
}
return ":" + port
}

// ServerMain extends [cli.Main] and returns true if a config port server has been started
// caller needs to select {} after their own code is ready.
// [cli.ExitFunction] will have been called (ie program will have exited exited)
Expand All @@ -39,16 +53,35 @@ func ServerMain() bool {
log.Critf("Unable to watch config/flag changes in %v: %v", *configDir, err)
}
}
shortScliV, _, _ := version.FromBuildInfoPath("fortio.org/scli")

hasStartedServer := false
if *configPort != "" {
mux, addr := fhttp.HTTPServer("config", *configPort) // err already logged
if addr != nil {
hasStartedServer = true
setURL := "/set"
ep := endpoint.NewFlagsEndpoint(flag.CommandLine, setURL)
mux.HandleFunc("/", ep.ListFlags)
mux.HandleFunc(setURL, ep.SetFlag)
// Sort of inlining fortio.org/fhttp.HTTPServer here to avoid
// a dependency loop.
port := NormalizePort(*configPort)
m := http.NewServeMux()
s := &http.Server{
Addr: port,
Handler: m,
ReadTimeout: 3 * time.Second,
}
setURL := "/set"
ep := endpoint.NewFlagsEndpoint(flag.CommandLine, setURL)
m.HandleFunc("/", ep.ListFlags)
m.HandleFunc(setURL, ep.SetFlag)
ln, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("Unable to serve config on %s: %v", s.Addr, err)
}
log.Infof("Fortio scli %v dflag config server listening on %s", shortScliV, ln.Addr())
go func() {
err := s.Serve(ln)
if err != nil {
log.Fatalf("Unable to serve config on %s: %v", s.Addr, err)
}
}()
hasStartedServer = true
}
log.Infof("Starting %s %s", cli.ProgramName, cli.LongVersion)
return hasStartedServer
Expand Down
13 changes: 5 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@ go 1.18

require (
fortio.org/cli v1.1.0
fortio.org/dflag v1.4.2
fortio.org/fortio v1.51.0
fortio.org/log v1.2.2
fortio.org/dflag v1.5.2
fortio.org/log v1.3.0
fortio.org/version v1.0.2
)

require (
fortio.org/version v1.0.2 // indirect
fortio.org/sets v1.0.2 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/google/uuid v1.3.0 // indirect
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
)
24 changes: 9 additions & 15 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,24 +1,18 @@
fortio.org/assert v1.1.3 h1:zXm8xiNiKvq2xG/YQ3sONAg3287XUuklKIDdjyD9pyg=
fortio.org/assert v1.1.4 h1:Za1RaG+OjsTMpQS3J3UCvTF6wc4+IOHCz+jAOU37Y4o=
fortio.org/cli v1.1.0 h1:ATIxi7DgA7WAexUCF8p5a0qlGYk48ZgkwSEDrvwXeN4=
fortio.org/cli v1.1.0/go.mod h1:O3nVImKwJSvHKbMYWkqMbEagAXCS1xvSv1YbHwkKJjY=
fortio.org/dflag v1.4.2 h1:kkPNgmoGViSqh16Muf3BGt+qxPgsx+yfFGB/bpmiGqM=
fortio.org/dflag v1.4.2/go.mod h1:pTEF7UEj6sHP9rj9gZG2GyhAGrrPJE4c6zOO7zB2yyI=
fortio.org/fortio v1.51.0 h1:NNmWObMqvhNppfDnaXtQm4We8nU/vWkgb/4WcEmFkX8=
fortio.org/fortio v1.51.0/go.mod h1:xbh8Taib2MudgXt5MYu2upVaDvlIq4w97CDXz/4td3M=
fortio.org/log v1.2.2 h1:vs42JjNwiqbMbacittZjJE9+oi72Za6aekML9gKmILg=
fortio.org/log v1.2.2/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
fortio.org/dflag v1.5.2 h1:F9XVRj4Qr2IbJP7BMj7XZc9wB0Q/RZ61Ool+4YPVad8=
fortio.org/dflag v1.5.2/go.mod h1:ppb/A8u+KKg+qUUYZNYuvRnXuVb8IsdHb/XGzsmjkN8=
fortio.org/log v1.3.0 h1:bESPvuQGKejw7rrx41Sg3GoF+tsrB7oC08PxBs5/AM0=
fortio.org/log v1.3.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU=
fortio.org/sets v1.0.2 h1:gSWZFg9rgzl1zJfI/93lDJKBFw8WZ3Uxe3oQ5uDM4T4=
fortio.org/sets v1.0.2/go.mod h1:xVjulHr0FhlmReSymI+AhDtQ4FgjiazQ3JmuNpYFMs8=
fortio.org/version v1.0.2 h1:8NwxdX58aoeKx7T5xAPO0xlUu1Hpk42nRz5s6e6eKZ0=
fortio.org/version v1.0.2/go.mod h1:2JQp9Ax+tm6QKiGuzR5nJY63kFeANcgrZ0osoQFDVm0=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb h1:PaBZQdo+iSDyHT053FjUCgZQ/9uqVwPOcl7KSWhKn6w=
golang.org/x/exp v0.0.0-20230213192124-5e25df0256eb/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2 h1:Jvc7gsqn21cJHCmAWx0LiimpP18LZmUxkT5Mp7EZ1mI=
golang.org/x/exp v0.0.0-20230224173230-c95f2b4c22f2/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=

0 comments on commit b81106f

Please sign in to comment.