forked from sktston/go-rest-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (39 loc) · 1.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sktston/go-rest-project/config"
"github.com/sktston/go-rest-project/router"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"os"
"time"
)
// @title Go Rest Project API
// @version 0.1.0
func main() {
//Set a logger with zerolog
zerolog.SetGlobalLevel(zerolog.InfoLevel)
if gin.IsDebugging() {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
}
log.Logger = log.Output(zerolog.ConsoleWriter{Out:os.Stderr,TimeFormat: time.RFC3339})
//Load application config
err := config.LoadConfig()
if err != nil {
log.Fatal().Msgf("cannot load config: %v", err)
}
//Connect to DB
config.DB, err = gorm.Open(postgres.Open(config.GetDSN()), &gorm.Config{})
if err != nil {
log.Fatal().Msgf("Status: %v", err)
}
//Migrate the schema (Create the book table)
config.MigrateSchema()
//Start the gin server
log.Info().Msgf("Starting the server")
r := router.SetupRouter()
r.Run(":" + viper.GetString("server.port"))
}