Skip to content

Commit

Permalink
feat(Server): Credential in env and docker compose for server
Browse files Browse the repository at this point in the history
  • Loading branch information
Djangss committed Nov 30, 2024
1 parent e91edde commit f5e04f4
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 36 deletions.
22 changes: 22 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
12 changes: 2 additions & 10 deletions server/config.json
Original file line number Diff line number Diff line change
@@ -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"
]
}
4 changes: 2 additions & 2 deletions server/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const docTemplate = `{
"email": "[email protected]"
},
"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}}"
},
Expand Down
4 changes: 2 additions & 2 deletions server/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
"email": "[email protected]"
},
"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"
},
Expand Down
4 changes: 2 additions & 2 deletions server/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
10 changes: 0 additions & 10 deletions server/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
13 changes: 7 additions & 6 deletions server/internal/pkg/db.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions server/internal/utils/auth.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 1 addition & 2 deletions server/internal/utils/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils

import (
"AREA/internal/config"
"github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
"net/http"
Expand All @@ -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"})
}
Expand Down
4 changes: 2 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
// @contact.url http://www.swagger.io/support
// @contact.email [email protected]

// @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 /
Expand Down
7 changes: 7 additions & 0 deletions server/start_server.sh
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions server/stop_server.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f5e04f4

Please sign in to comment.