Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rely on docker and bash scripts #64

Merged
merged 25 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
32ca28c
feat: add docker-compose.yml 1/N
berdal84 Mar 16, 2024
cd8fe04
feat: add docker-compose.yml 2/N
berdal84 Mar 16, 2024
5ebba1f
feat: add docker-compose.yml 3/N
berdal84 Mar 16, 2024
fa1c5f9
fix(server): User => Authentication in exit_if_not_logged
berdal84 Apr 27, 2024
90a9f80
feat(docker): improve docker-compose
berdal84 Apr 27, 2024
1f5c751
refactor: change install/build/pack strategy
berdal84 Apr 27, 2024
ae796ae
fix(ci): old commands
berdal84 Apr 27, 2024
621b6a2
fix(test.sh): add flags for ci`
berdal84 Apr 27, 2024
65d78e7
feat: add db folder, clean install methods
berdal84 Apr 27, 2024
17b4548
refactor: rename server => api
berdal84 Apr 27, 2024
13ade31
refactor(api): rename src/api => src/v1
berdal84 Apr 27, 2024
1c936f3
fix(client/ng): api.service
berdal84 Apr 28, 2024
d1223f4
refactor(api): add lib/app/php, fix headers and cookies
berdal84 Apr 28, 2024
53ffaad
feat: add docker for the ui (ng client)
berdal84 Apr 28, 2024
0a62381
doc: rewrite instructions according to docker-compose
berdal84 Apr 28, 2024
9667630
doc(ci): add comment relative to docker build
berdal84 Apr 28, 2024
f8963fe
Merge branch 'master' into feat/docker-compose
berdal84 Apr 28, 2024
dd0aad1
feat(ui): add ci
berdal84 Apr 28, 2024
466f7ed
refactor: rename client => ui
berdal84 Apr 28, 2024
a240838
doc: update project files section
berdal84 Apr 28, 2024
0259437
refactor: change user/password
berdal84 Apr 28, 2024
a1b00d1
feat(ci): remove old ci, add ui/api/db ci
berdal84 Apr 28, 2024
db399b2
doc: typos
berdal84 Apr 28, 2024
4f61b36
fix: tests
berdal84 Apr 28, 2024
4309b9f
fix: remove useless scripts, run test in docker
berdal84 Apr 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions .github/workflows/node.js.yml → .github/workflows/api.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI
name: api

on:
push:
Expand All @@ -11,20 +11,7 @@ on:

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [18.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- run: npm ci
- run: npm run build
- run: npm test
- run: docker compose build api
17 changes: 17 additions & 0 deletions .github/workflows/db.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: db

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: docker compose build db
18 changes: 18 additions & 0 deletions .github/workflows/ui.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: ui

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- run: docker compose build ui
- run: ./test-ui.sh
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,4 @@ testem.log

# System Files
.DS_Store
Thumbs.db

# should never be commited
config*.php
!config.sample.php
Thumbs.db
75 changes: 38 additions & 37 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,64 +1,65 @@
# Jeu de mots
# Jeu De Mots

<a href="https://github.com/berdal84/jeudemots-ng/actions?query=workflow Node.js CI" title="ng build">
<img src="https://github.com/berdal84/jeudemots-ng/workflows/Node.js CI/badge.svg" />
</a>
[![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)
[![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)
[![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)

## Introduction

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),
_Jeu De Mots_ is a web application to host jokes developed with Angular and PHP.

Try it: [https://jeudemots.42borgata.com](https://jeudemots.42borgata.com)

<div align="center">
<img width="100%" height="100%" src="./screenshot.png" />
<img width="100%" height="100%" src="./docs/screenshot.png" />
<p>Angular Frontend | Slideshow capture<p/>
</div>

## Project files

This repository contains source code for different parts of the project:

- `server`: backend PHP sources files
- `client/ng`: main Angular frontend sources
## Quick start

WIP:
### Prerequisites

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

## Development
### Launch

### Prerequisites
Run the following command to build and launch the app:

Install the following dependencies:
```
sudo apt install nodejs npm php libapache2-mod-php php-mysql php-cli
docker compose up -d
```

And install the project
Browse `https://localhost:4200/`

```
npm install
```
### Post-launch (do once)

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

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

*note: the first time, before launching the command, you need to duplicate `server/config.sample.php`,
rename it to `config.prod.php` and replace the data inside the file with your credentials (depends on your server
config). If not, the server build will fail.*
Once logged, install the app by following the instructions of the `install` section.

<div align="center">
<img width="100%" height="100%" src="./docs/install-page.png" />
<img height=100 src="./docs/install-confirmation.png" />
</div>

## Project files

### `npm run pack`
Quick summary of the project's folder hierarchy:

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

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

## History

Expand Down
3 changes: 3 additions & 0 deletions api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Avoids to include any config file other than config.sample.php
config*
!config.sample.php
4 changes: 4 additions & 0 deletions api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# No config should be committed except the sample
config*.php
!config.dev.php
!config.sample.php
21 changes: 21 additions & 0 deletions api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM php:7.2-apache
WORKDIR /var/www/html

# Install MySQL dependency
RUN docker-php-ext-install mysqli

# Copy sources
COPY src/v1 ./v1
COPY src/lib ./lib
COPY src/sql ./sql
COPY src/config.sample.php ./config.php

# Allow to override configuration file
VOLUME [ "./config.php" ]

# Override entry point
ENTRYPOINT [ "docker-php-entrypoint", "apache2-foreground"]

# Expose regular http/https port
EXPOSE 80
#EXPOSE 443 (TODO: add certificates)
19 changes: 19 additions & 0 deletions api/src/config.dev.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

define('DB_HOST', 'db');
define('DB_NAME', 'jokes');
define('DB_USER', 'admin');
define('DB_PASS', 'admin');

define('ADMIN_USER', 'admin');
define('ADMIN_PASS', hash('sha256', 'admin'));
define('ADMIN_EMAIL', '');
define('ACCESS_CONTROL_ALLOW_ORIGIN', '*' );
#define('COOKIE_DOMAIN', 'my.domain.com');
#define('COOKIE_LIFETIME', 60*5); // 5min
define('DEBUG', false);

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
if(DEBUG) ini_set('display_errors', 1);

?>
15 changes: 15 additions & 0 deletions api/src/config.sample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

define('DB_HOST', '');
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASS', '');

define('ADMIN_USER', '');
define('ADMIN_PASS', '');
define('ADMIN_EMAIL', '');
define('ACCESS_CONTROL_ALLOW_ORIGIN', array('domain.com', 'alternative-domain.com') );
define('COOKIE_DOMAIN', '');
define('COOKIE_LIFETIME', 60*30);

?>
26 changes: 26 additions & 0 deletions api/src/lib/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require_once('../lib/authentication.php');
require_once('../lib/db.php');
require_once('../lib/header.php');
require_once('../lib/joke-crud.php');
require_once('../lib/mail.php');
require_once('../lib/response.php');
require_once('../lib/url-params.php');

class App
{
public static function start()
{
Authentication::session_start();
Header::headers();
}

public static function start_if_logged()
{
App::start();
Authentication::exit_if_not_logged();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,43 @@
class Authentication
{
static public function session_start(): bool
{
session_set_cookie_params([
'lifetime' => COOKIE_LIFETIME,
'domain' => COOKIE_DOMAIN,
{

$lifetime = defined('COOKIE_LIFETIME') ? COOKIE_LIFETIME : 60*4; // 4 min by default
$domain = defined('COOKIE_DOMAIN') ? COOKIE_DOMAIN : '127.0.0.1';

$cookie_ok = session_set_cookie_params([
'lifetime' => $lifetime,
'domain' => $domain,
'path' => '/',
'httponly' => true,
]);

if( !session_start() ) return false;

if (isset($_SESSION['created_at']) &&
time() - $_SESSION['created_at'] > COOKIE_LIFETIME)
// setcookie('test', 'value', 60*60, "/");

if ( !$cookie_ok )
{
session_regenerate_id(true); // invalidate old session ID
die("cookie error");
}

if( !session_start() )
{
return false;
}

// Invalidate old cookies
$created_at = $_SESSION['created_at'];
if (isset($created_at))
{
$age = time() - $created_at;
if ( $age > $lifetime )
{
session_regenerate_id(true);
}
}

$_SESSION['created_at'] = time();
$_SESSION['lifetime'] = COOKIE_LIFETIME;
$_SESSION['lifetime'] = $lifetime;
return true;
}

Expand Down
38 changes: 38 additions & 0 deletions api/src/lib/db.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

require_once __DIR__.'/joke-crud.php';

class DB
{
static function connect()
{
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check connection
if (!$mysqli) {
Response::failure("DB: Could not connect.");
}

return $mysqli;
}

public static function uninstall(): bool
{
return DB::query_from_sql_file(__DIR__.'/../sql/uninstall.sql');
}

public static function install(): bool
{
return DB::query_from_sql_file(__DIR__.'/../sql/install.sql');
}

private static function query_from_sql_file($path): bool
{
$mysqli = DB::connect();
$sql = file_get_contents($path);
$success = $mysqli->multi_query($sql);
$mysqli->close();
return $success;
}
}

?>
Loading
Loading