forked from bxcodec/go-clean-arch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
73 lines (63 loc) · 1.81 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
package main
import (
"database/sql"
"fmt"
"log"
"net/url"
"os"
"time"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
"github.com/spf13/viper"
_articleHttpDeliver "github.com/bxcodec/go-clean-arch/article/delivery/http"
_articleRepo "github.com/bxcodec/go-clean-arch/article/repository"
_articleUcase "github.com/bxcodec/go-clean-arch/article/usecase"
_authorRepo "github.com/bxcodec/go-clean-arch/author/repository"
"github.com/bxcodec/go-clean-arch/middleware"
)
func init() {
viper.SetConfigFile(`config.json`)
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
if viper.GetBool(`debug`) {
fmt.Println("Service RUN on DEBUG mode")
}
}
func main() {
dbHost := viper.GetString(`database.host`)
dbPort := viper.GetString(`database.port`)
dbUser := viper.GetString(`database.user`)
dbPass := viper.GetString(`database.pass`)
dbName := viper.GetString(`database.name`)
connection := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", dbUser, dbPass, dbHost, dbPort, dbName)
val := url.Values{}
val.Add("parseTime", "1")
val.Add("loc", "Asia/Jakarta")
dsn := fmt.Sprintf("%s?%s", connection, val.Encode())
dbConn, err := sql.Open(`mysql`, dsn)
if err != nil && viper.GetBool("debug") {
fmt.Println(err)
}
err = dbConn.Ping()
if err != nil {
log.Fatal(err)
os.Exit(1)
}
defer func() {
err := dbConn.Close()
if err != nil {
log.Fatal(err)
}
}()
e := echo.New()
middL := middleware.InitMiddleware()
e.Use(middL.CORS)
authorRepo := _authorRepo.NewMysqlAuthorRepository(dbConn)
ar := _articleRepo.NewMysqlArticleRepository(dbConn)
timeoutContext := time.Duration(viper.GetInt("context.timeout")) * time.Second
au := _articleUcase.NewArticleUsecase(ar, authorRepo, timeoutContext)
_articleHttpDeliver.NewArticleHandler(e, au)
log.Fatal(e.Start(viper.GetString("server.address")))
}