bottin/cmd/server.go

145 lines
3.6 KiB
Go

/*
Copyright © 2022 Victor Lacasse-Beaudoin <victor.lacassebeaudoin@gmail.com>
*/
package cmd
import (
"git.agecem.com/agecem/bottin/bottin"
"git.agecem.com/agecem/bottin/data"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"log"
"time"
)
var (
json_insert_path string
)
// serverCmd represents the server command
var serverCmd = &cobra.Command{
Use: "server",
Short: "Run the bottin server",
Run: func(cmd *cobra.Command, args []string) {
// Timer
db_retry_timer := time.Duration(viper.GetInt("db.retry-timer"))
// Open and migrate db
for {
err := data.OpenDatabase()
if err != nil {
log.Println(err)
time.Sleep(db_retry_timer * time.Second)
continue
}
err = data.MigrateDatabase()
if err != nil {
log.Println(err)
time.Sleep(db_retry_timer * time.Second)
continue
}
break
}
bottin.UpdateFlags()
// Import from flag
if json_insert_path != "" {
bottin.InsertJson(json_insert_path)
}
// Run web app
bottin.RunServer()
},
}
func init() {
declareFlags()
rootCmd.AddCommand(serverCmd)
}
func declareFlags() {
// db.type
serverCmd.PersistentFlags().String(
"db-type", "",
"Database type (config: 'db.type')")
viper.BindPFlag(
"db.type",
serverCmd.PersistentFlags().Lookup("db-type"))
serverCmd.MarkPersistentFlagRequired("db.type")
// db.sqlite.path
serverCmd.PersistentFlags().String(
"db-sqlite-path", "",
"Path to sqlite database (config: 'db.sqlite.path')")
viper.BindPFlag(
"db.sqlite.path",
serverCmd.PersistentFlags().Lookup("db-sqlite-path"))
// server.port
serverCmd.PersistentFlags().Int(
"server-port", 1312,
"The port on which the web application will server content (config: 'server.port')")
viper.BindPFlag(
"server.port",
serverCmd.PersistentFlags().Lookup("server-port"))
// server.static_dir
serverCmd.PersistentFlags().String(
"static-dir", "/var/lib/bottin/static",
"The directory containing static assets (config: 'server.static_dir')")
viper.BindPFlag(
"server.static_dir",
serverCmd.PersistentFlags().Lookup("static-dir"))
// login.username
serverCmd.PersistentFlags().String(
"login-username", "bottin",
"The username to login to the web ui. (config: 'login.username')")
viper.BindPFlag(
"login.username",
serverCmd.PersistentFlags().Lookup("login-username"))
// login.password
serverCmd.PersistentFlags().String(
"login-password", "bottin",
"The password to login to the web ui. (config: 'login.password')")
viper.BindPFlag(
"login.password",
serverCmd.PersistentFlags().Lookup("login-password"))
// import.insert-batch-size
serverCmd.PersistentFlags().Int(
"insert-batch-size", 500,
"The amount of inserts to do per batch (config: 'import.insert_batch_size')")
viper.BindPFlag(
"import.insert_batch_size",
serverCmd.PersistentFlags().Lookup("insert-batch-size"))
// json-insert-path
serverCmd.PersistentFlags().StringVar(
&json_insert_path, "json-insert-path", "",
"The location of a json file containing Membres to insert.")
/*
// Not using viper for json-insert-path since it would make it too easy
// to forget to remove from the config every time, which heavily risks
// doubling values.
//
// It would at least need to check for doubles before importing the file,
// for it to be a kind of automatic differential update of the database.
viper.BindPFlag(
"import.json-insert-path",
serverCmd.PersistentFlags().Lookup("json-insert-path"))
*/
// db.retry-timer
serverCmd.PersistentFlags().Int(
"db-retry-timer", 2,
"Time between failed database connection retries, in seconds.")
viper.BindPFlag(
"db.retry-timer",
serverCmd.PersistentFlags().Lookup("db-retry-timer"))
}