Skip to content

Commit

Permalink
Merge pull request #128 from ASM-Studios/cma/feat-server-docker-setup
Browse files Browse the repository at this point in the history
feat(docker): add fullstack docker setup
  • Loading branch information
charlesmadjeri authored Dec 8, 2024
2 parents 8a16dac + cd77760 commit ae918f4
Show file tree
Hide file tree
Showing 24 changed files with 331 additions and 218 deletions.
10 changes: 9 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
VITE_PORT=8081
NODE_ENV=development # development or production
VITE_PORT=8081

# Server database environment variables
DB_NAME=area
DB_PASSWORD=change-me
DB_HOST=mariadb
DB_PORT=3306
DB_USER=root
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

ssl/
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TARGET_MAX_CHAR_NUM=20

.PHONY: start build stop restart reset logs clean help

PROJECT_IMAGES = area-client-web area-client-mobile
PROJECT_IMAGES = area-client-web area-client-mobile area-server mariadb rabbitmq

## Show help
help:
Expand All @@ -33,6 +33,10 @@ help:
start:
docker compose up -d

## Start containers in detached mode for production
start-prod:
docker compose -f docker-compose.yml -f docker-compose.prod.yml up -d

## Build and start containers in detached mode
build:
docker compose up --build -d
Expand Down
36 changes: 13 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ With AREA, you can create automated workflows that integrate various services an
## Table of Contents

- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Installation & Usage](#installation--usage)
- [Prerequisites](#prerequisites)
- [Installation & Usage](#installation--usage)
- [Documentation](#documentation)
- [Requirements](#requirements)
- [Usage](#usage)
- [Requirements](#requirements)
- [Usage](#usage)
- [Tests](#tests)
- [License](#license)
- [Contributors](#contributors)
Expand All @@ -19,50 +19,39 @@ With AREA, you can create automated workflows that integrate various services an

### Prerequisites

- vite js
- go
- docker
- make

### Installation & Usage

<details>
<summary>Click to expand</summary>

1. Clone the repo

```sh
git clone [email protected]:ASM-Studios/AREA.git
```

2. Create .env files

- Run the following command to create private env files

```sh
cp .env.example .env
cp server/.env.server.example server/.env.server
cp client_web/.env.local.example .env.local
cp client_mobile/.env.mobile.example .env.mobile
```

- Fill the .env, .env.web and .env.mobile files

3. Install NPM packages
```sh
cd AREA/client-web
npm install
```
4. Run the project

3. Install Go packages
```sh
cd AREA/server
<TODO>
make start
```

4. Run the project
```sh
cd AREA/client-web
npm run start
```
```sh
cd AREA/server
go run ./...
```
</details>

### Documentation
Expand All @@ -87,6 +76,7 @@ The documentation is automatically built and deployed to GitHub Pages when a pus
You can consult the documentation online at [AREA Documentation](https://asm-studios.github.io/AREA/).

You can build the documentation locally by running the following command:

```sh
cd AREA/docs
make docs
Expand Down
14 changes: 0 additions & 14 deletions client_mobile/.env.mobile.example
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
VITE_PORT=8081
VITE_ENDPOINT=http://localhost:8080

VITE_GOOGLE_CLIENT_ID=
VITE_GOOGLE_CLIENT_SECRET=

VITE_MICROSOFT_CLIENT_ID=

VITE_LINKEDIN_CLIENT_ID=
VITE_LINKEDIN_CLIENT_SECRET=

VITE_SPOTIFY_CLIENT_ID=
VITE_SPOTIFY_CLIENT_SECRET=

# Server URLs
API_URL=http://localhost:8080
WEB_CLIENT_URL=http://localhost:8081
Expand Down
47 changes: 32 additions & 15 deletions client_mobile/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,43 @@
FROM ghcr.io/cirruslabs/flutter:stable
FROM ghcr.io/cirruslabs/flutter:3.24.5 AS builder

WORKDIR /app

ARG VITE_PORT
ARG VITE_ENDPOINT
ARG VITE_GOOGLE_CLIENT_ID
ARG VITE_GOOGLE_CLIENT_SECRET
ARG VITE_MICROSOFT_CLIENT_ID
ARG VITE_LINKEDIN_CLIENT_ID
ARG VITE_LINKEDIN_CLIENT_SECRET
ARG VITE_SPOTIFY_CLIENT_ID
ARG VITE_SPOTIFY_CLIENT_SECRET
# Create a non-root user and give permissions to Android SDK directory
RUN useradd -m -d /home/flutteruser -s /bin/bash flutteruser && \
chown -R flutteruser:flutteruser /app && \
chown -R flutteruser:flutteruser /sdks/flutter && \
chown -R flutteruser:flutteruser /opt/android-sdk-linux && \
mkdir -p /opt/android-sdk-linux/licenses && \
chown -R flutteruser:flutteruser /opt/android-sdk-linux/licenses

USER flutteruser

RUN git config --global --add safe.directory /sdks/flutter && \
git config --global --add safe.directory /app

# Ensure Flutter is properly set up
RUN flutter doctor -v && \
flutter config --no-analytics

COPY --chown=flutteruser:flutteruser pubspec.* ./

RUN flutter pub get

COPY --chown=flutteruser:flutteruser . .

ARG API_URL
ARG WEB_CLIENT_URL
ARG MOBILE_CLIENT_URL
ARG GITHUB_CLIENT_ID
ARG GITHUB_CLIENT_SECRET

COPY . .
# Accept Android licenses and install SDK components
RUN yes | sdkmanager --licenses && \
sdkmanager "build-tools;30.0.3"

RUN flutter pub get
RUN flutter build apk --release
# Clean and get dependencies again after copying all files
RUN flutter clean && \
flutter pub get

RUN mv build/app/outputs/flutter-apk/app-release.apk build/app/outputs/flutter-apk/client.apk
RUN chmod -R 755 build/app/outputs/flutter-apk/
RUN flutter build apk --release
RUN mv build/app/outputs/flutter-apk/app-release.apk build/app/outputs/flutter-apk/client.apk
2 changes: 1 addition & 1 deletion client_mobile/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
sdk: '>=3.4.1 <4.0.0'
sdk: '^3.5.0'

# Dependencies specify other packages that your package needs in order to work.
# To automatically upgrade your package dependencies to the latest versions
Expand Down
91 changes: 52 additions & 39 deletions client_web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,54 +1,67 @@
###----------------------- Certificate generation stage -----------------------###
FROM alpine:3.19 AS cert-builder
FROM alpine:3.20.3 AS cert-builder

# Install mkcert dependencies
RUN apk add --no-cache \
curl \
nss \
nss-tools
RUN apk add --no-cache openssl
RUN mkdir -p /etc/nginx/ssl && \
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout /etc/nginx/ssl/private.key \
-out /etc/nginx/ssl/certificate.crt \
-subj "/C=FR/ST=Paris/L=Paris/O=Area/OU=IT/CN=localhost" \
-addext "subjectAltName = DNS:localhost" && \
chmod 644 /etc/nginx/ssl/certificate.crt /etc/nginx/ssl/private.key

# Install mkcert
RUN curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64" \
&& chmod +x mkcert-v*-linux-amd64 \
&& mv mkcert-v*-linux-amd64 /usr/local/bin/mkcert \
&& mkcert -install \
&& mkcert localhost
###----------------------- Build stage -----------------------###
FROM node:20-alpine AS builder

WORKDIR /app

###----------------------- Build stage for Node.js application -----------------------###
FROM node:latest AS builder
COPY package*.json ./

WORKDIR /app
RUN if [ "$NODE_ENV" = "production" ] ; then npm ci ; else npm install ; fi

ARG VITE_PORT
ARG VITE_ENDPOINT
ARG VITE_GOOGLE_CLIENT_ID
ARG VITE_GOOGLE_CLIENT_SECRET
ARG VITE_MICROSOFT_CLIENT_ID
ARG VITE_LINKEDIN_CLIENT_ID
ARG VITE_LINKEDIN_CLIENT_SECRET
ARG VITE_SPOTIFY_CLIENT_ID
ARG VITE_SPOTIFY_CLIENT_SECRET
ARG API_URL
ARG WEB_CLIENT_URL
ARG MOBILE_CLIENT_URL
ARG GITHUB_CLIENT_ID
ARG GITHUB_CLIENT_SECRET

COPY ./package*.json ./
RUN npm install
COPY . .

RUN npm run build

RUN if [ "$NODE_ENV" = "production" ] ; then npm run build ; fi

###----------------------- Production stage -----------------------###
FROM nginx:alpine AS production
FROM nginx:1.25.3-alpine AS production

ARG VITE_PORT

RUN adduser -D nginxuser && \
mkdir -p /usr/share/nginx/html/mobile_builds && \
mkdir -p /etc/nginx/ssl && \
chown -R nginxuser:nginxuser /usr/share/nginx/html && \
chown -R nginxuser:nginxuser /etc/nginx/ssl && \
chown -R nginxuser:nginxuser /var/cache/nginx && \
chown -R nginxuser:nginxuser /var/log/nginx && \
touch /var/run/nginx.pid && \
chown -R nginxuser:nginxuser /var/run/nginx.pid

COPY --from=builder /app/dist /usr/share/nginx/html
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
RUN mkdir -p /usr/share/nginx/html/mobile_builds
COPY --from=cert-builder --chown=nginxuser:nginxuser /etc/nginx/ssl /etc/nginx/ssl
COPY --from=builder --chown=nginxuser:nginxuser /app/dist /usr/share/nginx/html
COPY --chown=nginxuser:nginxuser ./nginx.conf /etc/nginx/conf.d/default.conf

USER nginxuser

EXPOSE ${VITE_PORT}

HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:${VITE_PORT}/health || exit 1

CMD ["nginx", "-g", "daemon off;"]

###----------------------- Development stage -----------------------###
FROM builder AS development

RUN apk add --no-cache \
nss-tools \
curl \
&& curl -JLO "https://dl.filippo.io/mkcert/latest?for=linux/amd64" \
&& chmod +x mkcert-v*-linux-amd64 \
&& cp mkcert-v*-linux-amd64 /usr/local/bin/mkcert

ENV NODE_ENV=development
EXPOSE ${VITE_PORT}

CMD ["nginx", "-g", "daemon off;"]
CMD ["sh", "-c", "if [ ! -f localhost-key.pem ]; then mkcert -install && mkcert localhost; fi && mv localhost*.pem /app/ && npm run dev -- --host"]
7 changes: 6 additions & 1 deletion client_web/nginx.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
server {
listen 8081;
listen 8081 ssl;
server_name localhost;

ssl_certificate /etc/nginx/ssl/certificate.crt;
ssl_certificate_key /etc/nginx/ssl/private.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
Expand Down
2 changes: 1 addition & 1 deletion client_web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"main": "electron.js",
"scripts": {
"start": "mkcert -install && mkcert localhost && vite",
"dev": "mkcert -install && mkcert localhost && vite",
"dev": "vite --host",
"production": "vite --mode production",
"build": "tsc -b && vite build",
"lint": "eslint .",
Expand Down
10 changes: 7 additions & 3 deletions client_web/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import fs from 'fs'
import path from 'path'
import fs from 'fs'

export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd())
Expand All @@ -10,9 +10,13 @@ export default defineConfig(({ mode }) => {
plugins: [react()],
server: {
port: parseInt(env.VITE_PORT) || 8081,
watch: {
usePolling: true,
},
host: true,
https: {
key: fs.readFileSync(path.resolve(__dirname, 'localhost-key.pem')),
cert: fs.readFileSync(path.resolve(__dirname, 'localhost.pem')),
key: fs.readFileSync('localhost-key.pem'),
cert: fs.readFileSync('localhost.pem'),
},
},
resolve: {
Expand Down
24 changes: 24 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
services:
area-client-web:
build:
target: development
volumes:
- ./client_web:/app
- /app/node_modules
- ssl-certs:/app/certs
environment:
- NODE_ENV=development
- CHOKIDAR_USEPOLLING=true

area-server:
build:
target: development
volumes:
- ./server:/app
- go-modules:/go/pkg/mod
environment:
- GO_ENV=development

volumes:
ssl-certs:
go-modules:
Loading

0 comments on commit ae918f4

Please sign in to comment.