-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
47 lines (41 loc) · 1.17 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
package main
import (
"fmt"
"net/http"
"os"
"strconv"
"strings"
"github.com/Ritalin-Developer/wifiscanner-relay/endpoint"
"github.com/Ritalin-Developer/wifiscanner-relay/model"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
)
func main() {
r := gin.Default()
allowedOrigins := os.Getenv("ALLOWED_ORIGIN")
corsConfig := cors.DefaultConfig()
corsConfig.AllowWildcard = true
corsConfig.AllowOrigins = strings.Split(allowedOrigins, ",") // contain whitelist domain
corsConfig.AllowHeaders = []string{"*", "Content-Type", "Accept"}
corsConfig.AllowCredentials = true
corsConfig.AddAllowMethods("OPTIONS")
r.Use(cors.New(corsConfig))
r.GET("/", func(c *gin.Context) {
var success string = fmt.Sprintf("Server listening with version %s", os.Getenv("VERSION"))
c.JSON(http.StatusOK, &model.Response{
Success: true,
Error: nil,
Msg: success,
Data: nil,
})
})
r.POST("/short", endpoint.ShortURL)
r.GET("/:key", endpoint.GetURL)
port, _ := strconv.Atoi(os.Getenv("PORT"))
log.Infof("Service version: %s", os.Getenv("VERSION"))
err := r.Run(fmt.Sprintf(":%d", port))
if err != nil {
log.Error(err)
}
}