2018-10-15 20:44:15 +02:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
2018-10-17 04:31:27 +02:00
|
|
|
"database/sql"
|
2018-10-16 22:57:55 +02:00
|
|
|
"flag"
|
2018-10-15 20:44:15 +02:00
|
|
|
"fmt"
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
staticDir = "static/"
|
2018-10-18 00:57:37 +02:00
|
|
|
|
|
|
|
serverSoftware = "Write Freely"
|
|
|
|
softwareURL = "https://writefreely.org"
|
|
|
|
softwareVer = "0.1"
|
2018-10-15 20:44:15 +02:00
|
|
|
)
|
|
|
|
|
2018-11-08 04:13:16 +01:00
|
|
|
var (
|
|
|
|
debugging bool
|
|
|
|
)
|
|
|
|
|
2018-10-15 20:44:15 +02:00
|
|
|
type app struct {
|
|
|
|
router *mux.Router
|
2018-10-17 04:31:27 +02:00
|
|
|
db *datastore
|
2018-10-15 20:44:15 +02:00
|
|
|
cfg *config.Config
|
|
|
|
keys *keychain
|
|
|
|
sessionStore *sessions.CookieStore
|
|
|
|
}
|
|
|
|
|
|
|
|
var shttp = http.NewServeMux()
|
|
|
|
|
|
|
|
func Serve() {
|
2018-11-08 04:13:16 +01:00
|
|
|
debugPtr := flag.Bool("debug", false, "Enables debug logging.")
|
2018-10-16 22:57:55 +02:00
|
|
|
createConfig := flag.Bool("create-config", false, "Creates a basic configuration and exits")
|
2018-10-25 15:15:10 +02:00
|
|
|
doConfig := flag.Bool("config", false, "Run the configuration process")
|
2018-10-16 22:57:55 +02:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-11-08 04:13:16 +01:00
|
|
|
debugging = *debugPtr
|
|
|
|
|
2018-10-16 22:57:55 +02:00
|
|
|
if *createConfig {
|
|
|
|
log.Info("Creating configuration...")
|
|
|
|
c := config.New()
|
|
|
|
log.Info("Saving configuration...")
|
2018-10-24 20:21:42 +02:00
|
|
|
err := config.Save(c)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to save configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2018-10-16 22:57:55 +02:00
|
|
|
os.Exit(0)
|
2018-10-25 15:15:10 +02:00
|
|
|
} else if *doConfig {
|
|
|
|
err := config.Configure()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to configure: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
2018-10-16 22:57:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-15 20:44:15 +02:00
|
|
|
log.Info("Initializing...")
|
|
|
|
|
|
|
|
log.Info("Loading configuration...")
|
|
|
|
cfg, err := config.Load()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to load configuration: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
app := &app{
|
|
|
|
cfg: cfg,
|
|
|
|
}
|
|
|
|
|
2018-11-08 04:13:16 +01:00
|
|
|
app.cfg.Server.Dev = *debugPtr
|
|
|
|
|
2018-10-15 20:44:15 +02:00
|
|
|
// Load keys
|
|
|
|
log.Info("Loading encryption keys...")
|
|
|
|
err = initKeys(app)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("\n%s\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize modules
|
2018-10-17 02:30:38 +02:00
|
|
|
app.sessionStore = initSession(app)
|
|
|
|
|
2018-10-17 04:31:27 +02:00
|
|
|
// Check database configuration
|
|
|
|
if app.cfg.Database.User == "" || app.cfg.Database.Password == "" {
|
|
|
|
log.Error("Database user or password not set.")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
if app.cfg.Database.Host == "" {
|
|
|
|
app.cfg.Database.Host = "localhost"
|
|
|
|
}
|
|
|
|
if app.cfg.Database.Database == "" {
|
|
|
|
app.cfg.Database.Database = "writeas"
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Connecting to database...")
|
|
|
|
db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database))
|
|
|
|
if err != nil {
|
|
|
|
log.Error("\n%s\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
app.db = &datastore{db}
|
|
|
|
defer shutdown(app)
|
|
|
|
app.db.SetMaxOpenConns(50)
|
|
|
|
|
2018-10-15 20:44:15 +02:00
|
|
|
r := mux.NewRouter()
|
|
|
|
handler := NewHandler(app.sessionStore)
|
|
|
|
|
|
|
|
// Handle app routes
|
2018-10-17 04:31:27 +02:00
|
|
|
initRoutes(handler, r, app.cfg, app.db)
|
2018-10-15 20:44:15 +02:00
|
|
|
|
|
|
|
// Handle static files
|
|
|
|
fs := http.FileServer(http.Dir(staticDir))
|
|
|
|
shttp.Handle("/", fs)
|
|
|
|
r.PathPrefix("/").Handler(fs)
|
|
|
|
|
|
|
|
// Handle shutdown
|
|
|
|
c := make(chan os.Signal, 2)
|
|
|
|
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
|
|
|
go func() {
|
|
|
|
<-c
|
|
|
|
log.Info("Shutting down...")
|
|
|
|
shutdown(app)
|
|
|
|
log.Info("Done.")
|
|
|
|
os.Exit(0)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start web application server
|
|
|
|
http.Handle("/", r)
|
|
|
|
log.Info("Serving on http://localhost:%d\n", app.cfg.Server.Port)
|
|
|
|
log.Info("---")
|
|
|
|
http.ListenAndServe(fmt.Sprintf(":%d", app.cfg.Server.Port), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func shutdown(app *app) {
|
2018-10-17 04:31:27 +02:00
|
|
|
log.Info("Closing database connection...")
|
|
|
|
app.db.Close()
|
2018-10-15 20:44:15 +02:00
|
|
|
}
|