PracowniaProgramowania/backend/main.go

70 lines
2.2 KiB
Go
Raw Normal View History

2018-11-18 23:42:18 +01:00
package main
//Uruchomienie na windowsie
// go run main.go connector_couchdb.go connector_mysql.go models.go views.go
2018-11-18 23:42:18 +01:00
import (
"net/http"
"github.com/gin-gonic/gin"
)
2019-01-09 09:22:07 +01:00
func Cors() gin.HandlerFunc {
//https://razil.cc/post/2018/10/go-cors-request-on-gin/
return func(c *gin.Context) {
method := c.Request.Method
origin := c.Request.Header.Get("Origin")
if origin != "" {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
}
if method == "OPTIONS" {
c.Header("Access-Control-Allow-Origin", "*")
c.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token, Authorization") //自定义 Header
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
c.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers, Content-Type")
c.Header("Access-Control-Allow-Credentials", "true")
c.AbortWithStatus(http.StatusNoContent)
}
c.Next()
}
}
2018-11-18 23:42:18 +01:00
func main() {
// Set the router as the default one shipped with Gin
router := gin.Default()
2019-01-09 09:22:07 +01:00
router.Use(Cors())
2018-11-18 23:42:18 +01:00
// Serve frontend static files
2019-01-09 09:22:07 +01:00
// router.Use(static.Serve("/", static.LocalFile("./views", true)))
// config.AllowOrigins = []string{"http://localhost:8080/"}
2018-11-18 23:42:18 +01:00
// Setup route group for the API
api := router.Group("/api")
{
api.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "pong",
})
})
}
api.GET("/getUsersView", getUsersView) //widok pobrania wszystkich użytkowników
api.POST("/addNewUserView", addNewUserView) // json z danymi nowego uzytkownika
api.POST("/loginUserView", loginUserView) //logowanie
api.POST("/updateUserPointsView", updateUserPointsView) // inkrementacja punktow
2019-01-09 07:26:21 +01:00
api.POST("/addNewCardView", addNewCardView) // [couchdb] dodawanie nowej karty
2018-11-18 23:42:18 +01:00
// Start and run the server
router.Run(":3000")
}