diff --git a/server/Dockerfile b/server/Dockerfile new file mode 100644 index 0000000..cc75960 --- /dev/null +++ b/server/Dockerfile @@ -0,0 +1,22 @@ +FROM golang:1.23.3-alpine as builder +WORKDIR /app + +COPY . . +RUN go mod tidy +RUN go mod download +RUN go get -u github.com/swaggo/swag +RUN go get -u github.com/gin-gonic/gin +RUN go build -o main . + + +FROM debian:bullseye-slim + +WORKDIR /app +COPY --from=builder /app/main . +COPY --from=builder /app/.env . +COPY --from=builder /app/config.json . + +RUN chmod +x /app/main + +EXPOSE 8080 +CMD ["./main"] diff --git a/server/config.json b/server/config.json index 56a81f4..5746968 100644 --- a/server/config.json +++ b/server/config.json @@ -1,19 +1,11 @@ { "app_name": "AREA", - "port": 8081, + "port": 8080, "gin_mode": "debug", - "secret_key": "your-secure-secret-key", "cors": true, - "db": { - "host": "127.0.1", - "port": 3306, - "name": "area", - "user": "root", - "password": "root" - }, "swagger": true, "cors_origins": [ - "http://localhost:3000", + "http://localhost:8081", "http://example.com" ] } diff --git a/server/docs/docs.go b/server/docs/docs.go index 5364fd0..39a5b16 100644 --- a/server/docs/docs.go +++ b/server/docs/docs.go @@ -16,8 +16,8 @@ const docTemplate = `{ "email": "support@example.com" }, "license": { - "name": "Apache 2.0", - "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + "name": "GPL-3.0", + "url": "https://www.gnu.org/licenses/gpl-3.0.en.html#license-text" }, "version": "{{.Version}}" }, diff --git a/server/docs/swagger.json b/server/docs/swagger.json index 606d71e..47e0315 100644 --- a/server/docs/swagger.json +++ b/server/docs/swagger.json @@ -10,8 +10,8 @@ "email": "support@example.com" }, "license": { - "name": "Apache 2.0", - "url": "http://www.apache.org/licenses/LICENSE-2.0.html" + "name": "GPL-3.0", + "url": "https://www.gnu.org/licenses/gpl-3.0.en.html#license-text" }, "version": "1.0" }, diff --git a/server/docs/swagger.yaml b/server/docs/swagger.yaml index fc8bde8..dee2540 100644 --- a/server/docs/swagger.yaml +++ b/server/docs/swagger.yaml @@ -7,8 +7,8 @@ info: url: http://www.swagger.io/support description: API documentation for AREA backend license: - name: Apache 2.0 - url: http://www.apache.org/licenses/LICENSE-2.0.html + name: GPL-3.0 + url: https://www.gnu.org/licenses/gpl-3.0.en.html#license-text termsOfService: http://swagger.io/terms/ title: AREA API version: "1.0" diff --git a/server/internal/config/config.go b/server/internal/config/config.go index 2a1d02b..acb5dc6 100644 --- a/server/internal/config/config.go +++ b/server/internal/config/config.go @@ -8,23 +8,13 @@ import ( var AppConfig *Config -type DB struct { - Host string `mapstructure:"host"` - Port int `mapstructure:"port"` - Name string `mapstructure:"name"` - User string `mapstructure:"user"` - Password string `mapstructure:"password"` -} - type Config struct { AppName string `mapstructure:"app_name"` Port int `mapstructure:"port"` GinMode string `mapstructure:"gin_mode"` Cors bool `mapstructure:"cors"` CorsOrigins []string `mapstructure:"cors_origins"` - SecretKey string `mapstructure:"secret_key"` Swagger bool `mapstructure:"swagger"` - DB DB `mapstructure:"db"` } func LoadConfig() { diff --git a/server/internal/pkg/db.go b/server/internal/pkg/db.go index 3deebdf..5139365 100644 --- a/server/internal/pkg/db.go +++ b/server/internal/pkg/db.go @@ -1,21 +1,22 @@ package db import ( - "AREA/internal/config" + "AREA/internal/utils" "fmt" "gorm.io/driver/mysql" "gorm.io/gorm" "log" - "strconv" ) var DB *gorm.DB func InitDB() { - - user := config.AppConfig.DB.User - password := config.AppConfig.DB.Password - dsn := user + ":" + password + "@tcp(" + config.AppConfig.DB.Host + ":" + strconv.Itoa(config.AppConfig.DB.Port) + ")/" + config.AppConfig.DB.Name + "?parseTime=true" + dbHost := utils.GetEnvVar("DB_HOST") + dbPort := utils.GetEnvVar("DB_PORT") + dbName := utils.GetEnvVar("DB_NAME") + user := utils.GetEnvVar("DB_USER") + password := utils.GetEnvVar("DB_PASSWORD") + dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", user, password, dbHost, dbPort, dbName) err := error(nil) DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { diff --git a/server/internal/utils/auth.go b/server/internal/utils/auth.go new file mode 100644 index 0000000..3a33012 --- /dev/null +++ b/server/internal/utils/auth.go @@ -0,0 +1,12 @@ +package utils + +import ( + "encoding/base64" + "golang.org/x/crypto/argon2" +) + +func HashPassword(password string) string { + salt := []byte("randomSalt") // Ideally, generate a unique salt per user + hash := argon2.IDKey([]byte(password), salt, 1, 64*1024, 4, 32) + return base64.RawStdEncoding.EncodeToString(hash) +} diff --git a/server/internal/utils/token.go b/server/internal/utils/token.go index 19f6716..6745b90 100644 --- a/server/internal/utils/token.go +++ b/server/internal/utils/token.go @@ -1,7 +1,6 @@ package utils import ( - "AREA/internal/config" "github.com/dgrijalva/jwt-go" "github.com/gin-gonic/gin" "net/http" @@ -13,7 +12,7 @@ func NewToken(c *gin.Context, email string) string { "email": email, "exp": time.Now().Add(time.Hour * 1).Unix(), }) - tokenString, err := token.SignedString([]byte(config.AppConfig.SecretKey)) + tokenString, err := token.SignedString([]byte(GetEnvVar("SECRET_KEY"))) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token"}) } diff --git a/server/main.go b/server/main.go index 346d498..1fb2853 100644 --- a/server/main.go +++ b/server/main.go @@ -20,8 +20,8 @@ import ( // @contact.url http://www.swagger.io/support // @contact.email support@example.com -// @license.name Apache 2.0 -// @license.url http://www.apache.org/licenses/LICENSE-2.0.html +// @license.name GPL-3.0 +// @license.url https://www.gnu.org/licenses/gpl-3.0.en.html#license-text // @host localhost:8080 // @BasePath / diff --git a/server/start_server.sh b/server/start_server.sh new file mode 100755 index 0000000..d9e74c6 --- /dev/null +++ b/server/start_server.sh @@ -0,0 +1,7 @@ +#! /bin/bash + +# Start the server +COMPOSE_FILE=build/docker-compose.yml +ENV_FILE=.env + +docker-compose --env-file $ENV_FILE -f $COMPOSE_FILE -p server up --build -d \ No newline at end of file diff --git a/server/stop_server.sh b/server/stop_server.sh new file mode 100755 index 0000000..20815e5 --- /dev/null +++ b/server/stop_server.sh @@ -0,0 +1,7 @@ +#! /bin/bash + +# Stop the server +COMPOSE_FILE=build/docker-compose.yml +ENV_FILE=.env + +docker-compose --env-file $ENV_FILE -f $COMPOSE_FILE down --remove-orphans \ No newline at end of file