-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
189 lines (167 loc) · 5.1 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"context"
"embed"
"log"
"net/http"
"os"
"os/signal"
"strings"
"time"
"shorten-url/utils"
"github.com/compose-spec/compose-go/dotenv"
"github.com/gin-gonic/gin"
)
var GIT_COMMIT string
//go:embed all:views
var webViews embed.FS
var SUPPORT string
func init() {
dotenv.Load()
SUPPORT = os.Getenv("SUPPORT")
gin.SetMode(strings.ToLower(os.Getenv("GIN_MODE")))
}
func main() {
log.Println("Git Commit:", GIT_COMMIT)
router := gin.Default()
router.LoadHTMLGlob("views/*.html")
router.NoRoute(func(c *gin.Context) {
if strings.HasPrefix(c.Request.URL.Path, "/api") {
c.JSON(http.StatusNotFound, gin.H{"error": "not found"})
c.Abort()
}
}, AddFileHandler(webViews))
router.Use(utils.RedirectLimiter).GET("/:id", func(ctx *gin.Context) {
shortenID := utils.ShortURL(strings.TrimSpace(ctx.Param("id")))
if urlData, err := shortenID.GetData(); urlData != nil {
urlData.IncreaseCount()
// no custom meta: header redirect
if urlData.Meta == nil {
ctx.Redirect(http.StatusTemporaryRedirect, string(urlData.TargetURL))
return
}
// has custom meta: js redirect
ctx.HTML(http.StatusOK, "redirect.html", gin.H{
"title": urlData.Meta.Title,
"description": urlData.Meta.Description,
"image": urlData.Meta.ImageURL,
"color": urlData.Meta.ThemeColor,
"targetURL": urlData.TargetURL,
})
return
} else if err != nil {
// server error
ctx.HTML(http.StatusInternalServerError, "500.html", gin.H{"support": SUPPORT})
return
}
// short url not found
ctx.HTML(http.StatusNotFound, "404.html", nil)
})
apiRouter := router.Group("/api")
apiRouter.Use(utils.ShortenLimiter).POST("/shorten", func(ctx *gin.Context) {
data := utils.CreateData{}
if err := ctx.BindJSON(&data); err != nil {
// check data is valid
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid JSON"})
return
}
data.URL = utils.LongURL(strings.TrimSpace(string(data.URL)))
// check whether url is empty
if data.URL == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "original URL is required"})
return
}
// check whether url format is valid
if err := data.URL.IsValid(); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// check whether custom url has been used
data.CustomURL = utils.ShortURL(strings.TrimSpace(string(data.CustomURL)))
if data.CustomURL == "" {
// check whether meta data is same
if urlDate, err := data.URL.CheckMetaSame(data); urlDate != nil {
// data exists and same, return it
ctx.JSON(http.StatusOK, gin.H{
"short": string(urlDate.ShortURL),
"url": data.URL,
"meta": data.Meta})
return
} else if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
} else if err := data.CustomURL.IsValid(); err != nil {
// check whether shortURL format is valid
ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
} else if old, err := data.CustomURL.GetData(); old != nil {
// check whether shortURL has been used
if data.URL != old.TargetURL || data.Meta != old.Meta {
// used
ctx.JSON(http.StatusBadRequest, gin.H{"error": "this custom url is already been used"})
} else {
// same as old, return it
ctx.JSON(http.StatusOK, old)
}
return
} else if err != nil {
// db error
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
// if has meta, fill meta field
if data.Meta != nil {
// check whether image url format is valid
if data.Meta.ImageURL != "" && !data.Meta.ImageURLIsValid() {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid image url"})
return
}
data.InsertMeta()
}
// create short url
urlDate, err := data.CreateShortURL()
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
ctx.JSON(http.StatusCreated, urlDate)
})
apiRouter.Use(utils.GetShortenLimiter).GET("/get/:id", func(ctx *gin.Context) {
shortenID := utils.ShortURL(strings.TrimSpace(ctx.Param("id")))
if urlData, err := shortenID.GetData(); urlData != nil {
ctx.JSON(http.StatusOK, urlData)
return
} else if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "internal server error"})
return
}
ctx.JSON(http.StatusNotFound, gin.H{"error": "not found"})
})
gin.ForceConsoleColor()
srv := &http.Server{
Addr: "0.0.0.0:8080",
Handler: router,
}
if gin.Mode() != gin.ReleaseMode {
srv.Addr = "127.0.0.1:8080"
}
go func() {
log.Println("Server starting...")
log.Println("Listening at:", srv.Addr)
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalln("An error occurred:", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Println("Server shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
utils.CloseDB()
if err := srv.Shutdown(ctx); err != nil {
log.Fatalln("Shutdown Error:", err)
}
log.Println("Server has been shutdown.")
}