Skip to content

Commit 71d55ca

Browse files
authored
refactor!: use listen address instead of port, change default (#49)
1 parent 2c72cd0 commit 71d55ca

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

Dockerfile

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,4 @@ COPY *.go ./
1010

1111
RUN go build -o /someguy
1212

13-
EXPOSE 8080
14-
15-
CMD [ "/someguy", "start", "--port", "8080" ]
13+
CMD [ "/someguy", "start" ]

docs/environment-variables.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
`someguy` ships with some implicit defaults that can be adjusted via env variables below.
44

55
- [Configuration](#configuration)
6-
- [`SOMEGUY_PORT`](#someguy_port)
6+
- [`SOMEGUY_LISTEN_ADDRESS`](#someguy_listen_address)
77
- [`SOMEGUY_ACCELERATED_DHT`](#someguy_accelerated_dht)
88
- [`SOMEGUY_PROVIDER_ENDPOINTS`](#someguy_provider_endpoints)
99
- [`SOMEGUY_PEER_ENDPOINTS`](#someguy_peer_endpoints)
@@ -16,11 +16,11 @@
1616

1717
## Configuration
1818

19-
### `SOMEGUY_PORT`
19+
### `SOMEGUY_LISTEN_ADDRESS`
2020

21-
The port to listen on to.
21+
The address to listen on.
2222

23-
Default: `8080`
23+
Default: `127.0.0.1:8190`
2424

2525
### `SOMEGUY_ACCELERATED_DHT`
2626

main.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ func main() {
2222
{
2323
Name: "start",
2424
Flags: []cli.Flag{
25-
&cli.IntFlag{
26-
Name: "port",
27-
Value: 8080,
28-
EnvVars: []string{"SOMEGUY_PORT"},
29-
Usage: "port to serve requests on",
25+
&cli.StringFlag{
26+
Name: "listen-address",
27+
Value: "127.0.0.1:8190",
28+
EnvVars: []string{"SOMEGUY_LISTEN_ADDRESS"},
29+
Usage: "listen address",
3030
},
3131
&cli.BoolFlag{
3232
Name: "accelerated-dht",
@@ -54,7 +54,7 @@ func main() {
5454
},
5555
},
5656
Action: func(ctx *cli.Context) error {
57-
return start(ctx.Context, ctx.Int("port"), ctx.Bool("accelerated-dht"), ctx.StringSlice("provider-endpoints"), ctx.StringSlice("peer-endpoints"), ctx.StringSlice("ipns-endpoints"))
57+
return start(ctx.Context, ctx.String("listen-address"), ctx.Bool("accelerated-dht"), ctx.StringSlice("provider-endpoints"), ctx.StringSlice("peer-endpoints"), ctx.StringSlice("ipns-endpoints"))
5858
},
5959
},
6060
{

server.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package main
33
import (
44
"context"
55
"log"
6+
"net"
67
"net/http"
7-
"strconv"
88

99
"github.com/CAFxX/httpcompression"
1010
"github.com/felixge/httpsnoop"
@@ -32,7 +32,7 @@ func withRequestLogger(next http.Handler) http.Handler {
3232
})
3333
}
3434

35-
func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error {
35+
func start(ctx context.Context, listenAddress string, runAcceleratedDHTClient bool, contentEndpoints, peerEndpoints, ipnsEndpoints []string) error {
3636
h, err := newHost(runAcceleratedDHTClient)
3737
if err != nil {
3838
return err
@@ -68,8 +68,13 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentE
6868
return err
6969
}
7070

71-
log.Printf("Listening on http://0.0.0.0:%d", port)
72-
log.Printf("Delegated Routing API on http://127.0.0.1:%d/routing/v1", port)
71+
_, port, err := net.SplitHostPort(listenAddress)
72+
if err != nil {
73+
return err
74+
}
75+
76+
log.Printf("Listening on %s", listenAddress)
77+
log.Printf("Delegated Routing API on http://127.0.0.1:%s/routing/v1", port)
7378

7479
mdlw := middleware.New(middleware.Config{
7580
Recorder: metrics.NewRecorder(metrics.Config{Prefix: "someguy"}),
@@ -103,7 +108,7 @@ func start(ctx context.Context, port int, runAcceleratedDHTClient bool, contentE
103108

104109
http.Handle("/debug/metrics/prometheus", promhttp.Handler())
105110
http.Handle("/", handler)
106-
server := &http.Server{Addr: ":" + strconv.Itoa(port), Handler: nil}
111+
server := &http.Server{Addr: listenAddress, Handler: nil}
107112
return server.ListenAndServe()
108113
}
109114

0 commit comments

Comments
 (0)