Skip to content

Commit

Permalink
added: form-api
Browse files Browse the repository at this point in the history
  • Loading branch information
scolastico committed Oct 12, 2024
1 parent 8acfb75 commit 2a2a2b5
Show file tree
Hide file tree
Showing 22 changed files with 7,776 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/publish-form-api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Publish Form API

on:
push:
branches: [ "main" ]
paths: [ "src/form-api/**", ".github/workflows/publish-form-api.yml" ]

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{github.actor}}
password: ${{secrets.GITHUB_TOKEN}}
- uses: mr-smithers-excellent/docker-build-push@v6
with:
image: s.containers/form-api
registry: ghcr.io
directory: ./src/form-api/
dockerfile: ./src/form-api/Dockerfile
addLatest: true
multiPlatform: true
platform: linux/amd64,linux/arm64
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
4 changes: 4 additions & 0 deletions src/form-api/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Dockerfile
Readme.md
.gitignore
node_modules
25 changes: 25 additions & 0 deletions src/form-api/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: 'tsconfig.json',
tsconfigRootDir: __dirname,
sourceType: 'module',
},
plugins: ['@typescript-eslint/eslint-plugin'],
extends: [
'plugin:@typescript-eslint/recommended',
'plugin:prettier/recommended',
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: ['.eslintrc.js'],
rules: {
'@typescript-eslint/interface-name-prefix': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-module-boundary-types': 'off',
'@typescript-eslint/no-explicit-any': 'off',
},
};
35 changes: 35 additions & 0 deletions src/form-api/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# compiled output
/dist
/node_modules

# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*

# OS
.DS_Store

# Tests
/coverage
/.nyc_output

# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace

# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
4 changes: 4 additions & 0 deletions src/form-api/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"singleQuote": true,
"trailingComma": "all"
}
28 changes: 28 additions & 0 deletions src/form-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM node:20-bookworm AS build

RUN npm install -g pnpm

WORKDIR /app
COPY package.json .
COPY pnpm-lock.yaml .

RUN pnpm i

COPY . .
RUN pnpm build

FROM node:20-bookworm AS production
WORKDIR /app

RUN npm install -g pnpm

COPY --from=build /app/dist .
COPY --from=build /app/package.json .
COPY --from=build /app/pnpm-lock.yaml .

RUN pnpm i --production

EXPOSE 3000
LABEL maintainer="Joschua Becker EDV <support@scolasti.co>"
CMD ["node", "main.js"]
STOPSIGNAL SIGKILL
56 changes: 56 additions & 0 deletions src/form-api/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# `s.containers/form-api`

### [Back to index](../../README.md)

## Why?

Form API is a simple form to email service. It is a simple way to send form data to an email address.

## Environment Variables

| Name | Type | Default Value | Description |
| ----------------------------- | ------ | ------------------ | ------------------------------------------------------------------------------------------------------ |
| `DISABLE_SWAGGER` | bool | `false` | Whether to disable the swagger documentation. |
| `ENABLE_DEBUG_ENDPOINT` | bool | `false` | Whether to enable the debug endpoint. |
| `SMTP_HOST` | string | `null` | The SMTP host. |
| `SMTP_PORT` | number | `587` | The SMTP port. |
| `SMTP_TLS` | bool | `true` | Whether to use TLS. |
| `SMTP_TLS_CHECK` | bool | `true` | Whether to check the TLS certificate. |
| `SMTP_USER` | string | `null` | The SMTP user. |
| `SMTP_FROM` | string | `$SMTP_USER` | The SMTP from address. |
| `SMTP_PASSWORD` | string | `null` | The SMTP password. |
| `CAPTCHA_VERIFY_IP` | bool | `true` | Whether to verify the ip address of the captcha response. |
| `ALLOW_FORWARDED_FOR` | bool | `false` | Whether to allow the `X-Forwarded-For` header to be used as the ip address. |
| `CFG_<name>_EMAIL` | string | `null` | The email address to send the form data to. |
| `CFG_<name>_SUBJECT` | string | `Form-API: <name>` | The subject of the email. |
| `CFG_<name>_CAPTCHA_SECRET` | string | `null` | The secret of the captcha. If no captcha strength is chosen the captcha is expected to be a reCAPTCHA. |
| `CFG_<name>_CAPTCHA_GENERATE` | bool | `false` | Whether to generate a captcha. |
| `CFG_<name>_MAX_SIZE` | number | `1000000` | The maximum size of the form data. |
| `CFG_<name>_JSON_SCHEMA` | string | `null` | The JSON schema to validate the form data. |
| `CFG_<name>_JSON_SCHEMA_URL` | string | `null` | The URL to fetch the JSON schema from. |

The captcha secret is optional and can be used to verify a [Google reCAPTCHA](https://developers.google.com/recaptcha) response. The captcha secret is used to verify the response. The captcha secret can be set in the [Google reCAPTCHA admin console](https://www.google.com/recaptcha/admin/create).

If the `CFG_<name>_CAPTCHA_GENERATE` is set to `true` a captcha is locally generated, see [config.type.ts](./src/captcha/config.type.ts) for the configuration options.

## API's

Visit the `/` endpoint to see a swagger documentation of the API, if the `DISABLE_SWAGGER` environment variable is not set to `true`.

## Example

```yaml
version: "3"
services:
app:
image: ghcr.io/scolastico-dev/s.containers/form-api:latest
environment:
SMTP_HOST: smtp.example.com
SMTP_USER: admin@example.com
SMTP_PASSWORD: password

CFG_CONTACT_EMAIL: contact@example.com
CFG_CONTACT_SUBJECT: Contact Form Submission
CFG_CONTACT_CAPTCHA_SECRET: secret
CFG_CONTACT_CAPTCHA_STRENGTH: 3
```
8 changes: 8 additions & 0 deletions src/form-api/nest-cli.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}
75 changes: 75 additions & 0 deletions src/form-api/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"name": "form-api",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"dev": "ENABLE_DEBUG_ENDPOINT=true CFG_CONTACT_EMAIL=to@example.com CFG_CONTACT_CAPTCHA_SECRET=secret CFG_CONTACT_CAPTCHA_STRENGTH=2 nest start --watch",
"start": "nest start",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.4.2",
"ajv": "^8.17.1",
"axios": "^1.7.7",
"canvas": "^2.11.2",
"dompurify": "^3.1.7",
"nodemailer": "^6.9.15",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
"devDependencies": {
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
"@typescript-eslint/parser": "^5.59.11",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",
"eslint-plugin-prettier": "^4.2.1",
"jest": "^29.5.0",
"prettier": "^2.8.8",
"source-map-support": "^0.5.21",
"supertest": "^6.3.3",
"ts-jest": "^29.1.0",
"ts-loader": "^9.4.3",
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
Loading

0 comments on commit 2a2a2b5

Please sign in to comment.