A modern Go backend with Gin framework, Firebase Firestore, and comprehensive API development.
- 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
- Go >= 1.19
- Firebase project with service account credentials
- Clone the repository:
git clone <your-repo-url>
cd XueEr/backend- Install dependencies:
go mod tidy- Set up environment variables:
cp .env.example .env- Configure your Firebase service account credentials in
credentials/ - You can download credentials from the GCP Console.(
IAM & Admin -> Service Accounts -> <Your Service Account> -> Keys -> Add Key -> Create New Key -> JSON) - Then place the downloaded JSON file in the
credentials/directory. - Change the path in
.envif necessary.(default iscredentials/firebase-credentials.json)
Start Firestore emulator (optional):
firebase emulators:startStart the development server:
go run src/main.goThe server will start on http://localhost:3000 by default.
Not implemented yet.
Build the project:
go build -o bin/main src/main.goStart the production server:
./bin/mainFormat Go code:
go fmt ./...Check for issues:
go vet ./...- 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)
- Query params:
These endpoints are commented out in production:
- POST
/courses- Add multiple courses - PATCH
/course/:id- Update a course partially
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)
- Create a Firebase project
- Generate a service account key
- Download the JSON file
- Place it in the
credentials/directory - Update the
FIREBASE_CREDENTIALS_PATHin.envif necessary
Access Firestore in Go:
import "XueEr-backend/src/lib/firestoreDB"
client := firestoreDB.FirestoreClientWhen 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
- set to
FIRESTORE_EMULATOR_HOST- set to
localhost:8081(or your configured host and port infirebase.json)
- set to
Then start the emulator with:
firebase emulators:start --project your-project-idThe your-project-id should match the FIREBASE_PROJECT_ID in your .env file.
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
- 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
go run src/main.godocker build -t xueer-backend .
docker run -p 3000:3000 xueer-backend- Build and push to Google Container Registry
- Deploy to Cloud Run with Firebase service account
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
The pre-commit hooks will automatically run Go code quality checks (formatting, vetting, and dependency cleanup).
This project is licensed under the ISC License. XueEr Backend Service