This repository has been archived by the owner on Feb 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
neutron.go
102 lines (86 loc) · 2.08 KB
/
neutron.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
package main
import (
"flag"
"io/ioutil"
"gopkg.in/macaron.v1"
"github.com/emersion/neutron/config"
"github.com/emersion/neutron/backend"
"github.com/emersion/neutron/backend/memory"
"github.com/emersion/neutron/backend/imap"
"github.com/emersion/neutron/backend/smtp"
"github.com/emersion/neutron/backend/disk"
"github.com/emersion/neutron/router/api"
)
const (
publicDir = "public/build"
indexFile = "app.html"
)
func main() {
// CLI arguments
cfgPath := flag.String("config", "config.json", "Config file path")
flag.Parse()
// Load config
c, err := config.Load(*cfgPath)
if err != nil {
panic(err)
}
// Create backend
bkd := backend.New()
if c.Memory != nil && c.Memory.Enabled {
memory.Use(bkd)
for _, name := range c.Memory.Domains {
bkd.InsertDomain(&backend.Domain{DomainName: name})
}
if c.Memory.Populate {
memory.Populate(bkd)
}
}
if c.Imap != nil && c.Imap.Enabled {
passwords := imap.Use(bkd, c.Imap.Config)
if c.Smtp != nil && c.Smtp.Enabled {
smtp.Use(bkd, c.Smtp.Config, passwords)
}
}
if c.Disk != nil && c.Disk.Enabled {
if c.Disk.Config != nil {
disk.Use(bkd, c.Disk.Config)
}
if c.Disk.Keys != nil {
disk.UseKeys(bkd, c.Disk.Keys.Config)
}
if c.Disk.Contacts != nil {
disk.UseContacts(bkd, c.Disk.Contacts.Config)
}
if c.Disk.UsersSettings != nil {
disk.UseUsersSettings(bkd, c.Disk.UsersSettings.Config)
}
if c.Disk.Addresses != nil {
disk.UseAddresses(bkd, c.Disk.Addresses.Config)
}
}
// Create server
m := macaron.New()
m.Use(macaron.Logger())
m.Use(macaron.Recovery())
m.Use(macaron.Renderer())
// Initialize API
m.Group("/api", func() {
api.New(m, bkd)
})
// Serve static files
m.Use(macaron.Static(publicDir, macaron.StaticOptions{
IndexFile: indexFile,
//SkipLogging: true,
}))
// Fallback to index file
m.NotFound(func(ctx *macaron.Context) {
data, err := ioutil.ReadFile(publicDir + "/" + indexFile)
if err != nil {
ctx.PlainText(404, []byte("page not found"))
return
}
ctx.Resp.Header().Set("Content-Type", "text/html")
ctx.Resp.Write(data)
})
m.Run()
}