Update header template and add option for custom css
[bloat] / main.go
1 package main
2
3 import (
4         "log"
5         "math/rand"
6         "net/http"
7         "os"
8         "path/filepath"
9         "strings"
10         "time"
11
12         "web/config"
13         "web/kv"
14         "web/renderer"
15         "web/repository"
16         "web/service"
17 )
18
19 func init() {
20         rand.Seed(time.Now().Unix())
21 }
22
23 func main() {
24         config, err := config.ParseFile("default.conf")
25         if err != nil {
26                 log.Fatal(err)
27         }
28
29         if !config.IsValid() {
30                 log.Fatal("invalid config")
31         }
32
33         renderer, err := renderer.NewRenderer(config.TemplatesGlobPattern)
34         if err != nil {
35                 log.Fatal(err)
36         }
37
38         err = os.Mkdir(config.DatabasePath, 0755)
39         if err != nil && !os.IsExist(err) {
40                 log.Fatal(err)
41         }
42
43         sessionDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "session"))
44         if err != nil {
45                 log.Fatal(err)
46         }
47
48         appDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "app"))
49         if err != nil {
50                 log.Fatal(err)
51         }
52
53         sessionRepo := repository.NewSessionRepository(sessionDB)
54         appRepo := repository.NewAppRepository(appDB)
55
56         customCSS := config.CustomCSS
57         if !strings.HasPrefix(customCSS, "http://") &&
58                 !strings.HasPrefix(customCSS, "https://") {
59                 customCSS = "/static/" + customCSS
60         }
61
62         var logger *log.Logger
63         if len(config.Logfile) < 1 {
64                 logger = log.New(os.Stdout, "", log.LstdFlags)
65         } else {
66                 lf, err := os.Open(config.Logfile)
67                 if err != nil {
68                         log.Fatal(err)
69                 }
70                 defer lf.Close()
71                 logger = log.New(lf, "", log.LstdFlags)
72         }
73
74         s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, customCSS, renderer, sessionRepo, appRepo)
75         s = service.NewAuthService(sessionRepo, appRepo, s)
76         s = service.NewLoggingService(logger, s)
77         handler := service.NewHandler(s, config.StaticDirectory)
78
79         log.Println("listening on", config.ListenAddress)
80         err = http.ListenAndServe(config.ListenAddress, handler)
81         if err != nil {
82                 log.Fatal(err)
83         }
84 }