new config package

This commit is contained in:
Suraj Patil 2016-01-23 16:03:10 +05:30
parent 3a3adb35c3
commit 825fc379e0
2 changed files with 34 additions and 2 deletions

30
config/config.go Normal file
View File

@ -0,0 +1,30 @@
package config
import (
"encoding/json"
"io/ioutil"
"log"
)
//Stores the main configuration for the application
type Configuration struct {
ServerPort string
}
var err error
var config Configuration
//ReadConfig will read the configuration json file to read the parameters
//which will be passed in the config file
func ReadConfig(fileName string) Configuration {
configFile, err := ioutil.ReadFile(fileName)
if err != nil {
log.Fatal("Unable to read log file")
}
//log.Print(configFile)
err = json.Unmarshal(configFile, &config)
if err != nil {
log.Print(err)
}
return config
}

View File

@ -6,11 +6,13 @@ package main
**/ **/
import ( import (
"github.com/thewhitetulip/Tasks/views" "github.com/thewhitetulip/Tasks/views"
"github.com/thewhitetulip/Tasks/config"
"log" "log"
"net/http" "net/http"
) )
func main() { func main() {
values := config.ReadConfig("config.json")
views.PopulateTemplates() views.PopulateTemplates()
http.HandleFunc("/", views.ShowAllTasksFunc) http.HandleFunc("/", views.ShowAllTasksFunc)
http.HandleFunc("/complete/", views.CompleteTaskFunc) http.HandleFunc("/complete/", views.CompleteTaskFunc)
@ -26,6 +28,6 @@ func main() {
http.HandleFunc("/search/", views.SearchTaskFunc) http.HandleFunc("/search/", views.SearchTaskFunc)
//http.HandleFunc("/static/", ServeStaticFunc) //http.HandleFunc("/static/", ServeStaticFunc)
http.Handle("/static/", http.FileServer(http.Dir("public"))) http.Handle("/static/", http.FileServer(http.Dir("public")))
log.Println("running server on 8080") log.Println("running server on ", values.ServerPort)
log.Fatal(http.ListenAndServe(":8080", nil)) log.Fatal(http.ListenAndServe(values.ServerPort, nil))
} }