Commit 885a427f authored by zdc's avatar zdc

added config file support

parent 23c74aeb
[server]
address = ""
port = "40080"
[routers]
user = ""
password = ""
...@@ -6,6 +6,7 @@ import ( ...@@ -6,6 +6,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"github.com/spf13/viper"
) )
func page01 (w http.ResponseWriter, r *http.Request) { func page01 (w http.ResponseWriter, r *http.Request) {
...@@ -34,11 +35,22 @@ func doit (w http.ResponseWriter, r *http.Request) { ...@@ -34,11 +35,22 @@ func doit (w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
errconfig := viper.ReadInConfig() // Find and read the config file
if errconfig != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", errconfig))
}
server_address := viper.GetString("server.address")
server_port := viper.GetString("server.port")
http.HandleFunc("/", page01) http.HandleFunc("/", page01)
http.HandleFunc("/do", doit) http.HandleFunc("/do", doit)
fmt.Println("started") fmt.Println("started")
err := http.ListenAndServe(":40080", nil) errserver := http.ListenAndServe(server_address+":"+server_port, nil)
if err != nil { if errserver != nil {
log.Fatal("ListenAndServe: ", err) log.Fatal("ListenAndServe: ", errserver)
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment