Subspace is a simple chat application backend built with PHP and the Slim framework. It allows users to create chat groups, join groups, send messages, and list messages within a group.
- Create chat groups
- Join existing chat groups
- Send messages to groups
- List messages in a group
- SQLite database for data storage
- RESTful JSON API
- PHP 7.4 or newer
- Composer
- SQLite 3
-
Clone the repository:
git clone https://github.com/sencerburak/subspace-backend.git cd subspace-backend
-
Install dependencies:
composer install
-
Set up the environment:
cp .env.example .env
Edit the
.env
file and set the appropriate values for your environment. -
Initialize the database:
php db_setup.php
-
Start the PHP development server:
composer start
The application should now be running at http://localhost:8080
.
The application follows a layered architecture to separate concerns and improve maintainability:
- Routing Layer: Implemented using Slim framework, defining API endpoints and HTTP methods.
- Controller Layer: Handles incoming requests, processes data, and returns responses.
- Service Layer: Contains business logic, orchestrates operations between repositories.
- Repository Layer: Manages data persistence and retrieval from the SQLite database.
- Model Layer: Defines data structures and entities used throughout the application.
-
User Management: Users are identified by a unique ID, which is used as a simple authentication mechanism for API requests.
-
Chat Groups: Public groups that any user can create or join. Groups are identified by a unique ID.
-
Messaging: Users can send messages within groups they've joined. Messages are associated with a user and a group.
-
Database: SQLite is used for simplicity and ease of setup. The database schema includes tables for users, chat_groups, user_group associations, and messages.
-
API Endpoints:
POST /users
: Create a new userGET /users
: List all usersGET /users/{id}
: Get a specific userPOST /chat-groups
: Create a new chat groupGET /chat-groups
: List all chat groupsPOST /chat-groups/{id}/join
: Join a chat groupPOST /chat-groups/{id}/messages
: Send a message to a groupGET /chat-groups/{id}/messages
: List messages in a group
All endpoints that require user identification should include a
User-Id
header in the request. -
Testing: PHPUnit is used for unit and integration testing, ensuring the reliability and correctness of the implemented features.
curl -X POST http://localhost:8080/users \
-H "Content-Type: application/json" \
-d '{"username": "johndoe"}'
curl -X GET http://localhost:8080/users \
-H "User-Id: 1"
curl -X GET http://localhost:8080/users/1 \
-H "User-Id: 1"
curl -X POST http://localhost:8080/chat-groups \
-H "Content-Type: application/json" \
-H "User-Id: 1" \
-d '{"name": "General Chat"}'
curl -X GET http://localhost:8080/chat-groups \
-H "User-Id: 1"
curl -X POST http://localhost:8080/chat-groups/1/join \
-H "User-Id: 1"
curl -X POST http://localhost:8080/chat-groups/1/messages \
-H "Content-Type: application/json" \
-H "User-Id: 1" \
-d '{"content": "Hello, everyone!"}'
curl -X GET http://localhost:8080/chat-groups/1/messages \
-H "User-Id: 1"
Note: Replace localhost:8080
with the appropriate host and port if your server is running on a different address. Also, make sure to replace the User-Id with a valid user ID when making requests.
To run the test suite, use the following command:
composer test