package main import ( "fmt" "path" "path/filepath" "git.cosysda.de/HuskyTeufel/Hochzeit/handlers" "github.com/gin-gonic/gin" ) var ( //audience string //domain string ) func main() { //setAuth0Variables() fmt.Println("Hallo") r := gin.Default() r.Use(CORSMiddleware()) r.NoRoute((func(c *gin.Context) { dir, file := path.Split(c.Request.RequestURI) ext := filepath.Ext(file) if file == "" || ext == "" { c.File("./ui/dist/ui/index.html") } else { c.File("./ui/dist/ui/" + path.Join(dir, file)) } })) //authorized := r.Group("/") //authorized.Use(authRequired()) r.GET("/besucher", handlers.GetBesucherListHandler) r.POST("/besucher", handlers.AddBesucherHandler) r.DELETE("/besucher/:id", handlers.DeleteBesucherHandler) r.PUT("/besucher", handlers.ComeBesucherHandler) err := r.Run(":3001") if err != nil { panic(err) } } func CORSMiddleware() gin.HandlerFunc { return func(c *gin.Context) { c.Writer.Header().Set("Access-Control-Allow-Origin", "*") c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With") c.Writer.Header().Set("Access-Control-Allow-Methods", "DELETE, GET, OPTIONS, POST, PUT") if c.Request.Method == "OPTIONS" { c.AbortWithStatus(204) return } c.Next() } } /*func setAuth0Variables() { audience = "https://hochzeit-api" //os.Getenv("AUTH0_API_IDENTIFIER") domain = "dev-uzlaiuax.eu.auth0.com/" //os.Getenv("AUTH0_DOMAIN") } func authRequired() gin.HandlerFunc { return func(c *gin.Context) { var auth0Domain = "https://" + domain + "/" client := auth0.NewJWKClient(auth0.JWKClientOptions{URI: auth0Domain + ".well-known/jwks.json"}, nil) configuration := auth0.NewConfiguration(client, []string{audience}, auth0Domain, jose.RS256) validator := auth0.NewValidator(configuration, nil) _, err := validator.ValidateRequest(c.Request) if err != nil { log.Println(err) terminateWithError(http.StatusUnauthorized, "token is not valid", c) return } c.Next() } } */ func terminateWithError(statusCode int, message string, c *gin.Context) { c.JSON(statusCode, gin.H{"error": message}) c.Abort() }