summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorache <ache@ache.one>2021-03-18 03:34:05 +0100
committerache <ache@ache.one>2021-03-18 03:34:05 +0100
commit895a9449f73e846de8f260e90f88a0238f688035 (patch)
tree326bf1237e93d8f3911cb80cd7e526ebd441af73
parentConfig + 1.16 + MultiSelection (diff)
Port configuration
-rw-r--r--config/config.go3
-rw-r--r--config/config.json4
-rw-r--r--server.go16
3 files changed, 18 insertions, 5 deletions
diff --git a/config/config.go b/config/config.go
index ddf3f0a..f10522d 100644
--- a/config/config.go
+++ b/config/config.go
@@ -12,6 +12,7 @@ type config struct {
AllowHidden bool `json:"allow-hidden"`
Auth bool `json:"auth"`
RootPath string `json:"root-path"`
+ Cache bool `json:"cache"`
}
var Config config
@@ -37,7 +38,7 @@ func ReadConfig(path string) error {
func SetDefaultValue() {
Config.Hostnames = []string{"localhost"}
- Config.Port = 8080
+ Config.Port = 0
Config.AllowHidden = false
Config.Auth = false
Config.RootPath = "."
diff --git a/config/config.json b/config/config.json
index 74dac99..3a88cb9 100644
--- a/config/config.json
+++ b/config/config.json
@@ -1,6 +1,6 @@
{
"host":["localhost"],
- "port": 8080,
"allow-hidden": false,
- "auth": true
+ "auth": true,
+ "cache": true
}
diff --git a/server.go b/server.go
index aed70d8..aa4a6a5 100644
--- a/server.go
+++ b/server.go
@@ -396,18 +396,23 @@ func handleMethod( w http.ResponseWriter, r *http.Request) {
func main() {
var dirRoot string
var help, version bool
+ var port = -1
helpString := `This is the help`
versionString := "0.0.0"
const (
defaultRoot = "."
+ defaultPort = 8080
+ portUsage = "port to listen"
usageDir = "the directory to serve"
usageHelp = "show some help"
usageVersion = "show the version"
)
flag.StringVar(&dirRoot, "directory", "", usageDir)
flag.StringVar(&dirRoot, "d", "", usageDir + " (shorthand)")
+ flag.IntVar(&port, "port", 0, portUsage)
+ flag.IntVar(&port, "p", 0, portUsage + " (shorthand)")
flag.BoolVar(&help, "help", false, usageHelp)
flag.BoolVar(&help, "h", false, usageHelp+" (shorthand)")
flag.BoolVar(&version, "version", false, usageVersion)
@@ -432,6 +437,13 @@ func main() {
}
}
+ if port == 0 {
+ port = config.Config.Port
+ if port == 0 {
+ port = defaultPort
+ }
+ }
+
if dirRoot == "" {
dirRoot = config.Config.RootPath
if dirRoot == "" {
@@ -461,9 +473,9 @@ func main() {
return
}
- fmt.Println("Launch server on port:", config.Config.Port)
+ fmt.Println("Launch server on port:", port)
http.HandleFunc("/", handleMethod)
- log.Fatal(http.ListenAndServe(":" + strconv.Itoa(config.Config.Port), nil))
+ log.Fatal(http.ListenAndServe(":" + strconv.Itoa(port), nil))
fmt.Println("Bye bye")
}