diff --git a/.dockerignore b/.dockerignore index 069a924..4222f42 100644 --- a/.dockerignore +++ b/.dockerignore @@ -19,3 +19,7 @@ node_modules .vscode .github .git +db_username.txt +db_password.txt +redis_password.txt +typesense_api_key.txt diff --git a/.gitignore b/.gitignore index 4e30353..80bda74 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,7 @@ composer.phar /coverage .DS_Store *.cache +db_username.txt +db_password.txt +redis_password.txt +typesense_api_key.txt diff --git a/.rr.yaml b/.rr.yaml index b5d4e79..0f9a29e 100644 --- a/.rr.yaml +++ b/.rr.yaml @@ -61,7 +61,7 @@ logs: # we want to use docker's log drivers, so push logs to stdout output: stdout - # we to use docker's log drivers, so push error logs to stdout + # we want to use docker's log drivers, so push error logs to stdout # this way it is possible for example to pipe logs to journald or to AWS Cloudwatch err_output: stdout diff --git a/container-setup.sh b/container-setup.sh new file mode 100644 index 0000000..17d2376 --- /dev/null +++ b/container-setup.sh @@ -0,0 +1,75 @@ +#!/bin/bash + +JIKAN_API_VERSION=v4.0.0-rc.11 +DOCKER_COMPOSE_PROJECT_NAME=jikan-api-$JIKAN_API_VERSION + +display_help() { + echo "============================================================" + echo "Jikan API Container Setup CLI" + echo "============================================================" + echo "Syntax: ./container-setup.sh [command]" + echo "Jikan API Version: $JIKAN_API_VERSION" + echo "---commands---" + echo "help Print CLI help" + echo "build-image Build Image Locally" + echo "start Start Jikan API (mongodb, typesense, redis, jikan-api workers)" + echo "validate-prereqs Validate pre-reqs installed (docker, docker-compose)" + echo "execute-indexers Execute the indexers, which will scrape and index data from MAL. (Notice: This can take days)" + echo "" +} + +validate_prereqs() { + docker -v >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo -e "'docker' is not installed or not runnable without sudo. \xE2\x9D\x8C" + else + echo -e "Docker is Installed. \xE2\x9C\x94" + fi + + docker-compose -v >/dev/null 2>&1 + if [ $? -ne 0 ]; then + echo -e "'docker-compose' is not installed. \xE2\x9D\x8C" + else + echo -e "Docker compose is Installed. \xE2\x9C\x94" + fi +} + +build_image() { + docker build --rm --compress -t jikanme/jikan-rest:$JIKAN_API_VERSION . +} + +start() { + docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME up -d +} + +case "$1" in + "help") + display_help + ;; + "validate-prereqs") + validate_prereqs + ;; + "build-image") + build_image + ;; + "start") + start + ;; + "execute-indexers") + echo "Indexing anime..." + docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:anime + echo "Indexing manga..." + docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:manga + echo "Indexing characters and people..." + docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:common + echo "Indexing genres..." + docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:genres + echo "Indexing producers..." + docker-compose -p $DOCKER_COMPOSE_PROJECT_NAME exec jikan_rest php /app/artisan indexer:producers + echo "Indexing done!" + ;; + *) + echo "No command specified, displaying help" + display_help + ;; +esac diff --git a/docker-compose.yml b/docker-compose.yml index 8fdacb3..8ebe53c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,12 @@ volumes: redis-data: { } typesense-data: { } +networks: + jikan_network: { } + secrets: + db_username: + file: db_username.txt db_password: file: db_password.txt redis_password: @@ -13,48 +18,30 @@ secrets: file: typesense_api_key.txt services: - jikan_rest: &jikan_rest - image: jikanme/jikan-rest:latest + jikan_rest: + image: jikanme/jikan-rest:v4.0.0-rc.11 user: "${APP_UID:-10001}:${APP_GID:-10001}" + networks: + - jikan_network secrets: + - db_username - db_password - typesense_api_key + - redis_password environment: PS1: '\[\033[1;32m\]\[\033[1;36m\][\u@\h] \[\033[1;34m\]\w\[\033[0;35m\] \[\033[1;36m\]# \[\033[0m\]' - APP_DEBUG: 'false' - APP_ENV: production - CACHING: 'true' - CACHE_DRIVER: redis - REDIS_HOST: redis - REDIS_PASSWORD__FILE: /run/secrets/redis_password - DB_CONNECTION: mongodb - DB_HOST: mongodb - DB_DATABASE: jikan - DB_PORT: 27017 - DB_ADMIN: "${DB_USERNAME:-root}" - DB_USERNAME: "${DB_USERNAME:-root}" - SCOUT_DRIVER: typesense - SCOUT_QUEUE: 'false' - DB_PASSWORD__FILE: /run/secrets/db_password - TYPESENSE_HOST: typesense - TYPESENSE_PORT: 8108 - TYPESENSE_API_KEY__FILE: /run/secrets/typesense_api_key - CORS_MIDDLEWARE: 'true' - MICROCACHING: 'true' - MICROCACHING_EXPIRE: 60 - depends_on: - mongodb: { condition: service_healthy } - redis: { condition: service_healthy } - typesense: { condition: service_healthy } - - web: - <<: *jikan_rest + env_file: + - ./docker/config/.env.compose ports: - '8080:8080/tcp' healthcheck: test: [ 'CMD-SHELL', 'wget --spider -q "http://127.0.0.1:2114/health?plugin=http"' ] interval: 2s timeout: 2s + depends_on: + mongodb: { condition: service_healthy } + redis: { condition: service_healthy } + typesense: { condition: service_healthy } mongodb: image: mongo:focal @@ -63,6 +50,8 @@ services: ports: - '27017/tcp' command: --wiredTigerCacheSizeGB ${MONGO_CACHE_SIZE_GB:1} + networks: + - jikan_network secrets: - db_password environment: @@ -80,6 +69,8 @@ services: - redis_password environment: REDIS_PASSWORD: "${REDIS_PASSWORD:-null}" + networks: + - jikan_network command: - /bin/sh - -c @@ -98,6 +89,8 @@ services: entrypoint: /bin/sh secrets: - typesense_api_key + networks: + - jikan_network command: - -c - TYPESENSE_API_KEY="$$(cat /run/secrets/typesense_api_key)" /opt/typesense-server --data-dir /data diff --git a/docker/config/.env.compose b/docker/config/.env.compose new file mode 100644 index 0000000..193d453 --- /dev/null +++ b/docker/config/.env.compose @@ -0,0 +1,21 @@ +APP_DEBUG=false +LOG_LEVEL=info +APP_ENV=production +CACHING=true +CACHE_DRIVER=redis +REDIS_HOST=redis +REDIS_PASSWORD__FILE=/run/secrets/redis_password +DB_CONNECTION=mongodb +DB_HOST=mongodb +DB_DATABASE=jikan +DB_USERNAME__FILE=/run/secrets/db_username +DB_ADMIN__FILE=/run/secrets/db_username +DB_PASSWORD__FILE=/run/secrets/db_password +SCOUT_DRIVER=typesense +SCOUT_QUEUE=false +TYPESENSE_HOST=typesense +TYPESENSE_PORT=8108 +TYPESENSE_API_KEY__FILE=/run/secrets/typesense_api_key +CORS_MIDDLEWARE=true +MICROCACHING=true +MICROCACHING_EXPIRE=60