Add single instance mode
[bloat] / main.go
1 package main
2
3 import (
4         "errors"
5         "fmt"
6         "log"
7         "math/rand"
8         "net/http"
9         "os"
10         "path/filepath"
11         "strings"
12         "time"
13
14         "bloat/config"
15         "bloat/kv"
16         "bloat/renderer"
17         "bloat/repo"
18         "bloat/service"
19         "bloat/util"
20 )
21
22 var (
23         configFile = "/etc/bloat.conf"
24 )
25
26 func init() {
27         rand.Seed(time.Now().Unix())
28 }
29
30 func errExit(err error) {
31         fmt.Fprintln(os.Stderr, err.Error())
32         os.Exit(1)
33 }
34
35 func main() {
36         opts, _, err := util.Getopts(os.Args, "f:")
37         if err != nil {
38                 errExit(err)
39         }
40
41         for _, opt := range opts {
42                 switch opt.Option {
43                 case 'f':
44                         configFile = opt.Value
45                 }
46         }
47
48         config, err := config.ParseFile(configFile)
49         if err != nil {
50                 errExit(err)
51         }
52
53         if !config.IsValid() {
54                 errExit(errors.New("invalid config"))
55         }
56
57         templatesGlobPattern := filepath.Join(config.TemplatesPath, "*")
58         renderer, err := renderer.NewRenderer(templatesGlobPattern)
59         if err != nil {
60                 errExit(err)
61         }
62
63         err = os.Mkdir(config.DatabasePath, 0755)
64         if err != nil && !os.IsExist(err) {
65                 errExit(err)
66         }
67
68         sessionDBPath := filepath.Join(config.DatabasePath, "session")
69         sessionDB, err := kv.NewDatabse(sessionDBPath)
70         if err != nil {
71                 errExit(err)
72         }
73
74         appDBPath := filepath.Join(config.DatabasePath, "app")
75         appDB, err := kv.NewDatabse(appDBPath)
76         if err != nil {
77                 errExit(err)
78         }
79
80         sessionRepo := repo.NewSessionRepo(sessionDB)
81         appRepo := repo.NewAppRepo(appDB)
82
83         customCSS := config.CustomCSS
84         if !strings.HasPrefix(customCSS, "http://") &&
85                 !strings.HasPrefix(customCSS, "https://") {
86                 customCSS = "/static/" + customCSS
87         }
88
89         var logger *log.Logger
90         if len(config.LogFile) < 1 {
91                 logger = log.New(os.Stdout, "", log.LstdFlags)
92         } else {
93                 lf, err := os.OpenFile(config.LogFile,
94                         os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
95                 if err != nil {
96                         errExit(err)
97                 }
98                 defer lf.Close()
99                 logger = log.New(lf, "", log.LstdFlags)
100         }
101
102         s := service.NewService(config.ClientName, config.ClientScope,
103                 config.ClientWebsite, customCSS, config.PostFormats, renderer,
104                 sessionRepo, appRepo, config.SingleInstance)
105         s = service.NewAuthService(sessionRepo, appRepo, s)
106         s = service.NewLoggingService(logger, s)
107         handler := service.NewHandler(s, config.StaticDirectory)
108
109         logger.Println("listening on", config.ListenAddress)
110         err = http.ListenAndServe(config.ListenAddress, handler)
111         if err != nil {
112                 errExit(err)
113         }
114 }