Skip to content

Commit

Permalink
feat: cleaner implementation of port in config
Browse files Browse the repository at this point in the history
  • Loading branch information
DblK committed May 2, 2023
1 parent c5e945a commit 3b54ce8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
3 changes: 1 addition & 2 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ host: tinshop.example.com
protocol: https

# Port [optional]
# This only affect the url to download games. The web server still run on port 3000.
# If you change the port, you will need to set up a reverse proxy
# This affect the url to download games & the web server will run on that port (default: 3000).
# port: 3000

# Shop name [optional]
Expand Down
2 changes: 1 addition & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func ComputeDefaultValues(config repository.Config) repository.Config {
// Retrieve current IP
host, _ := os.Hostname()
addrs, _ := net.LookupIP(host)
var myIP = ""
var myIP = "0.0.0.0"
for _, addr := range addrs {
if ipv4 := addr.To4(); ipv4 != nil {
if myIP == "" {
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"os"
"os/signal"
"strconv"
"time"

"github.com/DblK/tinshop/api"
Expand Down Expand Up @@ -83,9 +84,14 @@ func createShop() TinShop {
r.Use(shop.CORSMiddleware)
http.Handle("/", r)

var port = 3000
if shop.Shop.Config.Port() != 0 {
port = shop.Shop.Config.Port()
}

srv := &http.Server{
Handler: r,
Addr: "0.0.0.0:3000",
Addr: "0.0.0.0:" + strconv.Itoa(port),

// Good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: 0, // Installing large game can take a lot of time
Expand Down

0 comments on commit 3b54ce8

Please sign in to comment.