92
main.go
92
main.go
@@ -1,24 +1,88 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/jinzhu/gorm"
|
||||
"git.cosysda.de/HuskyTeufel/Hochzeit/handlers"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
var (
|
||||
//audience string
|
||||
//domain string
|
||||
)
|
||||
|
||||
func main() {
|
||||
pass := os.Getenv("DB_PASS")
|
||||
db, err := gorm.Open(
|
||||
"postgres",
|
||||
"host=besucher-db user=go password="+pass+" dbname=go sslmode=disable")
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
//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))
|
||||
}
|
||||
}))
|
||||
|
||||
|
||||
app := App{
|
||||
db: db,
|
||||
r: mux.NewRouter(),
|
||||
//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)
|
||||
}
|
||||
app.start()
|
||||
}
|
||||
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user