-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
85 lines (67 loc) · 2.34 KB
/
server.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
84
85
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"net/url"
"github.com/FauzanAr/clean-and-go/helpers/logger"
"github.com/FauzanAr/clean-and-go/modules/product/domain"
"github.com/FauzanAr/clean-and-go/modules/product/handler"
"github.com/FauzanAr/clean-and-go/modules/product/repository"
"github.com/FauzanAr/clean-and-go/modules/brand/domain"
"github.com/FauzanAr/clean-and-go/modules/brand/handler"
"github.com/FauzanAr/clean-and-go/modules/brand/repository"
"github.com/FauzanAr/clean-and-go/modules/transaction/domain"
"github.com/FauzanAr/clean-and-go/modules/transaction/handler"
"github.com/FauzanAr/clean-and-go/modules/transaction/repository"
_ "github.com/go-sql-driver/mysql"
)
func main() {
PORT := ":9000"
// Database
dbUser := "fauzan"
dbPass := "passwordlocal123"
dbHost := "localhost"
dbPort := "3306"
dbName := "watchDB"
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 {
log.Fatal(err)
}
err = dbConn.Ping()
if err != nil {
log.Fatal(err)
}
defer func() {
err := dbConn.Close()
if err != nil {
log.Fatal(err)
}
}()
// Declaration Modules
brandRepo := brand_repository.NewBrandRepositoryMysql(dbConn)
brandDomain := brand_domain.NewBrandDomain(brandRepo)
brandHandler := brand_handler.NewBrandHandler(brandDomain)
productRepo := product_repository.NewProductRepositoryMysql(dbConn)
productDomain := product_domain.NewProductDomain(productRepo, brandRepo)
productHandler := product_handler.NewProdutHandler(productDomain)
transactionRepo := transaction_repository.NewTransactionRepositoryMysql(dbConn)
transactionDomain := transaction_domain.NewTransactionDomain(transactionRepo, productRepo)
transactionHandler := transaction_handler.NewTransactionHandler(transactionDomain)
// Route
mux := http.NewServeMux()
mux.HandleFunc("/product/v1", productHandler.Product)
mux.HandleFunc("/product/v1/brand", productHandler.GetByBrand)
mux.HandleFunc("/brand/v1", brandHandler.Brand)
mux.HandleFunc("/transaction/v1", transactionHandler.Transaction)
errServer := http.ListenAndServe(PORT, mux)
if err != nil {
logger.ErrorLogger.Fatal(errServer.Error())
}
}