Update config

This commit is contained in:
r 2020-01-28 20:49:58 +00:00
parent 2af37d4778
commit 4613e9cc7b
3 changed files with 61 additions and 40 deletions

View File

@ -1,9 +1,46 @@
# Format:
# - Lines starting with a '#' are ignored
# - Key and Value are separated by a single '='
# - Leading and trailing white spaces in Key and Value are ignored
# - Quoting and multi-line values are not supported
#
# Changing values of client_name, client_scope or client_website will cause
# previously generated access tokens to be invalid. Issue the
# `rm database_path/*` command to clean the database afterwards.
# Address to listen to. Value can be of "HOSTNAME:PORT" or "IP:PORT" form. In
# case of empty HOSTNAME or IP, "0.0.0.0:PORT" is used.
# Example: ":8080", "127.0.0.1:8080", "mydomain:8080"
listen_address=:8080 listen_address=:8080
# Name of the client.
client_name=web client_name=web
# Mastodon scopes used by the client.
# See https://docs.joinmastodon.org/api/oauth-scopes/
client_scope=read write follow client_scope=read write follow
# Full URL of the website. Users will be redirected to this URL after
# authentication.
# Example: "http://localhost:8080", "https://mydomain.com"
client_website=http://localhost:8080 client_website=http://localhost:8080
static_directory=static
templates_glob_pattern=templates/* # Path of database directory. It's used to store session information.
#custom_css=custom.css
#post_formats=PlainText:text/plain,HTML:text/html,Markdown:text/markdown,BBCode:text/bbcode
database_path=database database_path=database
# Path of directory containing template files.
templates_path=templates
# Path of directory containing static files (CSS and JS).
static_directory=static
# Supported post formats. Value is a list of key:value pair separated by a ','.
# Empty value will disable the format selection in frontend.
post_formats=PlainText:text/plain,HTML:text/html,Markdown:text/markdown,BBCode:text/bbcode
# Log file. Will log to stdout if value is empty.
log_file=
# Path to custom CSS. Value can be a file path relative to the static directory.
# or a URL starting with either "http://" or "https://".
# custom_css=custom.css

View File

@ -16,11 +16,11 @@ type config struct {
ClientScope string ClientScope string
ClientWebsite string ClientWebsite string
StaticDirectory string StaticDirectory string
TemplatesGlobPattern string TemplatesPath string
DatabasePath string DatabasePath string
CustomCSS string CustomCSS string
PostFormats []model.PostFormat PostFormats []model.PostFormat
Logfile string LogFile string
} }
func (c *config) IsValid() bool { func (c *config) IsValid() bool {
@ -29,35 +29,15 @@ func (c *config) IsValid() bool {
len(c.ClientScope) < 1 || len(c.ClientScope) < 1 ||
len(c.ClientWebsite) < 1 || len(c.ClientWebsite) < 1 ||
len(c.StaticDirectory) < 1 || len(c.StaticDirectory) < 1 ||
len(c.TemplatesGlobPattern) < 1 || len(c.TemplatesPath) < 1 ||
len(c.DatabasePath) < 1 { len(c.DatabasePath) < 1 {
return false return false
} }
return true return true
} }
func getDefaultConfig() *config {
return &config{
ListenAddress: ":8080",
ClientName: "web",
ClientScope: "read write follow",
ClientWebsite: "http://localhost:8080",
StaticDirectory: "static",
TemplatesGlobPattern: "templates/*",
DatabasePath: "database.db",
CustomCSS: "",
PostFormats: []model.PostFormat{
model.PostFormat{"Plain Text", "text/plain"},
model.PostFormat{"HTML", "text/html"},
model.PostFormat{"Markdown", "text/markdown"},
model.PostFormat{"BBCode", "text/bbcode"},
},
Logfile: "",
}
}
func Parse(r io.Reader) (c *config, err error) { func Parse(r io.Reader) (c *config, err error) {
c = getDefaultConfig() c = new(config)
scanner := bufio.NewScanner(r) scanner := bufio.NewScanner(r)
for scanner.Scan() { for scanner.Scan() {
line := strings.TrimSpace(scanner.Text()) line := strings.TrimSpace(scanner.Text())
@ -90,8 +70,8 @@ func Parse(r io.Reader) (c *config, err error) {
c.ClientWebsite = val c.ClientWebsite = val
case "static_directory": case "static_directory":
c.StaticDirectory = val c.StaticDirectory = val
case "templates_glob_pattern": case "templates_path":
c.TemplatesGlobPattern = val c.TemplatesPath = val
case "database_path": case "database_path":
c.DatabasePath = val c.DatabasePath = val
case "custom_css": case "custom_css":
@ -115,8 +95,8 @@ func Parse(r io.Reader) (c *config, err error) {
}) })
} }
c.PostFormats = formats c.PostFormats = formats
case "logfile": case "log_file":
c.Logfile = val c.LogFile = val
default: default:
return nil, errors.New("invliad config key " + key) return nil, errors.New("invliad config key " + key)
} }

20
main.go
View File

@ -47,7 +47,8 @@ func main() {
log.Fatal("invalid config") log.Fatal("invalid config")
} }
renderer, err := renderer.NewRenderer(config.TemplatesGlobPattern) templatesGlobPattern := filepath.Join(config.TemplatesPath, "*")
renderer, err := renderer.NewRenderer(templatesGlobPattern)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -57,12 +58,14 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
sessionDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "session")) sessionDBPath := filepath.Join(config.DatabasePath, "session")
sessionDB, err := kv.NewDatabse(sessionDBPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
appDB, err := kv.NewDatabse(filepath.Join(config.DatabasePath, "app")) appDBPath := filepath.Join(config.DatabasePath, "app")
appDB, err := kv.NewDatabse(appDBPath)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -77,10 +80,10 @@ func main() {
} }
var logger *log.Logger var logger *log.Logger
if len(config.Logfile) < 1 { if len(config.LogFile) < 1 {
logger = log.New(os.Stdout, "", log.LstdFlags) logger = log.New(os.Stdout, "", log.LstdFlags)
} else { } else {
lf, err := os.Open(config.Logfile) lf, err := os.Open(config.LogFile)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -88,13 +91,14 @@ func main() {
logger = log.New(lf, "", log.LstdFlags) logger = log.New(lf, "", log.LstdFlags)
} }
s := service.NewService(config.ClientName, config.ClientScope, config.ClientWebsite, s := service.NewService(config.ClientName, config.ClientScope,
customCSS, config.PostFormats, renderer, sessionRepo, appRepo) config.ClientWebsite, customCSS, config.PostFormats, renderer,
sessionRepo, appRepo)
s = service.NewAuthService(sessionRepo, appRepo, s) s = service.NewAuthService(sessionRepo, appRepo, s)
s = service.NewLoggingService(logger, s) s = service.NewLoggingService(logger, s)
handler := service.NewHandler(s, config.StaticDirectory) handler := service.NewHandler(s, config.StaticDirectory)
log.Println("listening on", config.ListenAddress) logger.Println("listening on", config.ListenAddress)
err = http.ListenAndServe(config.ListenAddress, handler) err = http.ListenAndServe(config.ListenAddress, handler)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)