forked from dilshat/sms-sender
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (92 loc) · 2.66 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
109
110
package main
import (
"log"
"github.com/dilshat/sms-sender/controller"
"github.com/dilshat/sms-sender/dao"
_ "github.com/dilshat/sms-sender/docs"
"github.com/dilshat/sms-sender/service"
"github.com/dilshat/sms-sender/sms"
"github.com/dilshat/sms-sender/util"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
echoSwagger "github.com/swaggo/echo-swagger"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// @title Sms service HTTP API
// @description Simple sms service
// @contact.name Dilshat Aliev
// @contact.email [email protected]
func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading env variables", err)
}
initZapLog()
if err != nil {
log.Fatal("Error initializing logger", err)
}
}
func initZapLog() error {
config := zap.NewProductionConfig()
config.EncoderConfig.TimeKey = "timestamp"
config.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
//set log level
logLevel := zap.InfoLevel
logLevel.Set(util.GetEnv("LOG_LEVEL", "ERROR"))
config.Level.SetLevel(logLevel)
logFile := util.GetEnv("LOG_FILE", "")
if logFile != "" {
config.OutputPaths = append(config.OutputPaths, logFile)
}
logger, err := config.Build()
if err != nil {
return err
}
zap.ReplaceGlobals(logger)
return nil
}
func main() {
defer zap.L().Sync() // flushes buffer, if any
//create db client
dbClient, err := dao.GetClient(util.GetEnv("DB_PATH", "sms.db"))
if err != nil {
zap.L().Fatal("Error connecting to db", zap.Error(err))
}
//create smpp client
smppClient := sms.NewClient(util.GetEnv("SMS_IP", ""),
util.GetEnvAsInt("SMS_PORT", 8018),
util.GetEnv("SMS_ID", ""),
util.GetEnv("SMS_PWD", ""),
util.GetEnvAsInt("ENQ_LNK_SEC", 30),
util.GetEnvAsInt("TRX_PER_SEC", 100))
smsSender := sms.NewSender(smppClient)
//start sms sender
err = smsSender.Start()
if err != nil {
zap.L().Fatal("Error connecting to SMSC", zap.Error(err))
}
smsService := service.NewService(
smsSender,
dao.NewMessageDao(dbClient),
dao.NewRecipientDao(dbClient),
util.GetEnvAsInt("STATUS_STORE_DAYS", 7),
util.GetEnvAsInt("SMS_MAX_LEN", 300),
util.GetEnv("WEB_HOOK", ""),
util.GetEnv("PHONE_MASK", "996\\d{9}"),
)
//attach http handlers
e := echo.New()
e.GET("/swagger/*", echoSwagger.WrapHandler)
e.HideBanner = true
e.Use(middleware.BodyLimit("2K"))
bindRoutes(e, smsService)
//start http server
err = e.Start(":" + util.GetEnv("HTTP_PORT", "8080"))
zap.L().Fatal("Error starting http server", zap.Error(err))
}
func bindRoutes(e *echo.Echo, service service.Service) {
e.POST("/sms", controller.GetSendSmsFunc(service))
e.GET("/sms/:id", controller.GetCheckSmsFunc(service))
}