Skip to content

tranphuongkhoi/Backend

Repository files navigation

README - Lufian Backend

🚀 Mục Tiêu

Lufian là nền tảng kết nối con người qua tình cảm, bạn bè, công việc – được xây dựng bằng Golang, MongoDB, WebSocket với kiến trúc hướng clean architecture.


📁 Cấu Trúc Thư Mục Chuẩn

Backend/
├── cmd/
│   └── main.go
├── internal/
│   ├── api/
│   │   ├── handlers/
│   │   │   ├── admin_handler.go
│   │   │   ├── forgot_password_handler.go
│   │   │   ├── login_otp_handler.go
│   │   │   ├── match_handler.go
│   │   │   ├── match_ws_handler.go
│   │   │   ├── media_handler.go
│   │   │   ├── message_handler.go
│   │   │   ├── profile_handler.go
│   │   │   ├── subscription_handler.go
│   │   │   ├── user_handler.go
│   │   │   └── user_service_ext.go
│   │   ├── middleware/
│   │   │   ├── admin_only.go
│   │   │   ├── auth.go
│   │   │   ├── cors.go
│   │   │   ├── ensure_profile.go
│   │   │   ├── rate_limit.go
│   │   │   ├── RoleMiddleware.go
│   │   │   ├── session.go
│   │   │   └── user_context.go
│   │   ├── routes/
│   │   │   ├── admin_routes.go
│   │   │   ├── auth_routes.go
│   │   │   ├── match_routes.go
│   │   │   ├── match_ws_routes.go
│   │   │   ├── media_routes.go
│   │   │   ├── profile_routes.go
│   │   │   ├── subscription_routes.go
│   │   │   ├── user_routes.go
│   │   │   └── ws_routes.go
│   ├── config/
│   │   ├── cloud_config.go
│   │   ├── config.go
│   │   ├── database.go
│   │   ├── google_auth.go
│   │   ├── role_config.go
│   │   └── ws_config.go
│   ├── data/
│   │   ├── compatibility_loader.go
│   │   ├── mbti_compatibility.json
│   │   └── zodiac_compatibility.json
│   ├── models/
│   │   ├── chatroom.go
│   │   ├── client.go
│   │   ├── full_profile.go
│   │   ├── match.go
│   │   ├── message.go
│   │   ├── otp.go
│   │   ├── profile.go
│   │   ├── profile_career.go
│   │   ├── profile_insights.go
│   │   ├── profile_media.go
│   │   ├── session.go
│   │   ├── subscription.go
│   │   ├── swipe_action.go
│   │   └── user.go
│   ├── repositories/
│   │   ├── chat_repository.go
│   │   ├── match_repository.go
│   │   ├── message_repository.go
│   │   ├── otp_repository.go
│   │   ├── profile_career_repository.go
│   │   ├── profile_insights_repository.go
│   │   ├── profile_media_repository.go
│   │   ├── profile_repository.go
│   │   ├── session_repository.go
│   │   ├── subscription_repository.go
│   │   ├── swipe_tracker_repository.go
│   │   └── user_repository.go
│   ├── scheduler/
│   │   └── schedule.go
│   ├── services/
│   │   ├── auth_service.go
│   │   ├── chatroom_service.go
│   │   ├── email_service.go
│   │   ├── match_service.go
│   │   ├── media_service.go
│   │   ├── otp_service.go
│   │   ├── profile_service.go
│   │   ├── session_service.go
│   │   ├── subscription_service.go
│   │   ├── user_service.go
│   │   └── wsnotify/
│   │       ├── ws_hub.go
│   │       └── ws_push.go
│   └── utils/
│       ├── bson_utils.go
│       ├── compatibility_utils.go
│       ├── profile_utils.go
│       └── utils.go

🧠 Kiến Trúc & Design Patterns

🏗️ Creational Patterns

Pattern Mô tả
Singleton MongoDB, Cloudinary, Config – được khởi tạo 1 lần trong main.go
Factory Function NewUserService(...), NewMatchHandler(...) – tạo instance linh hoạt
Dependency Injection Inject repo → service → handler giúp dễ test & mở rộng

🧱 Structural Patterns

Pattern Mô tả
Interface Abstraction Interface cho mỗi repo/service → dễ mock/test

🔁 Behavioral Patterns

Pattern Mô tả
Service Layer Tách toàn bộ logic nghiệp vụ vào services/ – không dính HTTP
Repository Pattern Truy vấn Mongo tách riêng từng module → clean & testable
Strategy (Login) Tách login qua OTP hoặc Password thành 2 flow riêng
Observer (WebSocket) Dùng hub pattern (map[userID]conn) để push real-time event
Chain of Responsibility Chuỗi middleware theo thứ tự: Auth → Role → EnsureProfile → RateLimit

Lufian API Documentation (Full)

🔐 Lufian Auth API Documentation

Tài liệu chi tiết cho toàn bộ các API xác thực người dùng trong hệ thống Lufian. Bao gồm: đăng ký, đăng nhập (OTP/password), quản lý phiên (logout, logout-all), đổi mật khẩu, Google Login, quên mật khẩu.


🧾 1. Register

POST /register
Auth: ❌ Không yêu cầu
Header:

Content-Type: application/json

Body:

{
  "username": "john_doe",
  "email": "john@example.com",
  "password": "secret123",
  "confirmPassword": "secret123",
  "dateofbirth": "2000-01-01",
  "gender": "male"
}

Response:

{
  "message": "User registered successfully"
}

🔐 2. Login with Password

POST /login
Auth: ❌ Không yêu cầu
Header:

Content-Type: application/json
X-Device-Type: web | app

Body:

{
  "email": "john@example.com",
  "password": "secret123"
}

Response:

{
  "message": "Login successful",
  "access_token": "JWT string",
  "refresh_token": "JWT string",
  "user": {
    "id": "string",
    "username": "string",
    "email": "string"
  }
}

🔓 2.5 Logout

POST /user/auth/logout
Auth: ✅ Yêu cầu Access Token
Header:

Authorization: Bearer {{access_token}}

Body: (Không bắt buộc)

{}

Response:

{
  "message": "Logout successful"
}

🔓 2.6 Logout All Devices

POST /user/auth/logout-all
Auth: ✅ Yêu cầu Access Token
Header:

Authorization: Bearer {{access_token}}

Body: (Không bắt buộc)

{}

Response:

{
  "message": "Logged out from all devices"
}

📩 3. Request OTP for Login

POST /auth/login-otp/request
Auth: ❌ Không yêu cầu
Header:

Content-Type: application/json

Body:

{
  "email": "john@example.com"
}

Response:

{
  "message": "OTP sent to email"
}

🔐 4. Login with OTP

POST /auth/login-otp/verify
Auth: ❌ Không yêu cầu
Header:

Content-Type: application/json
X-Device-Type: web | app

Body:

{
  "email": "john@example.com",
  "otp": "123456"
}

Response:

{
  "message": "Login successful",
  "access_token": "JWT string",
  "refresh_token": "JWT string"
}

📧 5. Forgot Password

POST /auth/forgot-password
Auth: ❌ Không yêu cầu
Header:

Content-Type: application/json

Body:

{
  "email": "john@example.com"
}

Response:

{
  "message": "OTP sent to email"
}

🔁 6. Reset Password with OTP

POST /auth/reset-password
Auth: ❌ Không yêu cầu
Header:

Content-Type: application/json

Body:

{
  "email": "john@example.com",
  "otp": "123456",
  "new_password": "newpassword123"
}

Response:

{
  "message": "Password updated"
}

🔒 7. Change Password

PUT /user/change-password
Auth: ✅ Cần đăng nhập
Header:

Authorization: Bearer {{access_token}}
Content-Type: application/json

Body:

{
  "old_password": "secret123",
  "new_password": "newpass456"
}

Response:

{
  "message": "Password updated successfully"
}

🌐 8. Google Login Redirect

GET /auth/google/login
Auth: ❌ Không yêu cầu


🔁 9. Google Callback

GET /auth/google/callback
Auth: ❌ Không yêu cầu

👤 Lufian User API Documentation

Tài liệu API liên quan đến người dùng trong hệ thống Lufian.


✅ Check Email Existence

GET /check-email
Auth: ❌ Không yêu cầu

Query:

email=example@gmail.com

Response - Email chưa tồn tại (200 OK):

{
  "message": "Email is available"
}

Response - Email đã tồn tại (409 Conflict):

{
  "error": "Email already in use"
}

Response - Thiếu email (400 Bad Request):

{
  "error": "Email is required"
}

👤 Lufian Profile API Documentation

Tài liệu mô tả các API quản lý hồ sơ người dùng trong ứng dụng Lufian.


🔍 GET /user-profile/

Lấy thông tin hồ sơ người dùng

  • Method: GET
  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{test1_token}}

Response:

{
  "profile": { ... toàn bộ thông tin hồ sơ ... }
}

✏️ PUT /user-profile/

Cập nhật hồ sơ người dùng

  • Method: PUT
  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{test1_token}}

Body:

{
  "bio": "Cuộc sống là hành trình khám phá bản thân",
  "sexual_orientation": ["heterosexual"],
  "prefer_gender": "female",
  "prefer_match": ["friendship", "long-term", "serious_relationship"],
  "education": "Đại học Kinh tế Quốc dân",
  "education_level": "Bachelor",
  "height": 175,
  "weight": 65,
  "hobbies": ["travel", "guitar", "books", "coffee", "gym"],
  "interest": ["technology", "music", "startup", "philosophy", "meditation"],
  "religion": "none",
  "drinking_habits": "occasionally",
  "smoking_habits": "never",
  "children_preference": "maybe",
  "fitness_level": "medium",
  "languages": ["vietnamese", "english"],

  "career": {
    "job_title": "Software Engineer",
    "company": "VTC",
    "work_location": "Hybrid"
  },

  "insights": {
    "mbti": "INTP",
    "zodiac": "scorpio"
  },

  "privacy": {
    "show_age": true,
    "show_weight": false,
    "show_location": true
  },

  "location": {
    "latitude": 10.762622,
    "longitude": 106.660172
  }
}

Response:

{
  "message": "Profile updated successfully"
}

❌ DELETE /user-profile/

Xóa hồ sơ người dùng

  • Method: DELETE
  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{test1_token}}

Response:

{
  "message": "Profile deleted successfully"
}

🖼️ Lufian Media API Documentation

Tài liệu chi tiết các API quản lý media cá nhân: avatar, photo, video.


🧑‍🦱 Upload Avatar

POST user-profile/upload-avatar
(Thực tế định nghĩa API là vậy, nhưng nên hiểu là: upload avatar vào user-profile)

  • Header:
Authorization: Bearer {{test1_token}}
Content-Type: multipart/form-data
  • Body: (form-data)
    Key Type Mô tả
    file File Ảnh avatar

Response:

{
  "message": "Avatar uploaded successfully",
  "avatar_url": "https://res.cloudinary.com/.../profile_xxx.jpg"
}

🗑️ Delete Avatar

DELETE user-profile/delete-avatar
(Thực tế định nghĩa API là vậy, nhưng nên hiểu là: xóa avatar trong user-profile)

  • Header:
Authorization: Bearer {{test1_token}}

Response:

{
  "message": "Avatar deleted successfully"
}

🖼️ Upload Photo

POST media/upload-photo

  • Header:
Authorization: Bearer {{test1_token}}
Content-Type: multipart/form-data
  • Body: (form-data)
    Key Type Mô tả
    file File Ảnh upload

Response:

{
  "message": "Photo uploaded successfully",
  "photo_url": "https://res.cloudinary.com/.../photo_xyz.jpg"
}

🗑️ Delete Photo

DELETE media/delete-photo

  • Header:
Authorization: Bearer {{test1_token}}
Content-Type: application/json
  • Body:
{
  "photo_url": "https://res.cloudinary.com/.../photo_xyz.jpg"
}

Response:

{
  "message": "Photo deleted successfully"
}

🎥 Upload Video

POST media/upload-video

  • Header:
Authorization: Bearer {{test1_token}}
Content-Type: multipart/form-data
  • Body: (form-data)
    Key Type Mô tả
    file File Video file

Response:

{
  "message": "Video uploaded successfully",
  "video_url": "https://res.cloudinary.com/.../video_xyz.mp4"
}

🗑️ Delete Video

DELETE media/delete-video

  • Header:
Authorization: Bearer {{test1_token}}
Content-Type: application/json
  • Body:
{
  "video_url": "https://res.cloudinary.com/.../video_xyz.mp4"
}

Response:

{
  "message": "Video deleted successfully"
}

💬 Lufian Chat & WebSocket API Documentation

Tài liệu chi tiết cho toàn bộ API nhắn tin và kết nối realtime trong hệ thống Lufian.


📡 GET /chatrooms/:chatroom_id

Lấy thông tin chatroom theo ID

  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{access_token}}

Response:

{
  "chatroom_id": "abc123",
  "user1_id": "u1",
  "user2_id": "u2",
  "created_at": "2025-04-06T12:00:00Z"
}

📨 GET /chat/messages/:chatroom_id

Lấy danh sách tin nhắn theo chatroom

  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{access_token}}

Response:

[
  {
    "message_id": "msg1",
    "sender_id": "u1",
    "content": "Hi there",
    "sent_at": "2025-04-06T12:01:00Z"
  }
]

📝 POST /chat/messages

Gửi tin nhắn mới (qua REST)

  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{access_token}}
Content-Type: application/json
  • Body:
{
  "chatroom_id": "abc123",
  "sender_id": "u1",
  "content": "Hello via REST"
}
  • Response:
{
  "message_id": "msg2",
  "status": "sent"
}

❌ DELETE /chat/messages/:message_id

Xóa tin nhắn

  • Auth: ✅ Yêu cầu Access Token
  • Header:
Authorization: Bearer {{access_token}}
  • Response:
{
  "status": "deleted"
}

🔌 WebSocket: GET /ws/match

Lắng nghe sự kiện match mới (push chatroom_id)

  • Query: ?token={{access_token}}
  • Kết nối WebSocket:
ws://lufian.app/v1/ws/match?token={{access_token}}
  • Server gửi khi có match:
{
  "event": "match_success",
  "chatroom_id": "abc123"
}

🔌 WebSocket: GET /chat/ws/:chatroom_id

Giao tiếp realtime trong phòng chat

  • Query: ?token={{access_token}}
  • Kết nối WebSocket:
ws://lufian.app/v1/chat/ws/abc123?token={{access_token}}

📤 Client gửi:

{
  "type": "message",
  "content": "Hello"
}

📥 Server phản hồi:

{
  "type": "message",
  "sender_id": "u123",
  "content": "Hello",
  "message_id": "msg456",
  "sent_at": "2025-04-06T13:00:00Z"
}

Các endpoint WebSocket yêu cầu JWT hợp lệ. Nếu không có hoặc sai sẽ bị từ chối kết nối.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors