Skip to content

Commit

Permalink
Refactored code
Browse files Browse the repository at this point in the history
  • Loading branch information
VampireAotD committed Jun 22, 2024
1 parent 9d0f220 commit f74dd55
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 319 deletions.
12 changes: 5 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
APP_PORT=9000
XDEBUG_MODE=coverage

SERVER_PORT=80
NGINX_LOGS_TO_LOGSTASH=false

NODE_PORT=5173

MYSQL_PORT=3306
MYSQL_DATABASE=anilibrary
MYSQL_USER=
MYSQL_PASSWORD=
MYSQL_ROOT_PASSWORD=
MYSQL_BACKUP_PATH=
DB_DATABASE=anilibrary
DB_PORT=3306
DB_USER=
DB_PASSWORD=
DB_ROOT_PASSWORD=

REDIS_PORT=6379
REDIS_PASSWORD=
123 changes: 41 additions & 82 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,74 +32,41 @@ To fully use Anilibrary you can also use one or all services that are related to
Before you start to work with Anilibrary, you need to fill all required environment variables which will be located in
**.env** and **src/.env**. To acquire them, you can use script **install.sh** or launch it by using `make install`.

### Required variables:

1. **MySQL:**

For proper MySQL database connectivity, the following required environment variables are needed:

- `MYSQL_USER` - MySQL database username with necessary privileges to access the database.
- `MYSQL_PASSWORD` - Password for the MySQL database user.
- `MYSQL_ROOT_PASSWORD` - Password for the MySQL root user.

2. **Redis:**

To enable Redis support, set the following required environment variables:

- `REDIS_PASSWORD` - Password for accessing the Redis server.

3. **Application:**

Anilibrary is written using Laravel, which also relies on some required environment variables which are located in
**src/.env**:

- `DB_HOST` - Hostname of MySQL, you can specify it in **docker-compose.yml** using **container_name** or **name of
the service** which is **database**.
- `DB_PORT` - Value of `MYSQL_PORT` from **.env** in the project root.
- `DB_DATABASE` - Value of `MYSQL_DATABASE` from **.env** in the project root.
- `DB_USERNAME` - Value of `MYSQL_USER` from **.env** in the project
root.
- `DB_PASSWORD` - Value of `MYSQL_PASSWORD` from **.env** in the project root.
- `REDIS_HOST` - Hostname of Redis, you can specify it in **docker-compose.yml** using **container_name** or **name
of the service** which is **redis**.
- `REDIS_PORT` - Value of `REDIS_PORT` from **.env** in the project root.
- `REDIS_PASSWORD` - Value of `REDIS_PASSWORD` from **.env** in the project root.

### Optional:

1. **MySQL:**

Some MySQL parameters can be set to default values, but you may customize them if needed:

- `MYSQL_DATABASE` - Sets the database name, by default it will be **anilibrary**.
- `MYSQL_PORT` - Sets the database port, by default it will be **3306**.
- `MYSQL_BACKUP_PATH` - If you want to backup your database you can specify path on where to store the backup
itself. For more convenience you can use script **backup.sh**, which is located in **scripts/mysql**.

2. **Redis:**

Additionally, you can use optional Redis environment variables:

- `REDIS_PORT` - Sets the Redis port, by default it will be **6379**.

3. **Application:**

In addition to the required variables, you can specify some variables that are optional, but needed to work fully
with Anilibrary:

- `XDEBUG_MODE` - If you want to enable **Xdebug** you can set this variable to **on**.
- `PUSHER_*` - If you want to use real-time notifications, specify your [Pusher](https://pusher.com) credentials.
- `TELEGRAM_*` - If you also want to have Anilibrary bot, specify your Telegram bot credentials.
- `TELEGRAM_WHITELIST` - Specify Telegram IDs who can work with bot.
- `CLOUDINARY_*` - All images are stored in **Cloudinary**, so if you want to use this storage, specify your
[Cloudinary](https://cloudinary.com) credentials.
- `JWT_SECRET` - If you want to use **Scraper**, you need to specify this variable here and in **Scraper** as well.
- `SCRAPER_URL` - Also, you need to specify it url if you want to use it.
- `LOGSTASH_ADDRESS` - If you want to send visualize your logs in Kibana, send them to **ELK** service using
Logstash, and specify it url here.
- `ELASTICSEARCH_*` - If you want to have proper search, specify your Elasticsearch credentials.

After configuration, you can use `make frontned-watch` to access website.
### Variables

#### For containers

This variables will be located in **.env** and are required for container to run properly.

| Variable | Default Value | Description |
|--------------------------|---------------|----------------------------------------------------------------------|
| `XDEBUG_MODE` | coverage | Used to set the mode for Xdebug. By default it is set to `coverage`. |
| `SERVER_PORT` | 80 | Specifies the port on which Nginx will run. |
| `NGINX_LOGS_TO_LOGSTASH` | false | Indicates whether Nginx logs should be sent to Logstash. |
| `NODE_PORT` | 5173 | Specifies the port on which the Vite server will run. |
| `DB_DATABASE` | anilibrary | The name of the database to be used. |
| `DB_PORT` | 3306 | Specifies the port for the database connection. |
| `DB_USER` | (empty) | The username for the database connection. |
| `DB_PASSWORD` | (empty) | The password for the database connection. |
| `DB_ROOT_PASSWORD` | (empty) | The root password for the database connection. |
| `REDIS_PORT` | 6379 | Specifies the port for the Redis connection. |
| `REDIS_PASSWORD` | (empty) | The password for the Redis connection. |

#### Variables for Anilibrary

This variables will be in **./src/.env** and are required for application to properly work with containers and other
services.

| Variable | Description |
|--------------------|-------------------------------------------------------------------------------------------------------------------------------|
| `DB_*` | Set of variables for database connection, use values from **.env** for containers. |
| `REDIS_*` | Set of variables for Redis connection, use values from **.env** for containers. |
| `TELEGRAM_*` | Set of variables for Telegram bot. |
| `CLOUDINARY_*` | Set of variables for Cloudinary storage. |
| `ELASTICSEARCH_*` | Set of variables for Elasticsearch, use with [monitoring microservice](https://github.com/VampireAotD/anilibrary-monitoring). |
| `JWT_SECRET` | Secret for communication with different Anilibrary microservices. |
| `SCRAPER_URL` | Url for scraper microservice. |
| `LOGSTASH_ADDRESS` | Url for Logstash receiver. |

---

Expand All @@ -109,7 +76,7 @@ After configuration, you can use `make frontned-watch` to access website.

For code quality you can use things like **Psalm**, **PHPStan** and **Laravel Pint**. You can use them directly in
container, or via Makefile,like this:
```shell
```sh
make phpstan
make psalm
make pint
Expand All @@ -122,29 +89,21 @@ After configuration, you can use `make frontned-watch` to access website.
2. **Nginx logs to ELK service**

If you want to visualize you Nginx logs in Kibana you can send them to Elasticsearch via Logstash. All of them are
configured in **ELK** service. To send logs you need to specify **APP_ENV** and **LOGSTASH_URL** for nginx container,
like this:
configured in **ELK** service. To send logs you need to set **NGINX_LOGS_TO_LOGSTASH** variable to **true** in
**.env** and specify **LOGSTASH_URL** for Nginx container, like this:

```diff
# compose.yml

nginx:
+ environment:
+ APP_ENV: production
+ LOGSTASH_URL: <your-url>
```

3. **Testing**

You can test your frontend and backend using these commands:
```shell
```sh
make test #for PHP tests
make frontend-test # for TS tests
```

---

## Known issues

1. Anilibrary is using [`Telebot`](https://github.com/westacks/telebot) library for managing bot, and due to it
limitations, some tests can require additional call of `fake()` method, or cannot be made because of `mock()` method,
more info in
this [`issue`](https://github.com/westacks/telebot/issues/58).
2 changes: 1 addition & 1 deletion compose.testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ services:

networks:
testing:
name: testing
name: anilibrary-testing
external: true
32 changes: 17 additions & 15 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ services:
context: docker/mysql
container_name: anilibrary-database
environment:
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_USER: ${DB_USER}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
MYSQL_AUTHENTICATION_PLUGIN: caching_sha2_password
healthcheck:
test: [ "CMD", "mysqladmin", "-u$MYSQL_USER", "-p$MYSQL_PASSWORD", "ping", "-h", "localhost" ]
interval: 2s
test: [ "CMD", "mysqladmin", "-u$DB_USER", "-p$DB_PASSWORD", "ping", "-h", "localhost" ]
interval: 10s
timeout: 5s
retries: 5
volumes:
- mysql-data:/var/lib/mysql:rw
- db-data:/var/lib/mysql:rw
ports:
- ${MYSQL_PORT}:3306
- ${DB_PORT}:3306
networks:
- local

Expand All @@ -27,11 +27,11 @@ services:
command: redis-server --requirepass ${REDIS_PASSWORD}
healthcheck:
test: [ "CMD-SHELL", "redis-cli -a $REDIS_PASSWORD ping | grep PONG" ]
interval: 2s
interval: 10s
timeout: 5s
retries: 5
volumes:
- anilibrary-redis-data:/data
- redis-data:/data
ports:
- ${REDIS_PORT}:6379
networks:
Expand Down Expand Up @@ -77,13 +77,15 @@ services:
- testing

networks:
local:
driver: bridge
anilibrary:
name: anilibrary
local:
name: anilibrary-local
testing:
name: testing
name: anilibrary-testing

volumes:
mysql-data:
anilibrary-redis-data:
db-data:
name: anilibrary-db-data
redis-data:
name: anilibrary-redis-data
2 changes: 1 addition & 1 deletion docker/php/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ EXPOSE 5173

ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]

HEALTHCHECK --start-period=5s --interval=2s --timeout=5s --retries=8 CMD php artisan octane:status || exit 1
HEALTHCHECK --start-period=5s --interval=10s --timeout=5s --retries=5 CMD php artisan octane:status || exit 1
11 changes: 9 additions & 2 deletions src/app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ class ProfileUpdateRequest extends FormRequest
public function rules(): array
{
return [
'name' => ['string', 'max:255'],
'email' => ['email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)],
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
Rule::unique(User::class)->ignore($this->user()->id),
],
];
}
}
38 changes: 38 additions & 0 deletions src/app/Providers/FakerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace App\Providers;

use Faker\Factory;
use Faker\Generator;
use Illuminate\Support\ServiceProvider;
use Tests\Helpers\Faker\Providers\AnimeInformationProvider;

class FakerServiceProvider extends ServiceProvider
{
/**
* Register services.
*/
public function register(): void
{
if ($this->app->environment('testing')) {
$this->app->singleton(Generator::class, function () {
$faker = Factory::create();
$faker->addProvider(new AnimeInformationProvider($faker));

return $faker;
});

$this->app->bind(Generator::class . ':' . config('app.faker_locale'), Generator::class);
}
}

/**
* Bootstrap services.
*/
public function boot(): void
{
//
}
}
1 change: 1 addition & 0 deletions src/bootstrap/providers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
App\Providers\AliasServiceProvider::class,
App\Providers\AppServiceProvider::class,
App\Providers\FacadeServiceProvider::class,
App\Providers\FakerServiceProvider::class,
App\Providers\HorizonServiceProvider::class,
App\Providers\MacroServiceProvider::class,
App\Providers\RepositoryServiceProvider::class,
Expand Down
14 changes: 7 additions & 7 deletions src/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
"library",
"laravel"
],
"license": "MIT",
"license": "BSL-1.0",
"require": {
"php": "^8.3",
"aws/aws-sdk-php": "^3.311",
"aws/aws-sdk-php": "^3.314",
"cloudinary-labs/cloudinary-laravel": "^2.1",
"elasticsearch/elasticsearch": "^8.13",
"elasticsearch/elasticsearch": "^8.14",
"firebase/php-jwt": "^6.10",
"guzzlehttp/guzzle": "^7.8",
"inertiajs/inertia-laravel": "^1.2",
"laravel/framework": "^11.10",
"inertiajs/inertia-laravel": "^1.3",
"laravel/framework": "^11.11",
"laravel/horizon": "^5.24",
"laravel/octane": "^2.4",
"laravel/sanctum": "^4.0",
Expand All @@ -34,7 +34,7 @@
"barryvdh/laravel-ide-helper": "^3.0",
"fakerphp/faker": "^1.23",
"larastan/larastan": "^2.9",
"laravel/breeze": "^2.0",
"laravel/breeze": "^2.1",
"laravel/pint": "^1.16",
"laravel/sail": "^1.29",
"mockery/mockery": "^1.6",
Expand All @@ -43,7 +43,7 @@
"phpunit/phpunit": "^10.5",
"psalm/plugin-laravel": "^2.11",
"spatie/laravel-ignition": "^2.7",
"vimeo/psalm": "^5.24"
"vimeo/psalm": "^5.25"
},
"autoload": {
"psr-4": {
Expand Down
Loading

0 comments on commit f74dd55

Please sign in to comment.