-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
158 lines (141 loc) · 3.67 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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package main
import (
"bytes"
"fmt"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"html/template"
"io/ioutil"
"mime/multipart"
"net/http"
)
func OkResponse(c *gin.Context, data interface{}) {
c.JSON(200, data)
}
func ErrorResponse(c *gin.Context, code int, error error) {
c.JSON(code, gin.H{
"error": error.Error(),
})
}
func runServer() {
r := gin.Default()
templateRoute := r.Group("/template")
{
type UploadTemplateForm struct {
Template *multipart.FileHeader `form:"template" binding:"required"`
}
templateRoute.POST("/upload", func(c *gin.Context) {
var form UploadTemplateForm
if err := c.ShouldBind(&form); err != nil {
c.String(http.StatusBadRequest, "bad request")
ErrorResponse(c, http.StatusBadRequest, err)
return
}
isValid, err := ValidateFilename(form.Template.Filename)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
if !isValid {
ErrorResponse(c, http.StatusBadRequest, errors.New("invalid filename. It must have letters, numbers and \"_\" and \"-\" characters."))
return
}
file, err := form.Template.Open()
defer file.Close()
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
err = PutTemplate(file, form.Template.Filename, form.Template.Size)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
OkResponse(c, gin.H{
"message": "template uploaded",
})
})
templateRoute.DELETE("/delete", func(c *gin.Context) {
filename := c.Query("filename")
if filename == "" {
ErrorResponse(c, 400, errors.New("No filename provided"))
return
}
isValid, err := ValidateFilename(filename)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
if !isValid {
ErrorResponse(c, http.StatusBadRequest, errors.New("invalid filename. It must have letters, numbers and \"_\" and \"-\" characters."))
return
}
err = DeleteTemplate(filename)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
OkResponse(c, gin.H{
"message": "template deleted",
})
})
}
emailRoute := r.Group("/email")
{
type SendEmailInput struct {
TemplateName string `json:"template_name" binding:"required"`
TemplateData *interface{} `json:"template_data"`
EmailSepcs MJInput `json:"email_specs" binding:"required"`
}
emailRoute.POST("/send", func(c *gin.Context) {
var input SendEmailInput
err := c.ShouldBindJSON(&input)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
object, err := GetTemplate(input.TemplateName)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
dataInBytes, err := ioutil.ReadAll(object)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
tmpl, err := template.New(input.TemplateName).Parse(string(dataInBytes))
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
var buff bytes.Buffer
// merging: template = => final email <= = data
err = tmpl.Execute(&buff, input.TemplateData)
if err != nil {
ErrorResponse(c, http.StatusBadRequest, err)
return
}
input.EmailSepcs.HTMLPart = buff.String()
// validating from data
if input.EmailSepcs.From == nil {
input.EmailSepcs.From = &RecipientInputPart{
Email: mjSenderEmail,
Name: mjSenderName,
}
}
err = SendEmail(&input.EmailSepcs)
if err != nil {
ErrorResponse(c, http.StatusInternalServerError, err)
return
}
OkResponse(c, gin.H{
"message": "email sent",
})
})
}
err := r.Run(fmt.Sprintf(":%s", port))
if err != nil {
panic(err)
}
}