about summary refs log tree commit diff
path: root/src/main.go
diff options
context:
space:
mode:
authorEmile <git@emile.space>2024-08-16 21:42:03 +0200
committerEmile <git@emile.space>2024-08-16 21:42:03 +0200
commitc52d6f99bdbb4423fe7ad1133c5c1520f097114d (patch)
treedc8e113f51f1d9c792f10713fbf755e341b31121 /src/main.go
parent1a57267a17c2fc17fb6e104846fabc3e363c326c (diff)
host and port flags
Diffstat (limited to 'src/main.go')
-rw-r--r--src/main.go18
1 files changed, 16 insertions, 2 deletions
diff --git a/src/main.go b/src/main.go
index 27c066c..6442aa8 100644
--- a/src/main.go
+++ b/src/main.go
@@ -1,6 +1,8 @@
 package main
 
 import (
+	"flag"
+	"fmt"
 	"log"
 	"net/http"
 	"os"
@@ -8,6 +10,9 @@ import (
 	"github.com/gorilla/mux"
 )
 
+var host string
+var port int
+
 const database_file string = "main.db"
 const salt = "oogha3AiH7taimohreeH8Lexoonea5zi"
 
@@ -15,7 +20,16 @@ var (
 	globalState *State
 )
 
+func initFlags() {
+	flag.StringVar(&host, "host", "127.0.0.1", "the host to listen on")
+	flag.StringVar(&host, "h", "127.0.0.1", "the host to listen on (shorthand)")
+	flag.IntVar(&port, "port", 8080, "the port to listen on")
+	flag.IntVar(&port, "p", 8080, "the port to listen on (shorthand)")
+}
+
 func main() {
+	initFlags()
+	flag.Parse()
 
 	// log init
 	log.Println("[i] Setting up logging...")
@@ -70,6 +84,6 @@ func main() {
 	auth_needed.HandleFunc("/battle/{id}", battleSingleHandler)
 	auth_needed.HandleFunc("/battle/{id}/submit", battleSubmitHandler)
 
-	log.Println("[i] HTTP Server running on port :8080")
-	log.Fatal(http.ListenAndServe(":8080", r))
+	log.Printf("[i] HTTP Server running on %s:%d\n", host, port)
+	log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", host, port), r))
 }