2022-08-10 05:10:47 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
gosocial "git.kirsle.net/apps/gosocial/pkg"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/config"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/log"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/models"
|
|
|
|
"git.kirsle.net/apps/gosocial/pkg/redis"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"gorm.io/driver/postgres"
|
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
2022-08-11 03:59:59 +00:00
|
|
|
"gorm.io/gorm/logger"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Build-time values.
|
|
|
|
var (
|
|
|
|
Build = "n/a"
|
|
|
|
BuildDate = "n/a"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
config.RuntimeVersion = gosocial.Version
|
|
|
|
config.RuntimeBuild = Build
|
|
|
|
config.RuntimeBuildDate = BuildDate
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "gosocial",
|
|
|
|
Usage: "a niche social networking webapp",
|
|
|
|
Commands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "web",
|
|
|
|
Usage: "start the web server",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
// Debug mode.
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "debug",
|
|
|
|
Aliases: []string{"d"},
|
|
|
|
Usage: "debug mode (logging and reloading templates)",
|
|
|
|
},
|
|
|
|
|
|
|
|
// HTTP settings.
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "host",
|
|
|
|
Aliases: []string{"H"},
|
|
|
|
Value: "0.0.0.0",
|
|
|
|
Usage: "host address to listen on",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "port",
|
|
|
|
Aliases: []string{"P"},
|
|
|
|
Value: 8080,
|
|
|
|
Usage: "port number to listen on",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
if c.Bool("debug") {
|
|
|
|
config.Debug = true
|
|
|
|
log.SetDebug(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
initdb(c)
|
|
|
|
initcache(c)
|
|
|
|
|
|
|
|
log.Debug("Debug logging enabled.")
|
|
|
|
|
|
|
|
app := &gosocial.WebServer{
|
|
|
|
Host: c.String("host"),
|
|
|
|
Port: c.Int("port"),
|
|
|
|
}
|
|
|
|
|
|
|
|
return app.Run()
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "user",
|
|
|
|
Usage: "manage user accounts such as to create admins",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
{
|
|
|
|
Name: "add",
|
|
|
|
Usage: "add a new user account",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "username",
|
|
|
|
Aliases: []string{"u"},
|
|
|
|
Required: true,
|
|
|
|
Usage: "username, case insensitive",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "email",
|
|
|
|
Aliases: []string{"e"},
|
|
|
|
Required: true,
|
|
|
|
Usage: "email address",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "password",
|
|
|
|
Aliases: []string{"p"},
|
|
|
|
Required: true,
|
|
|
|
Usage: "set user password",
|
|
|
|
},
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "admin",
|
|
|
|
Usage: "set admin status",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
initdb(c)
|
|
|
|
|
|
|
|
log.Info("Creating user account: %s", c.String("username"))
|
|
|
|
user, err := models.CreateUser(
|
|
|
|
c.String("username"),
|
|
|
|
c.String("email"),
|
|
|
|
c.String("password"),
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Making an admin?
|
|
|
|
if c.Bool("admin") {
|
|
|
|
log.Warn("Promoting user to admin status")
|
|
|
|
user.IsAdmin = true
|
|
|
|
user.Save()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initdb(c *cli.Context) {
|
|
|
|
// Load the settings.json
|
|
|
|
config.LoadSettings()
|
|
|
|
|
2022-08-22 00:29:39 +00:00
|
|
|
var gormcfg = &gorm.Config{}
|
|
|
|
if c.Bool("debug") {
|
|
|
|
gormcfg = &gorm.Config{
|
|
|
|
Logger: logger.Default.LogMode(logger.Info),
|
|
|
|
}
|
2022-08-11 03:59:59 +00:00
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// Initialize the database.
|
|
|
|
log.Info("Initializing DB")
|
|
|
|
if config.Current.Database.IsSQLite {
|
2022-08-11 03:59:59 +00:00
|
|
|
db, err := gorm.Open(sqlite.Open(config.Current.Database.SQLite), gormcfg)
|
2022-08-10 05:10:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic("failed to open SQLite DB")
|
|
|
|
}
|
|
|
|
models.DB = db
|
|
|
|
} else if config.Current.Database.IsPostgres {
|
2022-08-11 03:59:59 +00:00
|
|
|
db, err := gorm.Open(postgres.Open(config.Current.Database.Postgres), gormcfg)
|
2022-08-10 05:10:47 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("failed to open Postgres DB: %s", err))
|
|
|
|
}
|
|
|
|
models.DB = db
|
|
|
|
} else {
|
|
|
|
log.Fatal("A choice of SQL database is required.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Auto-migrate the DB.
|
|
|
|
models.AutoMigrate()
|
|
|
|
}
|
|
|
|
|
|
|
|
func initcache(c *cli.Context) {
|
|
|
|
// Initialize Redis.
|
|
|
|
log.Info("Initializing Redis")
|
|
|
|
redis.Setup(c.String("redis"))
|
|
|
|
}
|