-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (49 loc) · 1.67 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
package main
import (
"log"
"pgxtest/account"
"github.com/gin-gonic/gin"
)
func main() {
// separate the code from the 'main' function so we can test it.
// all code that available in main function were not testable
Run()
}
func Run() {
// prepare gin
// uncomment below mode if want to get back to default debug mode
gin.SetMode(gin.ReleaseMode)
// gin with default setup
r := gin.New()
// r.Use(gin.Logger())
r.Use(gin.Recovery())
// prepare database, remember to create "golangtest" database first
dbPool, _, err := account.NewDBPool(account.DatabaseConfig{
Username : "golang",
Password : "golang",
Hostname : "localhost",
Port : "5432",
DBName : "golangtest",
})
defer dbPool.Close()
// log for error if error occur while connecting to the database
if err != nil {
log.Fatalf("unexpected error while tried to connect to database: %v\n", err)
}
// datastore := account.NewDatastore(dbPool)
accDB := account.NewDatabase(dbPool)
accService := account.NewAccountService(accDB)
accAPI := account.NewAccountHandler(accService)
// prepare router
// main group api endpoint url : http://domain.com/v1
v1 := r.Group("/v1")
// account app group api endpoint : http://domainname.com/v1/account
accRouter := v1.Group("/account")
accRouter.POST("/", accAPI.UserCreateHandler)
accRouter.PUT("/:id", accAPI.UserUpdateHandler)
accRouter.DELETE("/:id", accAPI.UserDeleteHandler)
accRouter.GET("/:id", accAPI.UserGetHandler)
accRouter.GET("/", accAPI.UserGetsHandler)
// run the server
log.Fatalf("%v", r.Run(":8000"))
}