Skip to content

Commit 1a5b47b

Browse files
authored
Merge pull request #64 from berdal84/feat/docker-compose
refactor: rely on docker and bash scripts
2 parents a06c78c + 4309b9f commit 1a5b47b

File tree

190 files changed

+477
-408
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

190 files changed

+477
-408
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
33

4-
name: Node.js CI
4+
name: api
55

66
on:
77
push:
@@ -11,20 +11,7 @@ on:
1111

1212
jobs:
1313
build:
14-
1514
runs-on: ubuntu-latest
16-
17-
strategy:
18-
matrix:
19-
node-version: [18.x]
20-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21-
2215
steps:
2316
- uses: actions/checkout@v2
24-
- name: Use Node.js ${{ matrix.node-version }}
25-
uses: actions/setup-node@v1
26-
with:
27-
node-version: ${{ matrix.node-version }}
28-
- run: npm ci
29-
- run: npm run build
30-
- run: npm test
17+
- run: docker compose build api

.github/workflows/db.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: db
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- run: docker compose build db

.github/workflows/ui.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: ui
5+
6+
on:
7+
push:
8+
branches: [ master ]
9+
pull_request:
10+
branches: [ master ]
11+
12+
jobs:
13+
build:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v2
17+
- run: docker compose build ui
18+
- run: ./test-ui.sh

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,4 @@ testem.log
3939

4040
# System Files
4141
.DS_Store
42-
Thumbs.db
43-
44-
# should never be commited
45-
config*.php
46-
!config.sample.php
42+
Thumbs.db

README.md

+38-37
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,65 @@
1-
# Jeu de mots
1+
# Jeu De Mots
22

3-
<a href="https://github.com/berdal84/jeudemots-ng/actions?query=workflow Node.js CI" title="ng build">
4-
<img src="https://github.com/berdal84/jeudemots-ng/workflows/Node.js CI/badge.svg" />
5-
</a>
3+
[![ui](https://github.com/berdal84/jeudemots/actions/workflows/ui.yml/badge.svg?branch=master&event=status)](https://github.com/berdal84/jeudemots/actions/workflows/ui.yml)
4+
[![api](https://github.com/berdal84/jeudemots/actions/workflows/api.yml/badge.svg?branch=master&event=status)](https://github.com/berdal84/jeudemots/actions/workflows/api.yml)
5+
[![db](https://github.com/berdal84/jeudemots/actions/workflows/db.yml/badge.svg?branch=master&event=status)](https://github.com/berdal84/jeudemots/actions/workflows/db.yml)
66

77
## Introduction
88

9-
This is a client-server solution to host jokes using Angular and PHP/MySQL. Two alternative React/Vue clients are work in progress. The project is deployed at [https://jeudemots.42borgata.com](https://jeudemots.42borgata.com),
9+
_Jeu De Mots_ is a web application to host jokes developed with Angular and PHP.
10+
11+
Try it: [https://jeudemots.42borgata.com](https://jeudemots.42borgata.com)
1012

1113
<div align="center">
12-
<img width="100%" height="100%" src="./screenshot.png" />
14+
<img width="100%" height="100%" src="./docs/screenshot.png" />
1315
<p>Angular Frontend | Slideshow capture<p/>
1416
</div>
1517

16-
## Project files
17-
18-
This repository contains source code for different parts of the project:
19-
20-
- `server`: backend PHP sources files
21-
- `client/ng`: main Angular frontend sources
18+
## Quick start
2219

23-
WIP:
20+
### Prerequisites
2421

25-
- `client/react-js`: alternative React frontend sources (deployed
26-
at [https://jeudemots.42borgata.com/react](https://jeudemots.42borgata.com/react))
27-
- `client/vue`: alternative Vue frontend sources (deployed
28-
at [https://jeudemots.42borgata.com/vue](https://jeudemots.42borgata.com/vue))
29-
- `client/shared`: shared code and resources.
22+
_Prerequisites: docker is **required**._
3023

31-
## Development
24+
### Launch
3225

33-
### Prerequisites
26+
Run the following command to build and launch the app:
3427

35-
Install the following dependencies:
3628
```
37-
sudo apt install nodejs npm php libapache2-mod-php php-mysql php-cli
29+
docker compose up -d
3830
```
3931

40-
And install the project
32+
Browse `https://localhost:4200/`
4133

42-
```
43-
npm install
44-
```
34+
### Post-launch (do once)
4535

46-
### `npm run build`
36+
Browse `https://localhost:4200/#/login` and login with the following credentials:
37+
- username/pass: `admin`/`admin`
4738

48-
Launches the build of the subprojects (server, angular/react frontends). Produce a `./build` subfolder in each
49-
subprojectfolder (see `in client/`).
39+
<div align="center">
40+
<img height=300 src="./docs/login-page.png" />
41+
</div>
5042

51-
*note: the first time, before launching the command, you need to duplicate `server/config.sample.php`,
52-
rename it to `config.prod.php` and replace the data inside the file with your credentials (depends on your server
53-
config). If not, the server build will fail.*
43+
Once logged, install the app by following the instructions of the `install` section.
44+
45+
<div align="center">
46+
<img width="100%" height="100%" src="./docs/install-page.png" />
47+
<img height=100 src="./docs/install-confirmation.png" />
48+
</div>
49+
50+
## Project files
5451

55-
### `npm run pack`
52+
Quick summary of the project's folder hierarchy:
5653

57-
Copy the last build files from all subprojects to a global `./dist` folder. This folder will contain the files you need
58-
to copy to your web server to get the application to work.
54+
- `./ui/ng`: Angular sources
55+
- `./ui/shared`: shared code and resources
56+
- `./ui/react`: WIP alternative React sources (deployed
57+
at [here](https://jeudemots.42borgata.com/react))
58+
- `./ui/vue`: WIP alternative Vue frontend (deployed
59+
at [here](https://jeudemots.42borgata.com/vue))
60+
- `./api`: api source
61+
- `./db`: database sources
5962

60-
*note: The first time you will need to install the database. To do that, sign in to the admin page (add `/admin`to the
61-
URL), go to the installation section and click on install. Then, you can optionally restore a previously backed up file.*
6263

6364
## History
6465

api/.dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Avoids to include any config file other than config.sample.php
2+
config*
3+
!config.sample.php

api/.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# No config should be committed except the sample
2+
config*.php
3+
!config.dev.php
4+
!config.sample.php

api/Dockerfile

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
FROM php:7.2-apache
2+
WORKDIR /var/www/html
3+
4+
# Install MySQL dependency
5+
RUN docker-php-ext-install mysqli
6+
7+
# Copy sources
8+
COPY src/v1 ./v1
9+
COPY src/lib ./lib
10+
COPY src/sql ./sql
11+
COPY src/config.sample.php ./config.php
12+
13+
# Allow to override configuration file
14+
VOLUME [ "./config.php" ]
15+
16+
# Override entry point
17+
ENTRYPOINT [ "docker-php-entrypoint", "apache2-foreground"]
18+
19+
# Expose regular http/https port
20+
EXPOSE 80
21+
#EXPOSE 443 (TODO: add certificates)

api/src/config.dev.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
define('DB_HOST', 'db');
4+
define('DB_NAME', 'jokes');
5+
define('DB_USER', 'admin');
6+
define('DB_PASS', 'admin');
7+
8+
define('ADMIN_USER', 'admin');
9+
define('ADMIN_PASS', hash('sha256', 'admin'));
10+
define('ADMIN_EMAIL', '');
11+
define('ACCESS_CONTROL_ALLOW_ORIGIN', '*' );
12+
#define('COOKIE_DOMAIN', 'my.domain.com');
13+
#define('COOKIE_LIFETIME', 60*5); // 5min
14+
define('DEBUG', false);
15+
16+
ini_set('session.use_strict_mode', 1); // see https://www.php.net/manual/en/features.session.security.management.php#features.session.security.management.non-adaptive-session
17+
if(DEBUG) ini_set('display_errors', 1);
18+
19+
?>

api/src/config.sample.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
define('DB_HOST', '');
4+
define('DB_NAME', '');
5+
define('DB_USER', '');
6+
define('DB_PASS', '');
7+
8+
define('ADMIN_USER', '');
9+
define('ADMIN_PASS', '');
10+
define('ADMIN_EMAIL', '');
11+
define('ACCESS_CONTROL_ALLOW_ORIGIN', array('domain.com', 'alternative-domain.com') );
12+
define('COOKIE_DOMAIN', '');
13+
define('COOKIE_LIFETIME', 60*30);
14+
15+
?>

api/src/lib/app.php

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
require_once('../lib/authentication.php');
4+
require_once('../lib/db.php');
5+
require_once('../lib/header.php');
6+
require_once('../lib/joke-crud.php');
7+
require_once('../lib/mail.php');
8+
require_once('../lib/response.php');
9+
require_once('../lib/url-params.php');
10+
11+
class App
12+
{
13+
public static function start()
14+
{
15+
Authentication::session_start();
16+
Header::headers();
17+
}
18+
19+
public static function start_if_logged()
20+
{
21+
App::start();
22+
Authentication::exit_if_not_logged();
23+
}
24+
}
25+
26+
?>

server/src/core/authentication.php api/src/lib/authentication.php

+32-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,43 @@
66
class Authentication
77
{
88
static public function session_start(): bool
9-
{
10-
session_set_cookie_params([
11-
'lifetime' => COOKIE_LIFETIME,
12-
'domain' => COOKIE_DOMAIN,
9+
{
10+
11+
$lifetime = defined('COOKIE_LIFETIME') ? COOKIE_LIFETIME : 60*4; // 4 min by default
12+
$domain = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '127.0.0.1';
13+
14+
$cookie_ok = session_set_cookie_params([
15+
'lifetime' => $lifetime,
16+
'domain' => $domain,
17+
'path' => '/',
18+
'httponly' => true,
1319
]);
1420

15-
if( !session_start() ) return false;
16-
17-
if (isset($_SESSION['created_at']) &&
18-
time() - $_SESSION['created_at'] > COOKIE_LIFETIME)
21+
// setcookie('test', 'value', 60*60, "/");
22+
23+
if ( !$cookie_ok )
1924
{
20-
session_regenerate_id(true); // invalidate old session ID
25+
die("cookie error");
2126
}
27+
28+
if( !session_start() )
29+
{
30+
return false;
31+
}
32+
33+
// Invalidate old cookies
34+
$created_at = $_SESSION['created_at'];
35+
if (isset($created_at))
36+
{
37+
$age = time() - $created_at;
38+
if ( $age > $lifetime )
39+
{
40+
session_regenerate_id(true);
41+
}
42+
}
43+
2244
$_SESSION['created_at'] = time();
23-
$_SESSION['lifetime'] = COOKIE_LIFETIME;
45+
$_SESSION['lifetime'] = $lifetime;
2446
return true;
2547
}
2648

api/src/lib/db.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
require_once __DIR__.'/joke-crud.php';
4+
5+
class DB
6+
{
7+
static function connect()
8+
{
9+
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
10+
// Check connection
11+
if (!$mysqli) {
12+
Response::failure("DB: Could not connect.");
13+
}
14+
15+
return $mysqli;
16+
}
17+
18+
public static function uninstall(): bool
19+
{
20+
return DB::query_from_sql_file(__DIR__.'/../sql/uninstall.sql');
21+
}
22+
23+
public static function install(): bool
24+
{
25+
return DB::query_from_sql_file(__DIR__.'/../sql/install.sql');
26+
}
27+
28+
private static function query_from_sql_file($path): bool
29+
{
30+
$mysqli = DB::connect();
31+
$sql = file_get_contents($path);
32+
$success = $mysqli->multi_query($sql);
33+
$mysqli->close();
34+
return $success;
35+
}
36+
}
37+
38+
?>

0 commit comments

Comments
 (0)