Skip to content

Commit 5e0e91a

Browse files
committed
new: add an option to bind to a specific address
1 parent 19b4a3b commit 5e0e91a

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ the filesystem
5151
|---------------|-------------|
5252
| `YBF_DATA_DIR` | points to an alternative direcotry to store data, default is `./data/` in current directory. |
5353
| `YBF_HTTP_PORT` | TCP port to run the server, default is `8080`. |
54+
| `YBF_LISTEN_ADDR` | IP address to bind, default is `0.0.0.0`. |
5455

5556
### Installation
5657

cmd/ybfeed/main.go

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
)
1010

1111
var HTTP_PORT int
12+
var LISTEN_ADDR string
1213
var DEBUG bool
1314
var dataDir string
1415
var maxBodySize int
@@ -30,6 +31,14 @@ func main() {
3031
Usage: "TCP Port to listen",
3132
Destination: &HTTP_PORT,
3233
},
34+
&cli.StringFlag{
35+
Name: "address",
36+
Aliases: []string{"b"},
37+
Value: "0.0.0.0",
38+
EnvVars: []string{"YBF_LISTEN_ADDR"},
39+
Usage: "IP address to bind",
40+
Destination: &LISTEN_ADDR,
41+
},
3342
&cli.BoolFlag{
3443
Name: "debug",
3544
Value: false,
@@ -83,6 +92,7 @@ func run() {
8392
api.Version = version
8493
api.MaxBodySize = maxBodySize * 1024 * 1024
8594
api.HttpPort = HTTP_PORT
95+
api.ListenAddr = LISTEN_ADDR
8696

8797
api.StartServer()
8898
}

internal/handlers/handlers.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ type ApiHandler struct {
8585
MaxBodySize int
8686
Config APIConfig
8787
HttpPort int
88+
ListenAddr string
8889
WebSocketManager *feed.WebSocketManager
8990
FeedManager *feed.FeedManager
9091
}
@@ -164,7 +165,7 @@ func (api *ApiHandler) WriteConfig() error {
164165
}
165166
func (api *ApiHandler) StartServer() {
166167
r := api.GetServer()
167-
err := http.ListenAndServe(fmt.Sprintf(":%d", api.HttpPort), r)
168+
err := http.ListenAndServe(fmt.Sprintf("%s:%d", api.ListenAddr, api.HttpPort), r)
168169
if err != nil {
169170
logger.Error("Unable to start HTTP server",
170171
slog.String("error", err.Error()))
@@ -209,6 +210,7 @@ func (api *ApiHandler) GetServer() *chi.Mux {
209210
slog.String("version", api.Version),
210211
slog.String("data_dir", api.BasePath),
211212
slog.Int("port", api.HttpPort),
213+
slog.String("address", api.ListenAddr),
212214
slog.Int("max-upload-size", api.MaxBodySize))
213215

214216
return r

0 commit comments

Comments
 (0)