feature: add web & migrate commands
This commit is contained in:
40
internal/commands/commands.go
Normal file
40
internal/commands/commands.go
Normal file
@ -0,0 +1,40 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const CitadelConfDirEnvName = "CITADEL_CONFIG_DIR"
|
||||
|
||||
const CitadelAppConf = "citadel"
|
||||
|
||||
const mainDescription = `citadel is an open soure project management
|
||||
system written in Golang & React.`
|
||||
|
||||
var (
|
||||
version = "dev"
|
||||
commit = "none"
|
||||
date = "unknown"
|
||||
)
|
||||
|
||||
var versionTemplate = fmt.Sprintf(`Version: %s
|
||||
Commit: %s
|
||||
Built: %s`, version, commit, date+"\n")
|
||||
|
||||
var commandError error
|
||||
var configDir string
|
||||
var verbose bool
|
||||
var noColor bool
|
||||
|
||||
var rootCmd = &cobra.Command{
|
||||
Use: "citadel",
|
||||
Long: mainDescription,
|
||||
Version: version,
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
rootCmd.SetVersionTemplate(versionTemplate)
|
||||
rootCmd.AddCommand(newWebCmd(), newMigrateCmd())
|
||||
rootCmd.Execute()
|
||||
}
|
68
internal/commands/migrate.go
Normal file
68
internal/commands/migrate.go
Normal file
@ -0,0 +1,68 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
"github.com/golang-migrate/migrate/v4/database/postgres"
|
||||
_ "github.com/golang-migrate/migrate/v4/source/file"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/jordanknott/project-citadel/api/internal/config"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type MigrateLog struct {
|
||||
verbose bool
|
||||
}
|
||||
|
||||
func (l *MigrateLog) Printf(format string, v ...interface{}) {
|
||||
log.Printf("%s", v)
|
||||
}
|
||||
|
||||
// Verbose shows if verbose print enabled
|
||||
func (l *MigrateLog) Verbose() bool {
|
||||
return l.verbose
|
||||
}
|
||||
|
||||
func newMigrateCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "migrate",
|
||||
Short: "Run the database schema migrations",
|
||||
Long: "Run the database schema migrations",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
appConfig, err := config.LoadConfig("conf/app.toml")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
|
||||
appConfig.Database.User,
|
||||
appConfig.Database.Password,
|
||||
appConfig.Database.Host,
|
||||
appConfig.Database.Name,
|
||||
)
|
||||
db, err := sqlx.Connect("postgres", connection)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
driver, err := postgres.WithInstance(db.DB, &postgres.Config{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m, err := migrate.NewWithDatabaseInstance(
|
||||
"file://migrations",
|
||||
"postgres", driver)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger := &MigrateLog{}
|
||||
m.Log = logger
|
||||
err = m.Up()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
}
|
52
internal/commands/web.go
Normal file
52
internal/commands/web.go
Normal file
@ -0,0 +1,52 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/spf13/cobra"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/jordanknott/project-citadel/api/internal/config"
|
||||
"github.com/jordanknott/project-citadel/api/internal/route"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func newWebCmd() *cobra.Command {
|
||||
return &cobra.Command{
|
||||
Use: "web",
|
||||
Short: "Run the web server",
|
||||
Long: "Run the web & api server",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
appConfig, err := config.LoadConfig("conf/app.toml")
|
||||
if err != nil {
|
||||
log.WithError(err).Error("loading config")
|
||||
}
|
||||
Formatter := new(log.TextFormatter)
|
||||
Formatter.TimestampFormat = "02-01-2006 15:04:05"
|
||||
Formatter.FullTimestamp = true
|
||||
log.SetFormatter(Formatter)
|
||||
log.SetLevel(log.InfoLevel)
|
||||
|
||||
connection := fmt.Sprintf("user=%s password=%s host=%s dbname=%s sslmode=disable",
|
||||
appConfig.Database.User,
|
||||
appConfig.Database.Password,
|
||||
appConfig.Database.Host,
|
||||
appConfig.Database.Name,
|
||||
)
|
||||
db, err := sqlx.Connect("postgres", connection)
|
||||
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
db.SetMaxOpenConns(25)
|
||||
db.SetMaxIdleConns(25)
|
||||
db.SetConnMaxLifetime(5 * time.Minute)
|
||||
|
||||
defer db.Close()
|
||||
log.WithFields(log.Fields{"url": appConfig.General.Host}).Info("starting server")
|
||||
r, _ := route.NewRouter(appConfig, db)
|
||||
http.ListenAndServe(appConfig.General.Host, r)
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user