18 StaticDirectory string
19 TemplatesGlobPattern string
22 PostFormats []model.PostFormat
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 {
39 func getDefaultConfig() *config {
41 ListenAddress: ":8080",
43 ClientScope: "read write follow",
44 ClientWebsite: "http://localhost:8080",
45 StaticDirectory: "static",
46 TemplatesGlobPattern: "templates/*",
47 DatabasePath: "database.db",
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"},
59 func Parse(r io.Reader) (c *config, err error) {
60 c = getDefaultConfig()
61 scanner := bufio.NewScanner(r)
63 line := strings.TrimSpace(scanner.Text())
69 index := strings.IndexRune(line, '#')
74 index = strings.IndexRune(line, '=')
76 return nil, errors.New("invalid config key")
79 key := strings.TrimSpace(line[:index])
80 val := strings.TrimSpace(line[index+1 : len(line)])
83 case "listen_address":
89 case "client_website":
91 case "static_directory":
92 c.StaticDirectory = val
93 case "templates_glob_pattern":
94 c.TemplatesGlobPattern = val
100 vals := strings.Split(val, ",")
101 var formats []model.PostFormat
102 for _, v := range vals {
103 pair := strings.Split(v, ":")
105 return nil, errors.New("invalid config key " + key)
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)
112 formats = append(formats, model.PostFormat{
117 c.PostFormats = formats
121 return nil, errors.New("invliad config key " + key)
128 func ParseFile(file string) (c *config, err error) {
129 f, err := os.Open(file)
135 info, err := f.Stat()
141 return nil, errors.New("invalid config file")