Here's a basic README for the ReferralAPI project on GitHub. It includes an overview, installation instructions, configuration details, and usage examples to get started with the API.
ReferralAPI is a referral link management service built with Golang and Gin. It allows users to create accounts, log in, and manage referrals by generating and validating unique referral codes.
- User Registration: Standard sign-up and referral-based sign-up.
- Login Authentication: Secure login with JWT-based token generation.
- Referral Code Management: Generate, retrieve, and delete unique referral codes.
- Documentation: Swagger documentation for API endpoints.
- Go: Core language
- Gin: Web framework for building RESTful APIs
- GORM: ORM for database interaction
- Swaggo: Generate and serve Swagger documentation
- JWT: For user authentication
- MySQL: Relational database for storing user and referral data
Make sure you have the following installed:
-
Clone the repository:
git clone https://github.com/left-try/ReferalAPI.git cd ReferalAPI -
Install dependencies:
go mod tidy
-
Set up MySQL database:
Create a new MySQL database for the project and configure the database connection in
database/config.go:const ( DBUser = "your_mysql_username" DBPassword = "your_mysql_password" DBName = "referral_db" )
-
Run database migrations:
Run the necessary migrations to set up the database schema.
-
Generate Swagger Documentation:
Install swaggo CLI and generate the Swagger documentation:
go install github.com/swaggo/swag/cmd/swag@latest swag init
-
Run the API server:
go run main.go
The API will start on
localhost:8000.
Swagger documentation is available at:
http://localhost:8000/docs/index.html
- Endpoint:
POST /signup - Description: Registers a new user.
- Request Body:
{ "email": "user@example.com", "password": "userpassword" } - Response:
{ "message": "User created", "user": { "id": 1, "email": "user@example.com", "referrerId": null } }
- Endpoint:
POST /login - Description: Logs in a user and returns a JWT token.
- Request Body:
{ "email": "user@example.com", "password": "userpassword" } - Response:
{ "message": "User logged in", "token": "jwt-token-string" }
- Endpoint:
POST /create_code - Description: Generates a new referral code for a user.
- Request Body:
{ "code": "REF123" } - Response:
{ "code": "REF123" }
- Endpoint:
DELETE /delete_code/:code - Description: Deletes a specific referral code.
- Response:
{ "message": "Code deleted successfully" }
- Run Server: Start up the server as shown higher
- Run Tests: Manually run http request located in the
testsfolder