PracowniaProgramowania/backend/main.go

68 lines
2.5 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
2019-01-12 20:42:29 +01:00
//go run main.go connector_couchdb.go connector_mysql.go models.go views.go tokens.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
2019-01-09 09:22:07 +01:00
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
2019-01-09 09:22:07 +01:00
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
// 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
api.POST("/addNewCardView", addNewCardView) // [couchdb] dodawanie nowej karty
api.POST("/getAllLoggedUsersView", getAllLoggedUsersView) // [mysql] pobieranie listy zalogowanych uzytkownikow
api.POST("/updateLoggedUserView", updateLoggedUserView) //aktualizacja ze uzytkownik zalogowany
api.POST("/updateLogoutUserView", updateLogoutUserView) //aktualizacja stanu jak uzytkownik sie wyloguje
2018-11-18 23:42:18 +01:00
// Start and run the server
router.Run(":3000")
}