-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
108 lines (89 loc) · 2.43 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"fmt"
"os"
"github.com/HenkCord/GOServiceNotifications/conf"
"github.com/HenkCord/GOServiceNotifications/db"
e "github.com/HenkCord/GOServiceNotifications/email"
"github.com/HenkCord/GOServiceNotifications/errors"
p "github.com/HenkCord/GOServiceNotifications/push"
s "github.com/HenkCord/GOServiceNotifications/sms"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func init() {
conf.Init()
StartLog()
}
func main() {
db.InitDBConnection()
defer db.CloseDBConnection()
router := gin.Default()
router.Use(Logger())
sms := router.Group("/sms")
{
smsTemplate := sms.Group("/template")
{
smsTemplate.GET("", s.GetTemplates)
smsTemplate.GET("/:name", s.GetTemplate)
smsTemplate.POST("", s.CreateTemplate)
smsTemplate.PUT("/:name", s.UpdateTemplate)
smsTemplate.DELETE("/:name", s.DeleteTemplate)
}
sms.POST("/getCode", s.GetCode)
}
email := router.Group("/email")
{
emailTemplate := email.Group("/template")
{
emailTemplate.GET("", e.GetTemplates)
emailTemplate.GET("/:name", e.GetTemplate)
emailTemplate.POST("", e.CreateTemplate)
emailTemplate.PUT("/:name", e.UpdateTemplate)
emailTemplate.DELETE("/:name", e.DeleteTemplate)
}
email.POST("/confirmEmail", e.ConfirmEmailAddress)
}
push := router.Group("/push")
{
pushTemplate := push.Group("/template")
{
pushTemplate.GET("", p.GetTemplates)
pushTemplate.GET("/:name", p.GetTemplate)
pushTemplate.POST("", p.CreateTemplate)
pushTemplate.PUT("/:name", p.UpdateTemplate)
pushTemplate.DELETE("/:name", p.DeleteTemplate)
}
push.POST("/giveReview", p.GiveReview)
}
host := viper.GetString("server.host")
port := viper.GetString("server.port")
router.Run(host + ":" + port)
}
//StartLog write info in console
func StartLog() {
host := viper.GetString("server.host")
port := viper.GetString("server.port")
env := os.Getenv("Env")
fmt.Println("--------------------------------")
fmt.Println("Server started on", host, port)
fmt.Println("Environment: " + env)
if env != "production" {
fmt.Println("Activation production mode: `--environment=production` in console")
}
fmt.Println("--------------------------------")
}
//Logger middleware
func Logger() gin.HandlerFunc {
return func(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
c.JSON(errors.InternalServerError("server_error", r.(string)))
return
}
}()
// before request
c.Next()
// after request
}
}