-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
71 lines (61 loc) · 1.58 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
package main
import (
"fmt"
"net/http"
"os"
"github.com/labstack/echo/v4"
"github.com/yanoandri/yano-golang-training-beginner/config"
controller "github.com/yanoandri/yano-golang-training-beginner/controller"
service "github.com/yanoandri/yano-golang-training-beginner/services"
"gorm.io/gorm"
)
type Healthy struct {
Status string `json:"status"`
}
type HelloWorld struct {
Message string `json:"message"`
}
func main() {
config.SetupDB()
if len(os.Args) > 1 {
switch os.Args[1] {
case "web":
web(config.GetDBInstance())
case "cron":
cron(config.GetDBInstance())
default:
fmt.Println("Command unknown...")
}
}
}
func web(db *gorm.DB) {
e := echo.New()
e.GET("/hello-world", helloWorld)
e.GET("/health", healthy)
paymentCodeService := service.NewPaymentCodeService(db)
controller.NewPaymentCodeController(e, paymentCodeService)
inquiryService := service.NewInquiryService(db)
controller.NewInquiryController(e, inquiryService)
paymentService := service.NewPaymentService(db)
controller.NewPaymentController(e, paymentService)
e.Logger.Fatal(e.Start(":1323"))
}
func cron(db *gorm.DB) {
fmt.Println("...Cron Start...")
paymentCodeService := service.NewPaymentCodeService(db)
result := paymentCodeService.ExpirePaymentCode()
fmt.Printf("Number of Row Affected : %d\n", result)
fmt.Println("...Cron Ended...")
}
func helloWorld(c echo.Context) error {
message := &HelloWorld{
Message: "hello world",
}
return c.JSON(http.StatusOK, message)
}
func healthy(c echo.Context) error {
status := &Healthy{
Status: "healthy",
}
return c.JSON(http.StatusOK, status)
}