ef0726030c4eab3b1788f129b7c7b5823cfc00f4
[bloat] / config / config.go
1 package config
2
3 import (
4         "bufio"
5         "errors"
6         "io"
7         "os"
8         "strings"
9
10         "bloat/model"
11 )
12
13 type config struct {
14         ListenAddress        string
15         ClientName           string
16         ClientScope          string
17         ClientWebsite        string
18         StaticDirectory      string
19         TemplatesGlobPattern string
20         DatabasePath         string
21         CustomCSS            string
22         PostFormats          []model.PostFormat
23         Logfile              string
24 }
25
26 func (c *config) IsValid() bool {
27         if len(c.ListenAddress) < 1 ||
28                 len(c.ClientName) < 1 ||
29                 len(c.ClientScope) < 1 ||
30                 len(c.ClientWebsite) < 1 ||
31                 len(c.StaticDirectory) < 1 ||
32                 len(c.TemplatesGlobPattern) < 1 ||
33                 len(c.DatabasePath) < 1 {
34                 return false
35         }
36         return true
37 }
38
39 func getDefaultConfig() *config {
40         return &config{
41                 ListenAddress:        ":8080",
42                 ClientName:           "web",
43                 ClientScope:          "read write follow",
44                 ClientWebsite:        "http://localhost:8080",
45                 StaticDirectory:      "static",
46                 TemplatesGlobPattern: "templates/*",
47                 DatabasePath:         "database.db",
48                 CustomCSS:            "",
49                 PostFormats: []model.PostFormat{
50                         model.PostFormat{"Plain Text", "text/plain"},
51                         model.PostFormat{"HTML", "text/html"},
52                         model.PostFormat{"Markdown", "text/markdown"},
53                         model.PostFormat{"BBCode", "text/bbcode"},
54                 },
55                 Logfile: "",
56         }
57 }
58
59 func Parse(r io.Reader) (c *config, err error) {
60         c = getDefaultConfig()
61         scanner := bufio.NewScanner(r)
62         for scanner.Scan() {
63                 line := strings.TrimSpace(scanner.Text())
64
65                 if len(line) < 1 {
66                         continue
67                 }
68
69                 index := strings.IndexRune(line, '#')
70                 if index == 0 {
71                         continue
72                 }
73
74                 index = strings.IndexRune(line, '=')
75                 if index < 1 {
76                         return nil, errors.New("invalid config key")
77                 }
78
79                 key := strings.TrimSpace(line[:index])
80                 val := strings.TrimSpace(line[index+1 : len(line)])
81
82                 switch key {
83                 case "listen_address":
84                         c.ListenAddress = val
85                 case "client_name":
86                         c.ClientName = val
87                 case "client_scope":
88                         c.ClientScope = val
89                 case "client_website":
90                         c.ClientWebsite = val
91                 case "static_directory":
92                         c.StaticDirectory = val
93                 case "templates_glob_pattern":
94                         c.TemplatesGlobPattern = val
95                 case "database_path":
96                         c.DatabasePath = val
97                 case "custom_css":
98                         c.CustomCSS = val
99                 case "post_formats":
100                         vals := strings.Split(val, ",")
101                         var formats []model.PostFormat
102                         for _, v := range vals {
103                                 pair := strings.Split(v, ":")
104                                 if len(pair) != 2 {
105                                         return nil, errors.New("invalid config key " + key)
106                                 }
107                                 n := strings.TrimSpace(pair[0])
108                                 t := strings.TrimSpace(pair[1])
109                                 if len(n) < 1 || len(t) < 1 {
110                                         return nil, errors.New("invalid config key " + key)
111                                 }
112                                 formats = append(formats, model.PostFormat{
113                                         Name: n,
114                                         Type: t,
115                                 })
116                         }
117                         c.PostFormats = formats
118                 case "logfile":
119                         c.Logfile = val
120                 default:
121                         return nil, errors.New("invliad config key " + key)
122                 }
123         }
124
125         return
126 }
127
128 func ParseFile(file string) (c *config, err error) {
129         f, err := os.Open(file)
130         if err != nil {
131                 return
132         }
133         defer f.Close()
134
135         info, err := f.Stat()
136         if err != nil {
137                 return
138         }
139
140         if info.IsDir() {
141                 return nil, errors.New("invalid config file")
142         }
143
144         return Parse(f)
145 }