-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (70 loc) · 2.26 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/biskitsx/Hexagonal-Architecture-Go/handler"
"github.com/biskitsx/Hexagonal-Architecture-Go/logs"
"github.com/biskitsx/Hexagonal-Architecture-Go/repository"
"github.com/biskitsx/Hexagonal-Architecture-Go/service"
_ "github.com/go-sql-driver/mysql"
"github.com/gorilla/mux"
"github.com/jmoiron/sqlx"
"github.com/spf13/viper"
)
func main() {
initTimeZone()
initConfig()
db := initDatabase()
customerRepositoryDB := repository.NewCustomerRepositoryDB(db)
customerService := service.NewCustomerService(customerRepositoryDB)
customerHandler := handler.NewCustomerHandler(customerService)
accountRepositoryDB := repository.NewAccountRepositoryDB(db)
accountService := service.NewAccountService(accountRepositoryDB)
accountHandler := handler.NewAccoutHandler(accountService)
router := mux.NewRouter()
router.HandleFunc("/customers", customerHandler.GetCustomers).Methods(http.MethodGet)
router.HandleFunc("/customers/{customerID:[0-9]+}", customerHandler.GetCustomer).Methods(http.MethodGet)
router.HandleFunc("/customers/{customerID:[0-9]+}/accounts", accountHandler.GetAccounts).Methods(http.MethodGet)
router.HandleFunc("/customers/{customerID:[0-9]+}/accounts", accountHandler.NewAccount).Methods(http.MethodPost)
logs.Info("Api service started at port " + viper.GetString("app.port"))
http.ListenAndServe(fmt.Sprintf(":%v", viper.GetInt("app.port")), router)
}
func initConfig() {
viper.SetConfigName("config")
viper.SetConfigType("yaml")
viper.AddConfigPath(".")
// config ค่าผ่าน cli
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
}
func initTimeZone() {
ict, err := time.LoadLocation("Asia/Bangkok")
if err != nil {
panic(err)
}
time.Local = ict
}
func initDatabase() *sqlx.DB {
dsn := fmt.Sprintf("%v:%v@tcp(%v:%v)/%v",
viper.GetString("db.username"),
viper.GetString("db.password"),
viper.GetString("db.host"),
viper.GetString("db.port"),
viper.GetString("db.database"),
)
db, err := sqlx.Open(viper.GetString("db.driver"), dsn)
if err != nil {
log.Fatal(err)
}
db.SetConnMaxLifetime(3 * time.Minute)
db.SetMaxOpenConns(10)
db.SetMaxIdleConns(10)
return db
}