-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
39 lines (32 loc) · 1016 Bytes
/
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
package main
import (
// "fmt"
"github.com/gin-gonic/gin"
"url-shortener/src/handlers"
"url-shortener/src/config"
)
const PORT = config.PORT
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Credentials", "true")
c.Header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Header("Access-Control-Allow-Methods", "POST,HEAD,PATCH, OPTIONS, GET, PUT, DELETE")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func main() {
app := gin.Default()
app.Use(CORSMiddleware())
//Init handlers
httpHandler := handlers.NewHandler()
//Routes
app.GET("/u/:key", httpHandler.ForwardUrl)
app.POST("/short-url", httpHandler.CreateShortUrl)
//Start server
app.Run("localhost:" + PORT)
}