You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.9 KiB
68 lines
1.9 KiB
package server
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.devices.local/mawas/golang-api-skeleton/api"
|
|
"git.devices.local/mawas/golang-api-skeleton/lib/config"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func Command(appName string, cfgFile string, flagCfg map[string]interface{}) *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "server",
|
|
Short: "Starts application",
|
|
Long: "Starts application",
|
|
// SilenceUsage: true,
|
|
// SilenceErrors: true,
|
|
DisableAutoGenTag: true,
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
return startServer(appName, cfgFile, flagCfg)
|
|
},
|
|
}
|
|
}
|
|
|
|
func startServer(appName string, cfgFile string, flagCfg map[string]interface{}) error {
|
|
if err := config.Initialize(appName, cfgFile, flagCfg); err != nil {
|
|
return fmt.Errorf("config:" + err.Error())
|
|
}
|
|
// db, err := database.Connect(database.Credentials{
|
|
// Host: viper.GetString("database.host"),
|
|
// Port: viper.GetInt("database.port"),
|
|
// Dialect: viper.GetString("database.dialect"),
|
|
// Database: viper.GetString("database.database"),
|
|
// User: viper.GetString("database.user"),
|
|
// Password: viper.GetString("database.password"),
|
|
// MaxOpenConn: viper.GetInt("database.port"),
|
|
// MaxIdleConn: viper.GetInt("database.port"),
|
|
// MaxLifeTime: viper.GetInt("database.port"),
|
|
// Debug: true,
|
|
// })
|
|
// if err != nil {
|
|
// return fmt.Errorf("database:%v", err)
|
|
// }
|
|
app := gin.New()
|
|
app.Use(gin.Recovery())
|
|
// app.Use(inject(db))
|
|
api.ApplyRoutes(app)
|
|
if err := app.Run(fmt.Sprintf("%s:%s", viper.GetString("application.listenaddress"), viper.GetString("application.port"))); err != nil {
|
|
return fmt.Errorf("api:%v", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// func ping(c *gin.Context) {
|
|
// c.JSON(200, gin.H{
|
|
// "message": "pong",
|
|
// })
|
|
// }
|
|
|
|
// Inject injects database to gin context
|
|
// func inject(db *gorm.DB) gin.HandlerFunc {
|
|
// return func(c *gin.Context) {
|
|
// c.Set("db", db)
|
|
// c.Next()
|
|
// }
|
|
// }
|