/
main.go
101 lines (80 loc) · 2.5 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
// This file is part of Darius. See License.txt for license information.
package main
import (
"database/sql"
"io/ioutil"
"log"
"net/http"
"os"
"github.com/Patrolavia/darius/common"
"github.com/Patrolavia/darius/controller"
"github.com/Patrolavia/darius/model"
"github.com/Patrolavia/jsonapi"
"github.com/gorilla/context"
_ "github.com/mattn/go-sqlite3"
"golang.org/x/oauth2/google"
)
var jsonFile string
func init() {
if len(os.Args) != 2 {
log.Fatalf("Usage: %s path/to/config.json", os.Args[0])
}
jsonFile = os.Args[1]
}
func main() {
data, err := ioutil.ReadFile(jsonFile)
if err != nil {
log.Fatalf("Cannot read data from %s: %s", jsonFile, err)
}
cfg, err := common.JSONConfig(data)
if err != nil {
log.Fatalf("Cannot read configuration: %s", err)
}
db, err := cfg.DB()
if err != nil {
log.Fatalf("Cannot open db connection: %s", err)
}
if err := initDB(db, cfg["DBType"]); err != nil {
log.Fatalf("Cannot initialize database: %s", err)
}
sf, err := common.BuildSession(cfg)
if err != nil {
log.Fatalf("Cannot prepare session store: %s", err)
}
if data, err = ioutil.ReadFile(cfg["GoogleKeyFile"]); err != nil {
log.Fatalf("Cannot read data from %s: %s", cfg["GoogleKeyFile"], err)
}
googleConfig, err := google.ConfigFromJSON(data, "https://www.googleapis.com/auth/plus.login", "email")
if err != nil {
log.Fatalf("Cannot prepare google login info: %s", err)
}
whale := &controller.Whale{sf}
jsonapi.HandleFunc("/api/whale", whale.Whale)
ac := &controller.Auth{googleConfig, sf, cfg}
http.HandleFunc("/auth/google", ac.Google)
http.HandleFunc("/auth/google/oauth2callback", ac.GoogleCallback)
http.HandleFunc("/api/paths", ac.Paths)
http.HandleFunc("/api/logout", ac.Logout)
uc := &controller.User{sf, cfg}
jsonapi.HandleFunc("/api/me", uc.Me)
jsonapi.HandleFunc("/api/user", uc.User)
jsonapi.HandleFunc("/api/users", uc.Users)
pc := &controller.Pad{db, sf, cfg}
jsonapi.HandleFunc("/api/create", pc.Create)
jsonapi.HandleFunc("/api/pad/", pc.View)
jsonapi.HandleFunc("/api/pads", pc.List)
jsonapi.HandleFunc("/api/delete/", pc.Delete)
jsonapi.HandleFunc("/api/edit/", pc.Edit)
sc := &controller.Static{Config: cfg}
http.HandleFunc("/", sc.File)
if err := http.ListenAndServe(cfg["Listen"], context.ClearHandler(http.DefaultServeMux)); err != nil {
log.Fatalf("Cannot start http server at %s: %s", cfg["Listen"], err)
}
}
func initDB(db *sql.DB, t string) (err error) {
m := model.InitSqlite3
if t == "mysql" {
m = model.InitMysql
}
return m(db)
}