-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
62 lines (50 loc) · 1.65 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package main
import (
"database/sql"
"os"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres" // specifies the db driver
_ "github.com/golang-migrate/migrate/v4/source/file" // specifies migration source is from a local file
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/sajitron/travel-agency/api"
db "github.com/sajitron/travel-agency/db/sqlc"
"github.com/sajitron/travel-agency/util"
)
func main() {
config, err := util.LoadConfig(".")
if err != nil {
log.Fatal().Err(err).Msg("cannot load config")
}
if config.Environment == "development" {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr})
}
//* connect to the database
conn, err := sql.Open(config.DBDriver, config.DBSource)
if err != nil {
log.Fatal().Err(err).Msg("unable to connect to the database")
}
runDBMigration(config.MigrationURL, config.DBSource)
store := db.NewStore(conn)
runGinServer(config, store)
}
func runDBMigration(migrationURL string, dbSource string) {
migration, err := migrate.New(migrationURL, dbSource)
if err != nil {
log.Fatal().Err(err).Msg("unable to create a new migration instance")
}
if err = migration.Up(); err != nil && err != migrate.ErrNoChange {
log.Fatal().Err(err).Msg("unable to run *migrate up*")
}
log.Info().Msg("Database was migrated successfully")
}
func runGinServer(config util.Config, store db.Store) {
server, err := api.NewServer(config, store)
if err != nil {
log.Fatal().Err(err).Msg("unable to create server")
}
err = server.Start(config.HTTPServerAddress)
if err != nil {
log.Fatal().Err(err).Msg("unable to initiate server")
}
}