A Go implementation of the textbook management and Gemini AI integration API using Gin framework and MongoDB.
- Textbook Management: Create and manage textbooks with detailed table of contents
- Google Gemini AI Integration: Query AI with specific textbook sections for educational assistance
- Role-based Authentication: JWT-based authentication with teacher and student roles
- MongoDB Integration: Efficient document storage and retrieval
- RESTful API: Clean and consistent API design with proper HTTP status codes
- Go 1.21 or higher
- MongoDB running locally or connection string
- Google Gemini API key
- Clone and setup:
cd go-api
go mod tidy
- Environment Setup:
cp .env.example .env
# Edit .env file with your configuration
- Run the server:
go run main.go
The server will start on http://localhost:8080
POST /api/v1/auth/register
Content-Type: application/json
{
"email": "teacher@example.com",
"full_name": "John Doe",
"password": "password123",
"role": "teacher"
}
POST /api/v1/auth/login
Content-Type: application/json
{
"email": "teacher@example.com",
"password": "password123"
}
POST /api/v1/teacher/textbooks
Authorization: Bearer <jwt_token>
Content-Type: application/json
{
"class": 10,
"subject": "Science",
"medium": "English",
"chapters": [
{
"chapter_number": 1,
"title": "Chemical Reactions and Equations",
"sections": [
"1.1 Chemical Equation",
"1.2 Balanced Chemical Equation"
]
}
]
}
GET /api/v1/teacher/textbooks
Authorization: Bearer <jwt_token>
GET /api/v1/teacher/textbooks/{textbook_id}
Authorization: Bearer <jwt_token>
GET /api/v1/common/textbooks
Authorization: Bearer <jwt_token>
GET /api/v1/common/textbooks/{textbook_id}
Authorization: Bearer <jwt_token>
POST /api/v1/common/textbooks/query-sections
Authorization: Bearer <jwt_token>
Content-Type: application/json
{
"textbook_id": "textbook_id_here",
"selected_sections": [
"1.1 Chemical Equation",
"1.2 Balanced Chemical Equation"
],
"custom_prompt": "Explain the difference between chemical equations and balanced equations for Class 10 students"
}
go-api/
├── main.go # Application entry point
├── go.mod # Go module definition
├── go.sum # Go module checksums
├── .env.example # Environment variables template
├── sample_textbook_data.json # Sample data for testing
├── README.md # This file
├── internal/ # Private application code
│ ├── config/ # Configuration management
│ │ └── config.go
│ ├── database/ # Database connection
│ │ └── database.go
│ ├── handlers/ # HTTP request handlers
│ │ ├── auth.go
│ │ └── textbook.go
│ ├── middleware/ # HTTP middleware
│ │ └── auth.go
│ ├── models/ # Data models
│ │ ├── textbook.go
│ │ └── user.go
│ └── services/ # Business logic
│ └── gemini.go
└── pkg/ # Public utility packages
└── utils/
└── auth.go
Environment variables:
Variable | Description | Default |
---|---|---|
PORT |
Server port | 8080 |
ENVIRONMENT |
Environment mode | development |
MONGODB_URL |
MongoDB connection string | mongodb://localhost:27017 |
MONGODB_DB_NAME |
MongoDB database name | clarity_education |
SECRET_KEY |
JWT secret key | dev_secret_key_123_change_this_in_production |
ACCESS_TOKEN_EXPIRE_MINUTES |
JWT token expiration | 30 |
GEMINI_API_KEY |
Google Gemini API key | (required for AI features) |
- Gin: HTTP web framework
- MongoDB Driver: Official MongoDB Go driver
- JWT: JWT token handling
- Bcrypt: Password hashing
- Godotenv: Environment variable loading
curl http://localhost:8080/health
curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"email": "teacher@example.com",
"full_name": "Test Teacher",
"password": "password123",
"role": "teacher"
}'
# Use the token from registration
curl -X POST http://localhost:8080/api/v1/teacher/textbooks \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-d @sample_textbook_data.json
- Go Gin instead of Python FastAPI
- Standard library JSON instead of Pydantic
- MongoDB Driver instead of Motor (async)
- Native HTTP client for Gemini API instead of google-generativeai library
- Compiled binary - faster startup and execution
- Built-in concurrency - better handling of concurrent requests
- Lower memory footprint - more efficient resource usage
- Static typing - compile-time error checking
- Same endpoints - identical API structure
- Same request/response format - compatible with existing clients
- Same authentication - JWT-based auth with same claims
- Same database schema - uses identical MongoDB collections
- Go to Google AI Studio
- Create a new API key
- Add it to your
.env
file asGEMINI_API_KEY=your_key_here
The API returns standard HTTP status codes:
200
- Success201
- Created400
- Bad Request (validation errors)401
- Unauthorized (authentication required)403
- Forbidden (insufficient permissions)404
- Not Found409
- Conflict (duplicate resource)500
- Internal Server Error503
- Service Unavailable (external service error)
- JWT Authentication - Secure token-based authentication
- Password Hashing - Bcrypt for secure password storage
- Role-based Access - Teacher/student role separation
- CORS Support - Configurable cross-origin resource sharing
- Input Validation - Request validation using Gin binding
- Set environment to production:
export ENVIRONMENT=production
- Use production MongoDB:
export MONGODB_URL=mongodb://your-production-mongo
- Build binary:
go build -o textbook-api main.go
- Run binary:
./textbook-api
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests
- Submit a pull request
This project is licensed under the MIT License.