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 (
"log"
"net"
"net/http"
"github.com/spf13/viper"
)
func page01 (w http.ResponseWriter, r *http.Request) {
......@@ -34,11 +35,22 @@ func doit (w http.ResponseWriter, r *http.Request) {
}
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("/do", doit)
fmt.Println("started")
err := http.ListenAndServe(":40080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
errserver := http.ListenAndServe(server_address+":"+server_port, nil)
if errserver != nil {
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