Skip to content

Commit

Permalink
config: set default bind address and port
Browse files Browse the repository at this point in the history
If BIND and/or PORT are not set, they now default to 127.0.0.1 and 8000
respectively.

Closes #21
  • Loading branch information
shoenig committed Dec 15, 2024
1 parent cb79006 commit 6ef43ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ Basically none

### Usage

Set `BIND` environment variable to configure bind address
Set `BIND` environment variable to configure bind address.

- defaults to `127.0.0.1`

Set `PORT` environment variable to configure bind port

- defaults to `8000`

Arguments are a comma-separated list of `<url-path>:<file-path>` pairs.

### Examples
Expand All @@ -24,7 +28,8 @@ BIND=0.0.0.0 PORT=8081 just-files /:/www

### Building

The `just-files` file server is written in Go. IT can be built using the normal Go toolchain steps, e.g.
The `just-files` file server is written in Go. IT can be built using the normal
Go toolchain steps, e.g.

```shell
go build
Expand Down
16 changes: 10 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"errors"
"fmt"
"log"
"net/http"
Expand All @@ -23,6 +22,13 @@ func main() {
}
}

func envOr(key, fallback string) string {
if value := os.Getenv(key); value != "" {
return value
}
return fallback
}

func run(args []string) error {
m, err := paths(args[1:])
if err != nil {
Expand All @@ -31,11 +37,9 @@ func run(args []string) error {

lockdown(m)

bind := os.Getenv("BIND")
port := os.Getenv("PORT")
if bind == "" || port == "" {
return errors.New("$BIND and $PORT must be set")
}
bind := envOr("BIND", "127.0.0.1")
port := envOr("PORT", "8000")

log.Println("bind is", bind)
log.Println("port is", port)

Expand Down
2 changes: 2 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
func TestRun(t *testing.T) {
t.Setenv("BIND", "127.0.0.1")
t.Setenv("PORT", "8787")

dir, err := os.MkdirTemp("", "")
must.NoError(t, err)

filename := filepath.Join(dir, "hi.txt")
err = os.WriteFile(filename, []byte("hello"), 0o644)
must.NoError(t, err)
Expand Down

0 comments on commit 6ef43ad

Please sign in to comment.