This repository contains the backend code for the Agency-Type application. It provides a RESTful API to handle user authentication, results management, and records retrieval. The backend is built using Go, with the Echo framework for handling HTTP requests, and PostgreSQL for database operations.
- User Authentication: Register and login endpoints with JWT token generation.
- Results Management: Create, retrieve, and manage user results.
- Records Retrieval: Fetch global and user-specific records based on game modes.
- Go: The primary programming language.
- Echo: A high performance, extensible, and minimalist web framework for Go.
- PostgreSQL: A powerful, open source object-relational database system.
- JWT: JSON Web Tokens for secure authentication.
- bcrypt: For password hashing.
- Go (version 1.16 or higher)
- PostgreSQL (version 10 or higher)
- Git
-
Clone the repository:
git clone https://github.com/your-repo/agency-type-backend.git cd agency-type-backend -
Set up the environment variables:
Create a
.envfile in the root directory and add the following:DATABASE_URL=postgres://postgres:PASSWORD@localhost:5432/postgres?sslmode=disable SECRET_TOKEN="TOKEN"
-
Install dependencies:
go mod download
-
Install Goose for database migrations:
go install github.com/pressly/goose/v3/cmd/goose@latest
-
Run the database migrations:
goose -dir .\internal\database\migrations postgres "postgres://postgres:PASSWORD@localhost:5432/postgres" up
-
Run the application:
cd .\cmd\server go run .
The server will start running on
http://localhost:8080.
-
POST /register - Register a new user.
Request
{ "login": string, "password": string }
Response
{ "message": "User registered successfully", "token": string "user_id": int }
-
POST /login - Login an existing user.
Request
{ "login": string, "password": string }
Response
{ "message": "Login successful", "token": string "user_id": int }
-
POST /results - Create a new result (requires authentication).
Request
header
Key Value token string { "game_mode": string, "duration": float, "mistakes": int, "accuracy": float, "count_words": int, "wpm": float, "cpm": float }
Response
{ "message": "Result created successfully" }
-
GET /results/ - Get all results for a user.
Request
Query Params
Key Value user_id int Response
[ { "id": int, "user_id": int, "game_mode": string, "start_time": time.Time, "duration": float, "mistakes": int, "accuracy": float, "count_words": int, "wpm": float, "cpm": float } ]
-
GET /results/:id - Get a specific result by ID.
Response
[ { "id": int, "user_id": int, "game_mode": string, "start_time": time.Time, "duration": float, "mistakes": int, "accuracy": float, "count_words": int, "wpm": float, "cpm": float } ]
-
GET /records/:gamemode - Get global records for a specific game mode.
Response
[ { "id": int, "user_id": int, "game_mode": string, "start_time": time.Time, "duration": float, "mistakes": int, "accuracy": float, "count_words": int, "wpm": float, "cpm": float } ]
-
GET /records - Get user-specific records.
Request
Query Params
Key Value user_id int Response
[ { "id": int, "user_id": int, "game_mode": string, "start_time": time.Time, "duration": float, "mistakes": int, "accuracy": float, "count_words": int, "wpm": float, "cpm": float } ]