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