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