diff --git a/server/build/docker-compose.yml b/server/build/docker-compose.yml new file mode 100644 index 0000000..f5ab8a2 --- /dev/null +++ b/server/build/docker-compose.yml @@ -0,0 +1,61 @@ +# TODO RESTORE +services: + rabbitmq: + image: rabbitmq:3-management + ports: + - "8082:15672" + - "5000:5673" + networks: + - app_network + volumes: + - ./rabbit-mq/rabbitmq.conf:/etc/rabbitmq/rabbitmq.conf + healthcheck: + test: [ "CMD", "rabbitmqctl", "status" ] + interval: 5s + timeout: 15s + retries: 5 + + mariadb: + image: mariadb:10.4 + ports: + - "3305:3306" + environment: + MYSQL_ROOT_PASSWORD: ${DB_PASSWORD} + MYSQL_DATABASE: ${DB_NAME} + volumes: + - mariadb_data:/var/lib/mysql + networks: + - app_network + healthcheck: + test: [ "CMD", "mysqladmin", "ping", "-h", "localhost" ] + interval: 10s + timeout: 5s + retries: 5 + +# go-app: +# build: +# context: ../. +# dockerfile: Dockerfile +# ports: +# - "8080:8080" +# environment: +# DB_HOST: mariadb +# DB_PORT: 3306 +# DB_NAME: ${DB_NAME} +# DB_USER: root +# DB_PASSWORD: ${DB_PASSWORD} +# SECRET_KEY: ${SECRET_KEY} +# depends_on: +# mariadb: +# condition: service_healthy +# rabbitmq: +# condition: service_healthy +# networks: +# - app_network + +volumes: + mariadb_data: + +networks: + app_network: + driver: bridge diff --git a/server/internal/controllers/oauth.go b/server/internal/controllers/oauth.go index 1d1e75d..e5e406b 100644 --- a/server/internal/controllers/oauth.go +++ b/server/internal/controllers/oauth.go @@ -1,6 +1,10 @@ package controllers import ( + "AREA/internal/pkg" + "fmt" + "net/http" + "github.com/gin-gonic/gin" ) @@ -9,3 +13,31 @@ func Google(c *gin.Context) { "message": "Google", }) } + +type Token struct { + Token string +} + +func OAuth(c *gin.Context) { + var token Token + err := c.ShouldBindJSON(&token) + if err != nil { + c.AbortWithStatus(http.StatusBadRequest) + return + } + userId, err := pkg.GetUserFromToken(c) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H { + "message": "User not found", + }) + return + } + serviceId, err := pkg.GetServiceFromName(c.Param("service")) + if err != nil { + c.AbortWithStatusJSON(http.StatusBadRequest, gin.H { + "message": "Service not found", + }) + return + } + fmt.Println(userId, serviceId) +} diff --git a/server/internal/models/service.go b/server/internal/models/service.go index e0c3ed7..e4545ed 100644 --- a/server/internal/models/service.go +++ b/server/internal/models/service.go @@ -5,5 +5,6 @@ import "gorm.io/gorm" type Service struct { gorm.Model Name string `json:"name"` - Events []Event `gorm:"constraint:OnDelete:CASCADE;" json:"events"` + Events []Event `gorm:"constraint:OnDelete:CASCADE;" json:"events"` + Tokens []Token `gorm:"constraint:OnDelete:CASCADE;" json:"tokens"` } diff --git a/server/internal/models/token.go b/server/internal/models/token.go new file mode 100644 index 0000000..5c8f96f --- /dev/null +++ b/server/internal/models/token.go @@ -0,0 +1,10 @@ +package models + +import "gorm.io/gorm" + +type Token struct { + gorm.Model + Value string + ServiceID uint + UserID uint +} diff --git a/server/internal/models/user.go b/server/internal/models/user.go index 4a6c48e..78a5739 100644 --- a/server/internal/models/user.go +++ b/server/internal/models/user.go @@ -11,4 +11,6 @@ type User struct { Password string `gorm:"not null" json:"password" binding:"required"` Salt string `gorm:"not null" json:"salt"` Token string `gorm:"not null" json:"token"` + Workflows []Workflow `gorm:"constraint:OnDelete:CASCADE;"` + Tokens []Token `gorm:"constraint:OnDelete:CASCADE;"` } diff --git a/server/internal/pkg/db.go b/server/internal/pkg/db.go index 56ec36e..7f05c2a 100644 --- a/server/internal/pkg/db.go +++ b/server/internal/pkg/db.go @@ -18,6 +18,7 @@ func migrateDB() error { &models.Service{}, &models.Event{}, &models.Parameters{}, + &models.Token{}, ) if err != nil { log.Fatalf("Failed to migrate DB: %v", err) diff --git a/server/internal/pkg/token.go b/server/internal/pkg/token.go index 1767c74..74d2984 100644 --- a/server/internal/pkg/token.go +++ b/server/internal/pkg/token.go @@ -19,3 +19,11 @@ func GetUserFromToken(c *gin.Context) (uint, error) { } return user.ID, nil } + +func GetServiceFromName(serviceName string) (uint, error) { + service := models.Service{} + if err := DB.Where("name = ?", serviceName).First(&service); err != nil { + return 0, errors.New("Service not found") + } + return service.ID, nil +} diff --git a/server/internal/routers/routers.go b/server/internal/routers/routers.go index 2536527..5a37750 100644 --- a/server/internal/routers/routers.go +++ b/server/internal/routers/routers.go @@ -11,7 +11,8 @@ import ( ) func setUpOauthGroup(router *gin.Engine) { - oauth := router.Group("/oauth") + router.POST("/oauth/:service", controllers.OAuth) + /*oauth := router.Group("/oauth") { oauth.POST("/google", controllers.Google) @@ -19,8 +20,8 @@ func setUpOauthGroup(router *gin.Engine) { oauth.POST("/github", controllers.Github) oauth.POST("/linkedin", controllers.Linkedin) oauth.POST("/discord", controllers.Discord) - auth.POST("/twitch", controllers.Twitch)*/ - } + auth.POST("/twitch", controllers.Twitch) + }*/ } func setUpAuthGroup(router *gin.Engine) {