-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.go
More file actions
110 lines (89 loc) · 2.31 KB
/
server.go
File metadata and controls
110 lines (89 loc) · 2.31 KB
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"
"os"
"github.com/fuellab/simple-sms/aligo"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/spf13/viper"
)
func readENV(key string) string {
viper.SetConfigFile(".env")
err := viper.ReadInConfig()
if err != nil {
log.Fatalf("Error reading env file: %s", err)
}
value, ok := viper.Get(key).(string)
if !ok {
log.Fatalf("Invalid type assertion")
}
return value
}
var aligoKey, aligoID, aligoPhone, port string
func init() {
aligoKey = readENV("ALIGO_KEY")
aligoID = readENV("ALIGO_ID")
aligoPhone = readENV("ALIGO_PHONE")
val, ok := os.LookupEnv("PORT")
if !ok {
port = "9001"
} else {
port = val
}
}
func main() {
app := fiber.New(fiber.Config{
Prefork: true,
AppName: "FUELLAB SMS V2.0.0",
ServerHeader: "FUELLAB Simple SMS",
ErrorHandler: func(c *fiber.Ctx, err error) error {
code := fiber.StatusNotFound
body := "Not Found"
if e, ok := err.(*fiber.Error); ok && e.Code == fiber.StatusInternalServerError {
code = e.Code
body = e.Message
}
c.Status(code).JSON(fiber.Map{"message": body, "status": "fail"})
return nil
},
})
app.Use(cors.New(cors.Config{
AllowHeaders: "Origin, Content-Type, Accept",
AllowMethods: "GET, POST, OPTIONS, HEAD",
}))
app.Post("/send/sms", func(c *fiber.Ctx) error {
sendData := new(aligo.SendData)
if err := c.BodyParser(sendData); err != nil {
log.Print(err)
return c.Status(403).JSON(fiber.Map{
"message": "Invalid body", "status": "fail",
})
}
if sendData.AKey != aligoKey {
return c.Status(403).JSON(fiber.Map{
"message": "알리고 키 인증에 실패했습니다.", "status": "fail",
})
}
sendData.Key = aligoKey
sendData.UserId = aligoID
sendData.Sender = aligoPhone
aligoRes := aligo.PostAligo(sendData)
if aligoRes.ResultCode != "1" {
return c.Status(400).JSON(fiber.Map{
"message": aligoRes.Message, "status": "fail"})
}
detail := make(map[string]string)
detail["msg_id"] = aligoRes.MsgId
return c.JSON(fiber.Map{
"message": "문자전송에 성공하였습니다.", "status": "success", "data": detail,
})
})
app.Get("/*", func(c *fiber.Ctx) error {
return fiber.ErrNotFound
})
err := app.Listen(":" + port)
if err != nil {
log.Fatalf("Error starting server: %s", err)
return
}
}