Skip to content

Commit

Permalink
Docker v1 (#5)
Browse files Browse the repository at this point in the history
* Create Dockerfile

Kleinere Tests mit Docker

* Update Dockerfile

Docker file wurde angepasst und lädt nun direkt aus dem GitHub Repo alle notwendigen Daten herunter.

* Enhanced API

Zu dem Image wurde ein Webinterface hinzugefügt welches über <url>/exports/ erreicht wird kann als GET API genutzt werden.

* Added API Webservice in Docker

+ Developers can now access the API inside the Docker container
+ Docker container now expoesed to 8080

* Update README.md

* Update README.md
  • Loading branch information
chrischivlog authored May 22, 2024
1 parent 1c4fbbb commit 0ce97f8
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# First stage: Clone the repository
FROM bitnami/git as clone_stage
RUN git clone https://github.com/ifheroes/infinityheroes-bot.git /bot

# Second stage: Build and run the Node application with Apache and PHP
FROM node:16.20.2

# Install Apache, PHP, and other dependencies
RUN apt-get update && apt-get install -y \
apache2 \
php \
libapache2-mod-php && \
a2enmod rewrite && \
apt-get clean && rm -rf /var/lib/apt/lists/*

# Configure Apache
RUN sed -i '/<Directory \/var\/www\/>/,/<\/Directory>/ s/AllowOverride None/AllowOverride All/' /etc/apache2/apache2.conf && \
sed -i 's/Listen 80/Listen 8080/' /etc/apache2/ports.conf && \
sed -i 's/<VirtualHost \*:80>/<VirtualHost \*:8080>/' /etc/apache2/sites-available/000-default.conf

# Create the working directory
WORKDIR /bot

# Copy the cloned repository from the first stage
COPY --from=clone_stage /bot ./

# Copy the configuration file
COPY config.json /bot/src/data

# Copy the index.php for the API to the webserver directory
COPY index.php /var/www/html/

# Remove the default index.html file if it exists
RUN rm -f /var/www/html/index.html

# Install Node.js dependencies
RUN npm install

# Create the exports directory and link it to the webserver
RUN mkdir /bot/exports && \
ln -s /bot/exports /var/www/html/exports

# Deploy the application (if this step is needed)
RUN npm run deploy

# Expose port 8080 for Apache
EXPOSE 8080

# Start Apache in the background and run the Node.js application as the primary process
CMD service apache2 start && npm run start
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,18 @@ Dieser Bot erlaubt die Synchronisation zwischen der Webseite von ifheroes.de und

Jede JSON-Datei existiert nur einmal, es kann keinen Dateinamen zwei mal geben, denn:
Der Dateiname besteht aus `Monat-Tag-Jahr_Stunde-Minute-Sekunde.json` (`MM-DD-YYYY_HH-mm-ss`), Bsp. `02.18.3912_11:27:27.json`

## Informationen zur Verwendung mit Docker

1. Zunächst muss das Repo von github heruntergeladen werden

2. Im gleichen file die dem Dockerfile muss nun die config.json abgelegt werden.

3. Aktuell muss noch ein Image erstellt werden mit dem Befehl:
````
docker build --pull --rm -f "Dockerfile" -t infinityheroesbot:latest "."
````
4. Nun kann das image mit ausgeführt werden mit
````
docker run -d -p 8080:8080 infinityheroesbot:latest
````
32 changes: 32 additions & 0 deletions index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

#set the header for json response
header('Content-Type: application/json');

#scan the /exports folder
$directory = "exports/";
$files = scandir($directory, SCANDIR_SORT_DESCENDING);

# exclude subdirs from the scan
$jsonFiles = array_filter($files, function($file) use ($directory) {
return is_file($directory . '/' . $file) && pathinfo($file, PATHINFO_EXTENSION) === 'json';
});

# print out the json files found in export folder
header('Content-Type: application/json');

$data = [];
$counter = 0;

foreach ($jsonFiles as $file) {
$counter++;
$data[] = [
'number'=>''.$counter.'', # print out counted number
'path'=>'https://'.$_SERVER['HTTP_HOST'].'/exports/' . $file, # path to file with current url
'date'=>date("d-m-Y", filectime($directory . '/' . $file)), # get creation date from json file
'file_id'=>date("dmYhis", filectime($directory . '/' . $file)), # create an file id out of the date and time
];
}

echo json_encode($data);
?>

0 comments on commit 0ce97f8

Please sign in to comment.