Skip to content

Commit 8051152

Browse files
committed
allow port to be configured by argument
1 parent effa1c3 commit 8051152

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

multiple-servers/IMPLEMENTATION.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ Copied over from server-database, with file split up:
5151

5252
This has the same setup steps as server-database, so those can be copied over.
5353

54+
# Ports
55+
56+
`cmd` files should allow ports to be configred using `--port`.
57+
5458
## Nginx
5559

5660
Switch static server over to 8082.

multiple-servers/api/api.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,6 @@ func Run(config Config) error {
6868
w.Write(response)
6969
})
7070

71+
log.Printf("port: %d\n", config.Port)
7172
return http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
7273
}

multiple-servers/cmd/api-server/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"flag"
45
"log"
56
"os"
67
"servers/api"
@@ -12,8 +13,11 @@ func main() {
1213
log.Fatalln("DATABASE_URL not set")
1314
}
1415

16+
port := flag.Int("port", 8081, "port the server will listen on")
17+
flag.Parse()
18+
1519
log.Fatal(api.Run(api.Config{
1620
DatabaseURL: os.Getenv("DATABASE_URL"),
17-
Port: 8081,
21+
Port: *port,
1822
}))
1923
}

multiple-servers/cmd/static-server/main.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
func main() {
1111
path := flag.String("path", ".", "path to static files")
12+
port := flag.Int("port", 8082, "port the server will listen on")
1213
flag.Parse()
1314

1415
absPath, err := filepath.Abs(*path)
@@ -18,6 +19,6 @@ func main() {
1819

1920
log.Fatal(static.Run(static.Config{
2021
Dir: absPath,
21-
Port: 8082,
22+
Port: *port,
2223
}))
2324
}

multiple-servers/static/static.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ func Run(config Config) error {
2323
http.ServeFile(w, r, path)
2424
})
2525

26+
log.Printf("port: %d\n", config.Port)
2627
return http.ListenAndServe(fmt.Sprintf(":%d", config.Port), nil)
2728
}

0 commit comments

Comments
 (0)