Add config file, Addr is configured via config

This commit is contained in:
Sean DuBois 2014-06-14 18:32:46 +00:00
parent de70086c1b
commit 865a7e49a4
2 changed files with 26 additions and 1 deletions

3
config.json Normal file
View file

@ -0,0 +1,3 @@
{
"Addr": "0.0.0.0:5000"
}

24
main.go
View file

@ -1,13 +1,35 @@
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"net/http"
"os"
)
type Configuration struct {
Addr string
}
func main() {
file, fileErr := os.Open("config.json")
if fileErr != nil {
fmt.Println("failed to open config:", fileErr)
os.Exit(1)
}
configuration := new(Configuration)
configErr := json.NewDecoder(file).Decode(configuration)
if configErr != nil {
fmt.Println("config error:", configErr)
os.Exit(1)
}
r := mux.NewRouter()
GlobalHandler(r.PathPrefix("/global").Subrouter())
http.Handle("/", r)
http.ListenAndServe(":5000", nil)
http.ListenAndServe(configuration.Addr, nil)
}