From 71d55cadb1d1b6abdf4699ed8f8aa7e76a0bcf88 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Thu, 7 Mar 2024 09:25:58 +0100 Subject: [PATCH] refactor!: use listen address instead of port, change default (#49) --- Dockerfile | 4 +--- docs/environment-variables.md | 8 ++++---- main.go | 12 ++++++------ server.go | 15 ++++++++++----- 4 files changed, 21 insertions(+), 18 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1d944f2..bb23baf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,6 +10,4 @@ COPY *.go ./ RUN go build -o /someguy -EXPOSE 8080 - -CMD [ "/someguy", "start", "--port", "8080" ] +CMD [ "/someguy", "start" ] diff --git a/docs/environment-variables.md b/docs/environment-variables.md index ee0f160..f4de147 100644 --- a/docs/environment-variables.md +++ b/docs/environment-variables.md @@ -3,7 +3,7 @@ `someguy` ships with some implicit defaults that can be adjusted via env variables below. - [Configuration](#configuration) - - [`SOMEGUY_PORT`](#someguy_port) + - [`SOMEGUY_LISTEN_ADDRESS`](#someguy_listen_address) - [`SOMEGUY_ACCELERATED_DHT`](#someguy_accelerated_dht) - [`SOMEGUY_PROVIDER_ENDPOINTS`](#someguy_provider_endpoints) - [`SOMEGUY_PEER_ENDPOINTS`](#someguy_peer_endpoints) @@ -16,11 +16,11 @@ ## Configuration -### `SOMEGUY_PORT` +### `SOMEGUY_LISTEN_ADDRESS` -The port to listen on to. +The address to listen on. -Default: `8080` +Default: `127.0.0.1:8190` ### `SOMEGUY_ACCELERATED_DHT` diff --git a/main.go b/main.go index 626c008..ae61974 100644 --- a/main.go +++ b/main.go @@ -22,11 +22,11 @@ func main() { { Name: "start", Flags: []cli.Flag{ - &cli.IntFlag{ - Name: "port", - Value: 8080, - EnvVars: []string{"SOMEGUY_PORT"}, - Usage: "port to serve requests on", + &cli.StringFlag{ + Name: "listen-address", + Value: "127.0.0.1:8190", + EnvVars: []string{"SOMEGUY_LISTEN_ADDRESS"}, + Usage: "listen address", }, &cli.BoolFlag{ Name: "accelerated-dht", @@ -54,7 +54,7 @@ func main() { }, }, Action: func(ctx *cli.Context) error { - return start(ctx.Context, ctx.Int("port"), ctx.Bool("accelerated-dht"), ctx.StringSlice("provider-endpoints"), ctx.StringSlice("peer-endpoints"), ctx.StringSlice("ipns-endpoints")) + return start(ctx.Context, ctx.String("listen-address"), ctx.Bool("accelerated-dht"), ctx.StringSlice("provider-endpoints"), ctx.StringSlice("peer-endpoints"), ctx.StringSlice("ipns-endpoints")) }, }, { diff --git a/server.go b/server.go index 053f4e2..08abef6 100644 --- a/server.go +++ b/server.go @@ -3,8 +3,8 @@ package main import ( "context" "log" + "net" "net/http" - "strconv" "github.com/CAFxX/httpcompression" "github.com/felixge/httpsnoop" @@ -32,7 +32,7 @@ func withRequestLogger(next http.Handler) http.Handler { }) } -func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error { +func start(ctx context.Context, listenAddress string, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error { h, err := newHost(runAcceleratedDHTClient) if err != nil { return err @@ -68,8 +68,13 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentE return err } - log.Printf("Listening on http://0.0.0.0:%d", port) - log.Printf("Delegated Routing API on http://127.0.0.1:%d/routing/v1", port) + _, port, err := net.SplitHostPort(listenAddress) + if err != nil { + return err + } + + log.Printf("Listening on %s", listenAddress) + log.Printf("Delegated Routing API on http://127.0.0.1:%s/routing/v1", port) mdlw := middleware.New(middleware.Config{ Recorder: metrics.NewRecorder(metrics.Config{Prefix: "someguy"}), @@ -103,7 +108,7 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentE http.Handle("/debug/metrics/prometheus", promhttp.Handler()) http.Handle("/", handler) - server := &http.Server{Addr: ":" + strconv.Itoa(port), Handler: nil} + server := &http.Server{Addr: listenAddress, Handler: nil} return server.ListenAndServe() }