Skip to content

Commit

Permalink
Restructure flags, and more pipelien fun
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsager committed Nov 25, 2023
1 parent 7b5fe5b commit f991f47
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/publish-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
jobs:
publish-docker-image:
runs-on: ubuntu-latest
permissions:
packages: write

env:
IMAGE_NAME: ghcr.io/thorsager/surl

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-on-branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ on:
jobs:
publish-docker-image:
runs-on: ubuntu-latest
permissions:
packages: write

env:
IMAGE_NAME: ghcr.io/thorsager/surl

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish-on-tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ on:
jobs:
publish-docker-image:
runs-on: ubuntu-latest
permissions:
packages: write

env:
IMAGE_NAME: ghcr.io/thorsager/surl

Expand Down
69 changes: 40 additions & 29 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,32 @@ import (
"github.com/spf13/pflag"
)

var version = "*unset*"
var (
version = "*unset*"
versionFlag bool
dumpRequestFlag bool
dumpBodyFlag bool
statusCodeFlag uint
responseHeadersFlag []string
responseBodyFlag string
exitAfterFlag uint
certFileFlag string
keyFileFlag string

responseCount uint = 0
)

func main() {
var responseCount uint = 0
var responseHeaders []string

versionFlag := pflag.Bool("version", false, "show version")
dumpRequest := pflag.Bool("dump", false, "dump client request")
dumpBody := pflag.Bool("dump-body", false, "dump client request body")
statusCode := pflag.UintP("status", "s", 200, "return status code")
pflag.StringArrayVarP(&responseHeaders, "header", "H", []string{}, "HTTP response header")
responseBody := pflag.StringP("data", "d", "", "add HTTP response body")
exitAfter := pflag.UintP("count", "c", 0, "exit after number of requests (0 keep running)")
certFile := pflag.String("cert", "", "TLS certificate file")
keyFile := pflag.String("key", "", "TLS certificate key-file")

pflag.BoolVar(&versionFlag, "version", false, "show version")
pflag.BoolVar(&dumpRequestFlag, "dump", false, "dump client request")
pflag.BoolVar(&dumpBodyFlag, "dump-body", false, "dump client request body")
pflag.UintVarP(&statusCodeFlag, "status", "s", 200, "return status code")
pflag.StringArrayVarP(&responseHeadersFlag, "header", "H", []string{}, "HTTP response header")
pflag.StringVarP(&responseBodyFlag, "data", "d", "", "add HTTP response body")
pflag.UintVarP(&exitAfterFlag, "count", "c", 0, "exit after number of requests (0 keep running)")
pflag.StringVar(&certFileFlag, "cert", "", "TLS certificate file")
pflag.StringVar(&keyFileFlag, "key", "", "TLS certificate key-file")

pflag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "Usage: %s [options...] <addr>\n%s", filepath.Base(os.Args[0]),
Expand All @@ -44,7 +55,7 @@ func main() {

pflag.Parse()

if *versionFlag {
if versionFlag {
fmt.Printf("surl %s\n", version)
os.Exit(0)
}
Expand All @@ -66,17 +77,17 @@ func main() {

log.Printf("request: %s %s %s", r.RemoteAddr, r.Method, r.URL)

if *dumpRequest || *dumpBody {
dump, err := httputil.DumpRequest(r, *dumpBody)
if dumpRequestFlag || dumpBodyFlag {
dump, err := httputil.DumpRequest(r, dumpBodyFlag)
if err != nil {
log.Printf("error: unable to dump client request: %s", err)
return
}
log.Printf("\n--\n%q\n--\n", dump)
}

if len(responseHeaders) != 0 {
for _, hdr := range responseHeaders {
if len(responseHeadersFlag) != 0 {
for _, hdr := range responseHeadersFlag {
if err := addRawHeader(w.Header(), hdr); err != nil {
log.Printf("error: unable to add response header: %s", err)
}
Expand All @@ -87,12 +98,12 @@ func main() {
w.Header().Add("Server", description)
}

w.WriteHeader(int(*statusCode))
w.WriteHeader(int(statusCodeFlag))

if *responseBody != "" {
if strings.HasPrefix(*responseBody, "@") {
if responseBodyFlag != "" {
if strings.HasPrefix(responseBodyFlag, "@") {
// response is filename
filename := trimFirst(*responseBody)
filename := trimFirst(responseBodyFlag)
file, err := os.Open(filename)
if err != nil {
log.Printf("error: unable to open file: '%s'", filename)
Expand All @@ -103,27 +114,27 @@ func main() {
log.Printf("error: unable to write response body: %s", err)
}
} else {
if _, err := w.Write([]byte(*responseBody)); err != nil {
if _, err := w.Write([]byte(responseBodyFlag)); err != nil {
log.Printf("error: unable to write response body: %s", err)
}
}
}
if *exitAfter != 0 && *exitAfter == responseCount {
if exitAfterFlag != 0 && exitAfterFlag == responseCount {
log.Printf("response count of %d reached, shutting down", responseCount)
sigChan <- os.Interrupt
}

})

go func() {
if *certFile != "" && *keyFile != "" {
log.Printf("starting %s on %s %s", description, addr, desc(*exitAfter))
if err := srv.ListenAndServeTLS(*certFile, *keyFile); !errors.Is(err, http.ErrServerClosed) {
if certFileFlag != "" && keyFileFlag != "" {
log.Printf("starting %s on %s %s", description, addr, desc(exitAfterFlag))
if err := srv.ListenAndServeTLS(certFileFlag, keyFileFlag); !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("startup error: %v", err)
}
return
}
log.Printf("starting %s on %s %s", description, addr, desc(*exitAfter))
log.Printf("starting %s on %s %s", description, addr, desc(exitAfterFlag))
if err := srv.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
log.Fatalf("startup error: %v", err)
}
Expand All @@ -136,7 +147,7 @@ func main() {
shutdownCtx, shutdownRelease := context.WithTimeout(context.Background(), 10*time.Second)
defer shutdownRelease()

if *exitAfter != responseCount {
if exitAfterFlag != responseCount {
log.Printf("shutting down after %d responses", responseCount)
}
err = srv.Shutdown(shutdownCtx)
Expand Down

0 comments on commit f991f47

Please sign in to comment.