bloat/config/config.go
localhost_frssoft f9702f81a9 Merge patch and add useredit template...
Patch from: https://git.freesoftwareextremist.com/bloat/commit/?id=887ed241d64ba5db3fd3d87194fb5595e5ad7d73
Patch description:
Use cookies for session
Remove the server side session storage and store all the session related data
in the client side cookies. This decreases the exposure of the auth tokens.
It also simplifies the installation process as bloat no longer requires write
access to the filesystem.

This is a breaking change, all the existing sessions will stop working.
2022-11-12 01:20:49 +03:00

136 lines
2.6 KiB
Go

package config
import (
"bufio"
"errors"
"io"
"os"
"strings"
"bloat/model"
)
type config struct {
ListenAddress string
ClientName string
ClientScope string
ClientWebsite string
SingleInstance string
StaticDirectory string
TemplatesPath string
CustomCSS string
PostFormats []model.PostFormat
LogFile string
}
func (c *config) IsValid() bool {
if len(c.ListenAddress) < 1 ||
len(c.ClientName) < 1 ||
len(c.ClientScope) < 1 ||
len(c.ClientWebsite) < 1 ||
len(c.StaticDirectory) < 1 ||
len(c.TemplatesPath) < 1 {
return false
}
return true
}
func Parse(r io.Reader) (c *config, err error) {
c = new(config)
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if len(line) < 1 {
continue
}
index := strings.IndexRune(line, '#')
if index == 0 {
continue
}
index = strings.IndexRune(line, '=')
if index < 1 {
return nil, errors.New("invalid config key")
}
key := strings.TrimSpace(line[:index])
val := strings.TrimSpace(line[index+1 : len(line)])
switch key {
case "listen_address":
c.ListenAddress = val
case "client_name":
c.ClientName = val
case "client_scope":
c.ClientScope = val
case "client_website":
c.ClientWebsite = val
case "single_instance":
c.SingleInstance = val
case "static_directory":
c.StaticDirectory = val
case "templates_path":
c.TemplatesPath = val
case "custom_css":
c.CustomCSS = val
case "database_path":
// ignore
case "post_formats":
vals := strings.Split(val, ",")
var formats []model.PostFormat
for _, v := range vals {
pair := strings.Split(v, ":")
if len(pair) != 2 {
return nil, errors.New("invalid config key " + key)
}
n := strings.TrimSpace(pair[0])
t := strings.TrimSpace(pair[1])
if len(n) < 1 || len(t) < 1 {
return nil, errors.New("invalid config key " + key)
}
formats = append(formats, model.PostFormat{
Name: n,
Type: t,
})
}
c.PostFormats = formats
case "log_file":
c.LogFile = val
default:
return nil, errors.New("invalid config key " + key)
}
}
return
}
func ParseFiles(files []string) (c *config, err error) {
var lastErr error
for _, file := range files {
f, err := os.Open(file)
if err != nil {
lastErr = err
if os.IsNotExist(err) {
continue
}
return nil, err
}
defer f.Close()
info, err := f.Stat()
if err != nil {
lastErr = err
return nil, err
}
if info.IsDir() {
continue
}
return Parse(f)
}
if lastErr == nil {
lastErr = errors.New("invalid config file")
}
return nil, lastErr
}