Skip to content

Commit 327b8ca

Browse files
authored
Merge pull request #23 from pooulad/dockerize
Dockerize
2 parents e0c4299 + 118821e commit 327b8ca

File tree

9 files changed

+194
-63
lines changed

9 files changed

+194
-63
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
./frontend/node_modules
2+
./frontend/.next
3+
node_modules

.env

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
1-
DB_HOST=localhost
1+
DB_HOST=postgres
22
DB_PORT=5432
33
DB_PASS=postgres
44
DB_USER=postgres
55
DB_SSLMODE=disable
66
DB_NAME=GoNextJsCrud
7-
SECRET_JWT=iuehieuher7v347834geuygwyu32gf783gfuwegysduycgwgf783tf3gf
7+
SECRET_JWT=iuehieuher7v347834geuygwyu32gf783gfuwegysduycgwgf783tf3gf
8+
NEXTAUTH_URL=http://localhost:3000
9+
10+
# api app configuration
11+
API_PORT=8000
12+
API_HOST=0.0.0.0
13+
14+
# client PORT
15+
APP_PORT=3000

Dockerfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
FROM focker.ir/golang:1.22
2+
3+
WORKDIR /app
4+
5+
COPY go.mod go.sum ./
6+
COPY .env ./
7+
RUN go mod download && go mod verify
8+
9+
COPY . .
10+
RUN go build -v -o main .
11+
12+
EXPOSE 8000
13+
14+
CMD ["/app/main"]

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,34 @@ in front-end:
2727

2828
## How to run
2929

30-
In root of source you should run your go project
30+
🚦I included the .env file intentionally in the project so that you can see how the services work and with what settings. Otherwise, it should not be displayed in the project.
31+
32+
With Docker🐳:
33+
34+
1-Run DB and API service
35+
```bash
36+
docker composer up -d
37+
```
38+
2- Run nextjs
39+
```bash
40+
cd ./frontend
41+
yarn install
42+
yarn run dev
43+
```
44+
45+
Manaul✌️:
46+
47+
1-Create DB based on .env file(you can change it in .env file)
48+
49+
2-In root of source you should run your go project
3150
```bash
51+
go mod tidy
3252
go run main.go
3353
```
34-
In root frontend directory you should run your next project
54+
3-In root frontend directory you should run your next project
3555
```bash
36-
npm install
37-
npm run dev
56+
yarn install
57+
yarn run dev
3858
```
3959

4060
## API Reference
@@ -60,7 +80,8 @@ This section tracks the progress of implemented features in project.
6080

6181
- [x] Add swagger to project.
6282
- [x] Add Next-auth to project -> frontend directory.
63-
- [ ] Add all api response states(success,failure).
83+
- [x] Add all api response states(success,failure).
84+
- [x] Dockerize project.
6485

6586
## Screenshots
6687

docker-compose.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
services:
2+
# ⚠️Next auth error in login api handler. so i decided to comment it for now
3+
# client:
4+
# container_name: ngca_client
5+
# build:
6+
# context: ./frontend/
7+
# dockerfile: Dockerfile
8+
# image: ngca_client
9+
# ports:
10+
# - "${APP_PORT}:3000"
11+
# environment:
12+
# NEXTAUTH_URL: http://localhost:3000
13+
# AUTH_URL: http://localhost:3000
14+
api:
15+
container_name: ngca_server
16+
build:
17+
context: .
18+
dockerfile: Dockerfile
19+
ports:
20+
- ${API_PORT}:8000
21+
env_file: ".env"
22+
depends_on:
23+
postgres:
24+
condition: service_healthy
25+
restart: true
26+
postgres:
27+
container_name: ngca_db
28+
ports:
29+
- ${DB_PORT}:5432
30+
image: postgres:latest
31+
healthcheck:
32+
test: [ "CMD-SHELL", "pg_isready -U ${DB_USER} -d ${DB_NAME}" ]
33+
interval: 10s
34+
retries: 5
35+
start_period: 30s
36+
timeout: 10s
37+
environment:
38+
- POSTGRES_USER=${DB_USER}
39+
- POSTGRES_PASSWORD=${DB_PASS}
40+
- POSTGRES_DB=${DB_NAME}

frontend/.env

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,16 @@
1-
NEXTAUTH_SECRET=iuehieuher7v347834geuygwyu32gf783gfuwegysduycgwgf783tf3gf
1+
NEXTAUTH_SECRET=iuehieuher7v347834geuygwyu32gf783gfuwegysduycgwgf783tf3gfX
2+
NEXTAUTH_URL=http://localhost:3000
3+
DB_HOST=postgres
4+
DB_PORT=5432
5+
DB_PASS=postgres
6+
DB_USER=postgres
7+
DB_SSLMODE=disable
8+
DB_NAME=GoNextJsCrud
9+
SECRET_JWT=iuehieuher7v347834geuygwyu32gf783gfuwegysduycgwgf783tf3gf
10+
11+
# api app configuration
12+
API_PORT=8000
13+
API_HOST=localhost
14+
15+
# client PORT
16+
APP_PORT=3000

frontend/Dockerfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
FROM focker.ir/node:lts-alpine
2+
3+
# تنظیم دایرکتوری کاری
4+
WORKDIR /app
5+
6+
# کپی فایل‌های پکیج قبل از بقیه فایل‌ها برای بهینه‌سازی کش
7+
COPY package.json yarn.lock ./
8+
9+
# نصب وابستگی‌ها
10+
RUN yarn install
11+
12+
# کپی فایل env
13+
COPY .env .env
14+
15+
# کپی باقی فایل‌ها
16+
COPY . .
17+
18+
# ساخت پروژه
19+
RUN yarn build
20+
21+
# تنظیم پورت
22+
EXPOSE 3000
23+
24+
# اجرای پروژه
25+
CMD ["yarn", "start"]

frontend/config/site.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SiteConfig } from "../types/index";
22

33
export const siteConfig: SiteConfig = {
4-
localApi: "http://127.0.0.1:8080/api",
4+
localApi: "http://127.0.0.1:8000/api",
55
};

frontend/lib/auth.ts

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,76 @@
1-
import { getServerSession, NextAuthOptions } from "next-auth"
1+
import { getServerSession, NextAuthOptions } from "next-auth";
22
import CredentialsProvider from "next-auth/providers/credentials";
33

44
const authOptions: NextAuthOptions = {
5-
session: {
6-
strategy: "jwt",
7-
},
8-
providers: [
9-
// EmailProvider
10-
// CustomProvider
11-
CredentialsProvider({
12-
name: "Credentials",
13-
14-
credentials: {
15-
username: { label: "Username", type: "text", placeholder: "jsmith" },
16-
password: { label: "Password", type: "password" },
17-
},
18-
async authorize(credentials, req) {
19-
// Add logic here to look up the user from the credentials supplied
20-
21-
const res = await fetch("http://localhost:8080/api/login", {
5+
session: {
6+
strategy: "jwt",
7+
},
8+
providers: [
9+
CredentialsProvider({
10+
name: "Credentials",
11+
credentials: {
12+
username: { label: "Username", type: "text", placeholder: "jsmith" },
13+
password: { label: "Password", type: "password" },
14+
},
15+
async authorize(credentials) {
16+
if (!credentials?.username || !credentials?.password) {
17+
// اگر ورودی خالی است خطا می‌دهیم
18+
throw new Error("Username and password are required");
19+
}
20+
21+
try {
22+
const res = await fetch("http://localhost:8000/api/login", {
2223
method: "POST",
2324
headers: {
2425
"Content-Type": "application/json",
2526
},
2627
body: JSON.stringify({
27-
username: credentials?.username,
28-
password: credentials?.password,
28+
username: credentials.username,
29+
password: credentials.password,
2930
}),
3031
});
32+
33+
// اگر درخواست ناموفق بود، خطا می‌دهیم
34+
if (!res.ok) {
35+
const errorData = await res.json();
36+
throw new Error(errorData?.message || "Invalid credentials");
37+
}
38+
3139
const user = await res.json();
32-
console.log("this is user",user);
33-
console.log("this is req",req);
34-
35-
36-
if (user) {
37-
return user;
38-
} else {
39-
return null;
40+
if (user && user.token) {
41+
return user; // برگشت دادن یوزر به NextAuth
4042
}
41-
},
42-
}),
43-
],
44-
callbacks: {
45-
async jwt({ token, user }) {
46-
return { ...token, ...user };
47-
},
48-
async session({ session, token, user }) {
49-
console.log("sd",user);
50-
51-
session.user = token as any;
52-
return session;
43+
44+
// در صورتی که توکن نداشته باشد، null برمی‌گرداند
45+
return null;
46+
} catch (error) {
47+
console.error("Authorization error:", error);
48+
// در صورتی که خطا داشتیم، null برگشت داده می‌شود که باعث جلوگیری از هدایت به صفحه خطا می‌شود
49+
return null;
50+
}
5351
},
52+
}),
53+
],
54+
callbacks: {
55+
async jwt({ token, user }) {
56+
if (user) {
57+
token = { ...token, ...user }; // اطلاعات کاربر به توکن اضافه می‌شود
58+
}
59+
return token;
5460
},
55-
pages: {
56-
signIn: "/auth/signin",
57-
// signOut: "/auth/signout",
58-
// error: "/auth/error",
59-
// verifyRequest: "/auth/verify-request",
60-
// newUser: "/auth/new-user",
61+
async session({ session, token }) {
62+
session.user = token as any; // اطلاعات توکن به سشن اضافه می‌شود
63+
return session;
6164
},
62-
debug: true,
63-
};
65+
},
66+
pages: {
67+
signIn: "/auth/signin", // صفحه لاگین به صورت اختصاصی
68+
// این بخش را برای جلوگیری از هدایت به صفحه خطا خالی نگه داریم
69+
error: "/auth/signin", // به این صورت، در صورت خطا به صفحه لاگین هدایت می‌شود
70+
},
71+
debug: true,
72+
};
6473

65-
/**
66-
* Helper function to get the session on the server without having to import the authOptions object every single time
67-
* @returns The session object or null
68-
*/
69-
const getSession = () => getServerSession(authOptions)
74+
const getSession = () => getServerSession(authOptions);
7075

71-
export { authOptions, getSession }
76+
export { authOptions, getSession };

0 commit comments

Comments
 (0)