Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Commit

Permalink
docker config done
Browse files Browse the repository at this point in the history
  • Loading branch information
KryptXBSA committed Oct 3, 2023
1 parent c13de7a commit 3f33cb8
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 108 deletions.
39 changes: 39 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
version: '3'

services:
postgres:
image: postgres:latest
container_name: storagebox-postgres
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: 123
POSTGRES_DB: postgres
ports:
- "5432:5432"

redis:
image: redis:latest
container_name: storagebox-redis
ports:
- "6379:6379"

server:
container_name: storagebox-server
build:
context: ./server
dockerfile: Dockerfile
ports:
- "4000:4000"
depends_on:
- postgres
- redis

website:
container_name: storagebox-website
build:
context: ./website
dockerfile: Dockerfile
ports:
- "4001:4001"
depends_on:
- server
17 changes: 16 additions & 1 deletion server/.env.example
Original file line number Diff line number Diff line change
@@ -1 +1,16 @@
DATABASE_URL="postgresql://postgres:123@localhost:5432/postgres"
JWT_SECRET=your-secret-key

DATABASE_URL=postgresql://postgres:123@localhost:5432/postgres
REDIS_URL=redis://localhost:6379/0

GITHUB_CLIENT_SECRET=
GITHUB_CLIENT_ID=
GITHUB_REDIRECT_URI=http://localhost:4001/auth/github

DISCORD_CLIENT_SECRET=
DISCORD_CLIENT_ID=
DISCORD_REDIRECT_URI=http://localhost:4001/auth/discord

GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_REDIRECT_URI=http://localhost:4001/auth/google
1 change: 0 additions & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
.env
.envv
uploads/*
12 changes: 3 additions & 9 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
FROM golang:1.21.1

# Set destination for COPY
WORKDIR /app

# Download Go modules
COPY . .

# Build
# RUN go run github.com/steebchen/prisma-client-go db push
# RUN CGO_ENABLED=0 GOOS=linux go build -o /main

# ENV JWT_SECRET=your-secret-key
# ENV DATABASE_URL=postgresql://postgres:123@sbp:5432/tus
# ENV REDIS_URL=redis://sbr:6379/0
RUN go mod download

EXPOSE 4000

COPY build-and-run.sh /app/build-and-run.sh
RUN chmod +x /app/build-and-run.sh

ENTRYPOINT ["/app/build-and-run.sh"]
32 changes: 0 additions & 32 deletions server/docker-compose.yml

This file was deleted.

46 changes: 13 additions & 33 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ package main

import (
"fmt"
"log"
"net/http"
"os"

"github.com/AlandSleman/StorageBox/auth"
"github.com/AlandSleman/StorageBox/config"
"github.com/AlandSleman/StorageBox/handler"
"github.com/AlandSleman/StorageBox/middleware"
"github.com/AlandSleman/StorageBox/prisma"
"github.com/AlandSleman/StorageBox/prisma/db"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/redis/go-redis/v9"
limiter "github.com/ulule/limiter/v3"
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
sredis "github.com/ulule/limiter/v3/drivers/store/redis"
)

func loadEnvVariables() {
Expand All @@ -26,46 +21,31 @@ func loadEnvVariables() {
os.Exit(1)
}
}

// create admin user
func seedDb() {
pass, _ := auth.HashPassword("admin")
_, _ = prisma.Client().User.CreateOne(db.User.Username.Set("admin"),
db.User.Provider.Set("password"),
db.User.Role.Set("admin"),
db.User.Password.Set(pass)).Exec(prisma.Context())
}

func main() {
loadEnvVariables()
r := gin.Default()
r.Use(middleware.Cors())

rate, err := limiter.NewRateFromFormatted("70-M")
if err != nil {
log.Fatal(err)
return
}

// Create a redis client.
option, err := redis.ParseURL(config.GetConfig().REDIS_URL)
if err != nil {
log.Fatal("errris:", err)
return
}
client := redis.NewClient(option)

// Create a store with the redis client.
store, err := sredis.NewStoreWithOptions(client, limiter.StoreOptions{
Prefix: "limiter",
MaxRetry: 3,
})
if err != nil {
log.Fatal(err)
return
}

// Create a new middleware with the limiter instance.
limiterMiddleware := mgin.NewMiddleware(limiter.New(store, rate))
r.Use(middleware.RateLimit())

r.Use(limiterMiddleware)
r.GET("/", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "hello",
})
})

prisma.Init()
seedDb()

r.POST("/login", handler.Login)

Expand Down
42 changes: 42 additions & 0 deletions server/middleware/rate_limit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package middleware

import (
"log"
"os"

"github.com/AlandSleman/StorageBox/config"
"github.com/gin-gonic/gin"
"github.com/redis/go-redis/v9"
limiter "github.com/ulule/limiter/v3"
mgin "github.com/ulule/limiter/v3/drivers/middleware/gin"
sredis "github.com/ulule/limiter/v3/drivers/store/redis"
)

func RateLimit() gin.HandlerFunc {
rate, err := limiter.NewRateFromFormatted("70-M")
if err != nil {
log.Fatal(err)
os.Exit(1)
}

// Create a redis client.
option, err := redis.ParseURL(config.GetConfig().REDIS_URL)
if err != nil {
log.Fatal("errris:", err)
os.Exit(1)
}
client := redis.NewClient(option)

// Create a store with the redis client.
store, err := sredis.NewStoreWithOptions(client, limiter.StoreOptions{
Prefix: "limiter",
MaxRetry: 3,
})
if err != nil {
log.Fatal(err)
os.Exit(1)
}

// Create a new middleware with the limiter instance.
return mgin.NewMiddleware(limiter.New(store, rate))
}
17 changes: 0 additions & 17 deletions server/wait-for.sh

This file was deleted.

2 changes: 2 additions & 0 deletions website/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.next/
3 changes: 2 additions & 1 deletion website/.gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

.env
# dependencies
node_modules
.pnp
Expand Down Expand Up @@ -33,4 +34,4 @@ yarn-error.log*
.turbo

.contentlayer
.env
.env
22 changes: 22 additions & 0 deletions website/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Use an official Node.js runtime as a parent image
FROM node:20

# Set the working directory in the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY . .

# Install project dependencies
RUN npm i -g pnpm
RUN pnpm install


# Build your Next.js app
RUN pnpm build

# Expose the port that your Next.js app will run on
EXPOSE 4001

# Define the command to run your Next.js app
CMD ["pnpm", "start"]
7 changes: 7 additions & 0 deletions website/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ const nextConfig = {
},
env: {
SERVER_URL: process.env.SERVER_URL,
LOCAL_SERVER_URL: process.env.LOCAL_SERVER_URL,
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
GITHUB_REDIRECT_URI: process.env.GITHUB_REDIRECT_URI,
DISCORD_CLIENT_ID: process.env.DISCORD_CLIENT_ID,
DISCORD_REDIRECT_URI: process.env.DISCORD_REDIRECT_URI,
GOOGLE_CLIENT_ID: process.env.GOOGLE_CLIENT_ID,
GOOGLE_REDIRECT_URI: process.env.GOOGLE_REDIRECT_URI
}
};

Expand Down
22 changes: 8 additions & 14 deletions website/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
// export let apiUrl = process.env.PRODUCTION
// ? "https://apii.kurdmake.com"
// : "http://localhost:4000"
export let serverUrl = "http://localhost:4000"
export let localServerUrl = "http://localhost:4000"
export let GITHUB_CLIENT_ID = "c98212440d44388b3ac6"
export let GITHUB_REDIRECT_URI = "http://localhost:4001/auth/github"

export let DISCORD_CLIENT_ID = "1154194839299170344"
export let DISCORD_REDIRECT_URI = "http://localhost:4001/auth/discord"

export let GOOGLE_CLIENT_ID =
"198565811833-bcdtkf2qro3fpeo204pnfkt5dial4tbd.apps.googleusercontent.com"
export let GOOGLE_REDIRECT_URI = "http://localhost:4001/auth/google"
export const serverUrl = process.env.SERVER_URL;
export const localServerUrl = process.env.LOCAL_SERVER_URL;
export const GITHUB_CLIENT_ID = process.env.GITHUB_CLIENT_ID;
export const GITHUB_REDIRECT_URI = process.env.GITHUB_REDIRECT_URI;
export const DISCORD_CLIENT_ID = process.env.DISCORD_CLIENT_ID;
export const DISCORD_REDIRECT_URI = process.env.DISCORD_REDIRECT_URI;
export const GOOGLE_CLIENT_ID = process.env.GOOGLE_CLIENT_ID;
export const GOOGLE_REDIRECT_URI = process.env.GOOGLE_REDIRECT_URI;

0 comments on commit 3f33cb8

Please sign in to comment.