-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtipsy.go
90 lines (74 loc) · 1.85 KB
/
tipsy.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
package main
import (
"log"
"net/http"
"os"
"time"
"github.com/blevesearch/bleve"
"github.com/ppartarr/tipsy/config"
"github.com/ppartarr/tipsy/mail"
"github.com/ppartarr/tipsy/web"
"github.com/ppartarr/tipsy/web/session"
"github.com/ppartarr/tipsy/web/users"
bolt "go.etcd.io/bbolt"
)
const (
version = "0.0.1"
domain = "typo.partarrieu.me"
email = "[email protected]"
)
var (
sessionKey = os.Getenv("SESSION_KEY")
)
func main() {
configFile := "tipsy.yml"
// load server config
tipsyConfig, err := config.LoadServer(configFile)
if err != nil {
log.Fatal(err)
}
// setup & open bolt database
var (
sessionDB bleve.Index
boltDB *bolt.DB
usersDBPath = "db/users.bolt"
)
boltDB, err = bolt.Open(usersDBPath, 0666, nil)
if err != nil {
log.Fatal(err)
}
defer boltDB.Close()
// init mailer
if stat, err := os.Stat(configFile); err == nil && !stat.IsDir() {
if tipsyConfig.Smtp != nil {
mail.InitMailer(
tipsyConfig.Smtp.Server,
tipsyConfig.Smtp.Username,
tipsyConfig.Smtp.Password,
tipsyConfig.Smtp.From,
tipsyConfig.Smtp.Port,
)
}
} else {
log.Fatal("specified configuration file " + configFile + " does not exist or is a directory")
}
// init session
maxAge := int(tipsyConfig.Web.Login.SessionValidity / time.Second)
log.Println("initializing cookie store, cookies will expire after: ", maxAge)
session.StorageDir = "./db"
session.InitStore(sessionKey, sessionDB, maxAge)
// create the server instance
var server *web.Server
server = &web.Server{
FileHandler: &web.FileServer{
Handler: http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))),
},
UserService: users.NewUserService(boltDB, tipsyConfig),
}
// start listening to requests
log.Println("Listening on :8000...")
err = http.ListenAndServe(":8000", server)
if err != nil {
log.Fatal(err)
}
}