2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2019-05-10 17:40:35 +02:00
|
|
|
* Copyright © 2018-2019 A Bunch Tell LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2018-10-15 20:44:15 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-10 17:40:35 +02:00
|
|
|
"flag"
|
2019-05-13 00:42:57 +02:00
|
|
|
"fmt"
|
2019-06-14 00:50:23 +02:00
|
|
|
"github.com/gorilla/mux"
|
2019-05-10 17:40:35 +02:00
|
|
|
"github.com/writeas/web-core/log"
|
2018-10-15 20:44:15 +02:00
|
|
|
"github.com/writeas/writefreely"
|
2019-05-10 17:40:35 +02:00
|
|
|
"os"
|
2019-05-13 00:42:57 +02:00
|
|
|
"strings"
|
2018-10-15 20:44:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2019-05-10 17:40:35 +02:00
|
|
|
// General options usable with other commands
|
|
|
|
debugPtr := flag.Bool("debug", false, "Enables debug logging.")
|
|
|
|
configFile := flag.String("c", "config.ini", "The configuration file to use")
|
|
|
|
|
|
|
|
// Setup actions
|
|
|
|
createConfig := flag.Bool("create-config", false, "Creates a basic configuration and exits")
|
|
|
|
doConfig := flag.Bool("config", false, "Run the configuration process")
|
2019-07-01 19:21:41 +02:00
|
|
|
configSections := flag.String("sections", "server db app", "Which sections of the configuration to go through (requires --config), "+
|
|
|
|
"valid values are any combination of 'server', 'db' and 'app' "+
|
|
|
|
"example: writefreely --config --sections \"db app\"")
|
2019-05-10 17:40:35 +02:00
|
|
|
genKeys := flag.Bool("gen-keys", false, "Generate encryption and authentication keys")
|
|
|
|
createSchema := flag.Bool("init-db", false, "Initialize app database")
|
|
|
|
migrate := flag.Bool("migrate", false, "Migrate the database")
|
|
|
|
|
|
|
|
// Admin actions
|
|
|
|
createAdmin := flag.String("create-admin", "", "Create an admin with the given username:password")
|
|
|
|
createUser := flag.String("create-user", "", "Create a regular user with the given username:password")
|
|
|
|
resetPassUser := flag.String("reset-pass", "", "Reset the given user's password")
|
|
|
|
outputVersion := flag.Bool("v", false, "Output the current version")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
app := writefreely.NewApp(*configFile)
|
|
|
|
|
|
|
|
if *outputVersion {
|
|
|
|
writefreely.OutputVersion()
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *createConfig {
|
|
|
|
err := writefreely.CreateConfig(app)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *doConfig {
|
2019-06-21 11:07:01 +02:00
|
|
|
writefreely.DoConfig(app, *configSections)
|
2019-05-10 17:40:35 +02:00
|
|
|
os.Exit(0)
|
|
|
|
} else if *genKeys {
|
2019-06-14 00:50:23 +02:00
|
|
|
err := writefreely.GenerateKeyFiles(app)
|
2019-05-10 17:40:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *createSchema {
|
|
|
|
err := writefreely.CreateSchema(app)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *createAdmin != "" {
|
2019-05-13 00:42:57 +02:00
|
|
|
username, password, err := userPass(*createAdmin, true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = writefreely.CreateUser(app, username, password, true)
|
2019-05-10 17:40:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *createUser != "" {
|
2019-05-13 00:42:57 +02:00
|
|
|
username, password, err := userPass(*createUser, false)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
err = writefreely.CreateUser(app, username, password, false)
|
2019-05-10 17:40:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *resetPassUser != "" {
|
|
|
|
err := writefreely.ResetPassword(app, *resetPassUser)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
} else if *migrate {
|
|
|
|
err := writefreely.Migrate(app)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2019-06-14 00:50:23 +02:00
|
|
|
// Initialize the application
|
|
|
|
var err error
|
2019-08-10 18:02:38 +02:00
|
|
|
log.Info("Starting %s...", writefreely.FormatVersion())
|
2019-06-14 00:50:23 +02:00
|
|
|
app, err = writefreely.Initialize(app, *debugPtr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("%s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set app routes
|
|
|
|
r := mux.NewRouter()
|
2019-06-14 03:56:13 +02:00
|
|
|
writefreely.InitRoutes(app, r)
|
2019-06-14 00:50:23 +02:00
|
|
|
app.InitStaticRoutes(r)
|
|
|
|
|
|
|
|
// Serve the application
|
|
|
|
writefreely.Serve(app, r)
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|
2019-05-13 00:42:57 +02:00
|
|
|
|
|
|
|
func userPass(credStr string, isAdmin bool) (user string, pass string, err error) {
|
|
|
|
creds := strings.Split(credStr, ":")
|
|
|
|
if len(creds) != 2 {
|
|
|
|
c := "user"
|
|
|
|
if isAdmin {
|
|
|
|
c = "admin"
|
|
|
|
}
|
|
|
|
err = fmt.Errorf("usage: writefreely --create-%s username:password", c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user = creds[0]
|
|
|
|
pass = creds[1]
|
|
|
|
return
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|