summaryrefslogtreecommitdiff
path: root/main.go
blob: 6d384cedd0c838b7c60de4a6ec7ddf5b0d7f8d26 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main

import (
	"encoding/hex"
	"flag"
	"fmt"
	"github.com/gorilla/mux"
	"github.com/gorilla/sessions"
	"github.com/silvasur/mailremind/confhelper"
	"github.com/silvasur/mailremind/model"
	_ "github.com/silvasur/mailremind/model/mysql"
	"github.com/silvasur/simpleconf"
	"log"
	"math/rand"
	"net/http"
	"time"
)

func debug(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintf(rw, "Content-Type: text/plain\r\n\r\n%#v", req)
}

var conf simpleconf.Config
var baseurl string

var SessionStorage sessions.Store

func initSessions() {
	_auth := confhelper.ConfStringOrFatal(conf, "securecookies", "auth")
	auth, err := hex.DecodeString(_auth)
	if err != nil {
		log.Fatalf("Could not decode securecookies.auth as hex: %s", err)
	}

	_crypt := confhelper.ConfStringOrFatal(conf, "securecookies", "crypt")
	crypt, err := hex.DecodeString(_crypt)
	if err != nil {
		log.Fatalf("Could not decode securecookies.auth as hex: %s", err)
	}

	SessionStorage = sessions.NewCookieStore(auth, crypt)
}

func main() {
	confpath := flag.String("config", "", "Path to config file")
	flag.Parse()

	var err error
	if conf, err = simpleconf.LoadByFilename(*confpath); err != nil {
		log.Fatalf("Could not read config: %s", err)
	}

	baseurl = confhelper.ConfStringOrFatal(conf, "web", "baseurl")

	rand.Seed(time.Now().UnixNano())

	initSessions()
	initTpls()
	loadTimeLocs()
	initMailing()
	initMails()
	initDB()
	initLimits()
	defer dbcon.Close()

	staticpath := confhelper.ConfStringOrFatal(conf, "paths", "static")
	laddr := confhelper.ConfStringOrFatal(conf, "net", "laddr")

	initCheckjobs()
	go checkjobs()

	router := mux.NewRouter()
	router.PathPrefix("/static").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(staticpath))))
	router.HandleFunc("/register", mkHttpHandler(register, tplRegister))
	router.HandleFunc("/activate", mkHttpHandler(activate, tplMsg))
	router.HandleFunc("/login", mkHttpHandler(login, tplLogin))
	router.HandleFunc("/logout", mkHttpHandler(logout, tplMsg))
	router.HandleFunc("/delete-acc/yes", mkHttpHandler(deleteacc, tplMsg))
	router.HandleFunc("/delete-acc", mkHttpHandler(deleteask, tplReallyDelete))
	router.HandleFunc("/pwreset", mkHttpHandler(pwreset, tplPwreset))
	router.HandleFunc("/forgotpw", mkHttpHandler(forgotpw, tplForgotpw))
	router.HandleFunc("/jobs", mkHttpHandler(jobs, tplJobs))
	router.HandleFunc("/jobedit", mkHttpHandler(jobedit, tplJobedit))
	router.HandleFunc("/jobedit/{ID}", mkHttpHandler(jobedit, tplJobedit))
	router.HandleFunc("/settings", mkHttpHandler(settings, tplSettings))
	router.HandleFunc("/", mkHttpHandler(index, tplIndex))

	http.Handle("/", router)

	if err := http.ListenAndServe(laddr, nil); err != nil {
		log.Fatalf("Could not ListenAndServe: %s", err)
	}
}

func index(user model.User, sess *sessions.Session, req *http.Request) (interface{}, model.User) {
	return nil, user
}