summaryrefslogtreecommitdiff
path: root/main.go
blob: d73587bbe6652b5e725b2c1c839ed9a9f7023894 (plain)
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
package main

import (
	"github.com/gorilla/mux"
	"github.com/gorilla/schema"
	"github.com/gorilla/sessions"
	"html/template"
	"log"
	"net/http"
	"os"
)

var decoder = schema.NewDecoder()

var store = sessions.NewCookieStore([]byte(os.Getenv("FREEMAIL_SECRET")))

var mainTempl = template.Must(template.New("global").Funcs(template.FuncMap{"add": add}).ParseGlob("./views/*.html"))
var emailTempl = template.Must(template.New("email").Funcs(template.FuncMap{"add": add}).ParseGlob("./views/email/*.html"))

func add(x, y int) int {
	return x + y
}

func main() {
	decoder.IgnoreUnknownKeys(true)
	store.Options = &sessions.Options{
		Path:     "/",
		MaxAge:   86400,
		HttpOnly: true,
	}
	checkConfig()
	InitDB()

	r := mux.NewRouter()
	r.HandleFunc("/", IndexHandler)
	r.HandleFunc("/register", RegisterHandler)
	r.HandleFunc("/create", CreateNewEntryHandler).Methods("POST")
	r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))

	http.Handle("/", r)

	ip := os.Getenv("FREEMAIL_HTTP_IP")
	port := os.Getenv("FREEMAIL_HTTP_PORT")

	err := http.ListenAndServe(ip+":"+port, nil)
	if err != nil {
		log.Panic(err)
	}
	log.Println("Server is up and listens on " + ip + ":" + port)
}