-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.go
74 lines (61 loc) · 1.43 KB
/
base.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* @Author: souravray
* @Date: 2014-10-20 16:36:25
* @Last Modified by: souravray
* @Last Modified time: 2014-10-22 00:58:27
*/
package controllers
import (
"encoding/json"
"errors"
"github.com/gorilla/sessions"
"html/template"
//"io/ioutil"
"fmt"
"net/http"
"os"
)
type Config struct {
DbURI string `json:"db_uri"`
DbName string `json:"db_name"`
}
var conf Config
var store = sessions.NewCookieStore([]byte("secret-wish-sting"))
var templates = template.Must(template.ParseFiles(
"static/index.html",
))
func render(w http.ResponseWriter, tmpl string) {
err := templates.ExecuteTemplate(w, tmpl, nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func dispatchError(w http.ResponseWriter, message string) {
err := errors.New(message)
http.Error(w, err.Error(), 500)
}
func dispatchJSON(w http.ResponseWriter, response interface{}) {
w.Header().Add("Content-Type", "application/json")
encoder := json.NewEncoder(w)
encoder.Encode(response)
}
func init() {
// change for heroku deployment
conf = Config{DbURI: os.Getenv("MONGO_URL"),
DbName: os.Getenv("MDB_NAME")}
fmt.Println(conf)
// for non heroku deployment
// content, err := ioutil.ReadFile("config.json")
// if err != nil {
// panic(err)
// }
// err = json.Unmarshal(content, &conf)
// if err != nil {
// panic(err)
// }
}
//
func Landing(w http.ResponseWriter, r *http.Request) {
render(w, "index.html")
return
}