Skip to content

Commit

Permalink
refactor!: use listen address instead of port, change default (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias authored Mar 7, 2024
1 parent 2c72cd0 commit 71d55ca
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
4 changes: 1 addition & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ COPY *.go ./

RUN go build -o /someguy

EXPOSE 8080

CMD [ "/someguy", "start", "--port", "8080" ]
CMD [ "/someguy", "start" ]
8 changes: 4 additions & 4 deletions docs/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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`

Expand Down
12 changes: 6 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"))
},
},
{
Expand Down
15 changes: 10 additions & 5 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"context"
"log"
"net"
"net/http"
"strconv"

"github.com/CAFxX/httpcompression"
"github.com/felixge/httpsnoop"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"}),
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit 71d55ca

Please sign in to comment.