-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (50 loc) · 1.53 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
package main
import (
"fmt"
"math/rand"
"net/http"
"os"
"strings"
"time"
"github.com/gin-gonic/gin"
cors "github.com/rs/cors/wrapper/gin"
"github.com/teodorus-nathaniel/uigram-api/comments"
"github.com/teodorus-nathaniel/uigram-api/database"
"github.com/teodorus-nathaniel/uigram-api/jsend"
"github.com/teodorus-nathaniel/uigram-api/posts"
"github.com/teodorus-nathaniel/uigram-api/users"
)
func initializeRoutes(router *gin.RouterGroup) {
posts.Routes(router)
users.Routes(router)
comments.Routes(router)
}
func main() {
_, err := os.Stat("img")
if os.IsNotExist(err) {
os.Mkdir("img", 0755)
}
rand.Seed(time.Now().UnixNano())
defer database.Client.Disconnect(database.Context)
router := gin.Default()
router.GET("/img/:filename", func(c *gin.Context) {
filePath := "img/"
file, exists := c.Params.Get("filename")
if !exists || (!strings.HasSuffix(file, ".jpg") && (!strings.HasSuffix(file, ".jpeg")) && !strings.HasSuffix(file, ".png")) {
c.JSON(http.StatusBadRequest, jsend.GetJSendFail("file not found"))
return
}
c.File(filePath + file)
})
router.Use(cors.New(cors.Options{
AllowedOrigins: []string{"http://localhost:3000", "http://uigram.herokuapp.com", "https://uigram.herokuapp.com"},
Debug: true,
AllowedHeaders: []string{"Authorization", "Content-Type"},
AllowedMethods: []string{"POST", "GET", "HEAD", "PATCH"},
}))
routerGroup := router.Group("/api/v1")
routerGroup.Use(users.GetUserMiddleware())
initializeRoutes(routerGroup)
fmt.Println("Server started...")
router.Run()
}