Skip to content

Map-N-Merge/XueEr-Backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

25 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

XueEr Backend

A modern Go backend with Gin framework, Firebase Firestore, and comprehensive API development.

πŸš€ Tech Stack

  • Framework: Gin (Go web framework)
  • Database: Firebase Admin SDK (Firestore)
  • Environment: godotenv
  • Code Quality: go fmt, go vet
  • Development: Hot reload with air (optional)
  • Security: CORS middleware + secure headers

πŸ“‹ Prerequisites

  • Go >= 1.19
  • Firebase project with service account credentials

πŸ› οΈ Installation

  1. Clone the repository:
git clone <your-repo-url>
cd XueEr/backend
  1. Install dependencies:
go mod tidy
  1. Set up environment variables:
cp .env.example .env
  1. Configure your Firebase service account credentials in credentials/
  2. You can download credentials from the GCP Console.(IAM & Admin -> Service Accounts -> <Your Service Account> -> Keys -> Add Key -> Create New Key -> JSON)
  3. Then place the downloaded JSON file in the credentials/ directory.
  4. Change the path in .env if necessary.(default is credentials/firebase-credentials.json)

πŸ”§ Development

Start Firestore emulator (optional):

firebase emulators:start

Start the development server:

go run src/main.go

The server will start on http://localhost:3000 by default.

πŸ§ͺ Testing

Not implemented yet.

πŸ—οΈ Build & Production

Build the project:

go build -o bin/main src/main.go

Start the production server:

./bin/main

πŸ§ͺ Code Quality

Format Go code:

go fmt ./...

Check for issues:

go vet ./...

πŸ“‘ API Endpoints

Courses

  • GET /courses - Get courses with pagination and sorting
    • Query params:
      • page: Page number (default: 1)
      • pageSize: Items per page (default: 10)
      • sort: Sort field (e.g., CourseYear:desc, CourseNameZh:asc)

Test Endpoints (Development Only)

These endpoints are commented out in production:

  • POST /courses - Add multiple courses
  • PATCH /course/:id - Update a course partially

πŸ” Environment Variables

  • PORT: Server port (default: 3000)
  • APP_ENV: Application environment (default: test)
  • FIRESTORE_EMULATOR: Firestore emulator flag (default: true)
  • CORS_ORIGINS: Allowed CORS origins (comma-separated, default: localhost:3000,localhost:5173)
  • FIREBASE_CREDENTIALS_PATH: Path to Firebase service account JSON file (default: credentials/firebase-credentials.json)
  • FIREBASE_PROJECT_ID: Firebase project ID (for development)
  • FIRESTORE_EMULATOR_HOST: Firestore emulator host (for development, e.g., localhost:8081, if using emulator)

πŸš€ Firebase Setup

  1. Create a Firebase project
  2. Generate a service account key
  3. Download the JSON file
  4. Place it in the credentials/ directory
  5. Update the FIREBASE_CREDENTIALS_PATH in .env if necessary

Firestore Usage

Access Firestore in Go:

import "XueEr-backend/src/lib/firestoreDB"

client := firestoreDB.FirestoreClient

Firestore Emulator (Development)

When you want to start using the Firestore emulator for local development, you will need to set the firebase.json and the following environment variable:

  • FIRESTORE_EMULATOR
    • set to true
  • FIRESTORE_EMULATOR_HOST
    • set to localhost:8081 (or your configured host and port in firebase.json)

Then start the emulator with:

firebase emulators:start --project your-project-id

The your-project-id should match the FIREBASE_PROJECT_ID in your .env file.

πŸ“ Project Structure

backend/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.go                 # Server entry point
β”‚   β”œβ”€β”€ lib/
β”‚   β”‚   β”œβ”€β”€ courses/
β”‚   β”‚   β”‚   └── ReadCourses.go  # GET /courses handler
β”‚   β”‚   β”œβ”€β”€ firestoreDB/
β”‚   β”‚   β”‚   └── InitFirestore.go # Firebase initialization
β”‚   β”‚   β”œβ”€β”€ schema/
β”‚   β”‚   β”‚   β”œβ”€β”€ Course.go       # Course data structures
β”‚   β”‚   β”‚   └── ClassSchedule.go
β”‚   β”‚   └── testOnly/
β”‚   β”‚       β”œβ”€β”€ CourseTestAPI.go # Test API handlers
β”‚   β”‚       └── firestoreEmulator.go
β”‚   └── utils/
β”‚       └── logger.ts
β”œβ”€β”€ credentials/               # Firebase credentials (gitignored)
β”‚   └── firebase-credentials.json # Firebase service account
β”œβ”€β”€ go.mod                      # Go dependencies
β”œβ”€β”€ go.sum                      # Dependency checksums
└── README.md

πŸ”’ Security Features

  • CORS: Configurable cross-origin resource sharing
  • Secure Headers: Frame deny, XSS protection, content type sniffing prevention
  • Input Validation: Schema validation with regex checks for course times

πŸš€ Deployment

Local Development

go run src/main.go

Docker Deployment

docker build -t xueer-backend .
docker run -p 3000:3000 xueer-backend

Cloud Run Deployment

  1. Build and push to Google Container Registry
  2. Deploy to Cloud Run with Firebase service account

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

The pre-commit hooks will automatically run Go code quality checks (formatting, vetting, and dependency cleanup).

πŸ“„ License

This project is licensed under the ISC License. XueEr Backend Service

About

XueEr Backend Service

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors