59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
|
/*
|
||
|
Package cmd
|
||
|
Copyright © 2024 Shane C. <shane@scaffoe.com>
|
||
|
*/
|
||
|
package cmd
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
"github.com/jackc/pgx/v5/stdlib"
|
||
|
"github.com/spf13/cobra"
|
||
|
"github.com/uptrace/bun"
|
||
|
"github.com/uptrace/bun/dialect/pgdialect"
|
||
|
"go.uber.org/zap"
|
||
|
"go.uber.org/zap/zapcore"
|
||
|
"omnibill.net/omnibill/shared"
|
||
|
"omnibill.net/omnibill/web"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
// runCmd represents the run command
|
||
|
var runCmd = &cobra.Command{
|
||
|
Use: "run",
|
||
|
Short: "A brief description of your command",
|
||
|
Long: `A longer description that spans multiple lines and likely contains examples
|
||
|
and usage of using your command. For example:
|
||
|
|
||
|
Cobra is a CLI library for Go that empowers applications.
|
||
|
This application is a tool to generate the needed files
|
||
|
to quickly create a Cobra application.`,
|
||
|
Run: func(cmd *cobra.Command, args []string) {
|
||
|
atomicLevel := zap.NewAtomicLevelAt(zap.InfoLevel)
|
||
|
|
||
|
consoleEncoderConfig := zap.NewDevelopmentEncoderConfig()
|
||
|
consoleEncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
||
|
consoleEncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
|
||
|
consoleEncoderConfig.EncodeCaller = zapcore.ShortCallerEncoder
|
||
|
|
||
|
core := zapcore.NewCore(zapcore.NewConsoleEncoder(consoleEncoderConfig), zapcore.AddSync(os.Stdout), atomicLevel)
|
||
|
logger := zap.New(core, zap.AddCaller())
|
||
|
defer logger.Sync()
|
||
|
|
||
|
postgresDSN := shared.GetPostgresURI()
|
||
|
pool, err := pgxpool.New(context.Background(), postgresDSN)
|
||
|
if err != nil {
|
||
|
logger.Fatal("failed to connect to database", zap.Error(err))
|
||
|
}
|
||
|
|
||
|
sqlDB := stdlib.OpenDBFromPool(pool)
|
||
|
db := bun.NewDB(sqlDB, pgdialect.New())
|
||
|
|
||
|
web.Start(logger, db, pool)
|
||
|
},
|
||
|
}
|
||
|
|
||
|
func init() {
|
||
|
rootCmd.AddCommand(runCmd)
|
||
|
}
|