forked from akrennmair/activitylog
/
main.go
103 lines (85 loc) · 2.81 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
_ "code.google.com/p/go-mysql-driver/mysql"
"code.google.com/p/gorilla/pat"
"code.google.com/p/gorilla/sessions"
"database/sql"
"flag"
goconf "github.com/akrennmair/goconf"
"log"
"net/http"
"os"
"runtime"
)
const (
ActivityLimit = 10
SESSION_NAME = "activities-session"
PBKDF2_ROUNDS = 10000
PBKDF2_SIZE = 32
)
type Activity struct {
TypeId int64 `json:"type_id"`
Timestamp string `json:"ts"`
Description string `json:"desc"`
Latitude *float64 `json:"lat"`
Longitude *float64 `json:"long"`
}
type ActivityType struct {
Id int64 `json:"type_id"`
Name string `json:"name"`
TimePeriod bool `json:"time_period"`
}
type AuthResult struct {
Authenticated bool `json:"authenticated"`
ErrorMsg string `json:"errormsg,omitempty"`
Activities []ActivityType `json:"activities,omitempty"`
}
type User struct {
Id string `_id`
Password []byte
Salt []byte
}
var (
gitRevision string
)
func main() {
var cfgfile *string = flag.String("config", "", "configuration file")
flag.Parse()
if *cfgfile == "" {
flag.Usage()
os.Exit(1)
}
log.Printf("this is activities, revision %s", gitRevision)
log.Printf("GOMAXPROCS = %d", runtime.GOMAXPROCS(0))
log.Printf("NumCPU = %d", runtime.NumCPU())
cfg, err := goconf.ReadConfigFile(*cfgfile)
// TODO: add error handling
driver, _ := cfg.GetString("database", "driver")
dsn, _ := cfg.GetString("database", "dsn")
auth_key, _ := cfg.GetString("sessions", "authkey")
enc_key, _ := cfg.GetString("sessions", "enckey")
db_handle, err := sql.Open(driver, dsn)
if err != nil {
log.Fatalf("sql.Open: %v", err)
}
defer db_handle.Close()
db := NewDatabase(db_handle)
store := sessions.NewCookieStore([]byte(auth_key), []byte(enc_key))
r := pat.New()
r.Add("POST", "/auth/try", &TryAuthenticateHandler{Db: db, Store: store})
r.Add("POST", "/auth/signup", &SignupHandler{Db: db})
r.Add("POST", "/auth/logout", &LogoutHandler{Store: store})
r.Add("POST", "/auth", &AuthenticateHandler{Db: db, Store: store})
r.Add("POST", "/activity/add", &AddActivityHandler{Store: store, Db: db})
r.Add("GET", "/activity/list/{page:[0-9]+}", &ListActivitiesHandler{Db: db, Store: store})
r.Add("POST", "/activity/type/add", &AddActivityTypeHandler{Db: db, Store: store})
r.Add("POST", "/activity/type/edit", &EditActivityTypeHandler{ Db: db, Store: store})
r.Add("POST", "/activity/type/del", &DeleteActivityTypeHandler{Db: db, Store: store})
r.Add("GET", "/activity/type/list", &ListActivityTypesHandler{Db: db, Store: store})
r.Add("GET", "/activity/latest", &LatestActivitiesHandler{Db: db, Store: store})
r.Add("GET", "/", http.FileServer(http.Dir("htdocs")))
httpsrv := &http.Server{Handler: Logger(r), Addr: ":8000"}
if err := httpsrv.ListenAndServe(); err != nil {
log.Fatalf("ListenAndServe: %v", err)
}
}