Skip to content

Commit

Permalink
feat: generate Dockerfiles for Express and FastAPI apps
Browse files Browse the repository at this point in the history
Part of #42
  • Loading branch information
php-coder committed Mar 14, 2024
1 parent 7db66f3 commit 00a1f9d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
1. Generate code
| Language | Command for code generation | Example of generated files | Libraries |
| -----------| ----------------------------| ---------------------------| --------- |
| JavaScript | `npx query2app --lang js` | [`app.js`](examples/js/express/mysql/app.js)<br/>[`routes.js`](examples/js/express/mysql/routes.js)<br/>[`package.json`](examples/js/express/mysql/package.json) | Web: [`express`](https://www.npmjs.com/package/express)<br>Database: [`mysql`](https://www.npmjs.com/package/mysql) |
| TypeScript | `npx query2app --lang ts` | [`app.ts`](examples/ts/express/mysql/app.ts)<br/>[`routes.ts`](examples/ts/express/mysql/routes.ts)<br/>[`package.json`](examples/ts/express/mysql/package.json)<br/>[`tsconfig.json`](examples/ts/express/mysql/tsconfig.json) | Web: [`express`](https://www.npmjs.com/package/express)<br>Database: [`mysql`](https://www.npmjs.com/package/mysql) |
| JavaScript | `npx query2app --lang js` | [`app.js`](examples/js/express/mysql/app.js)<br/>[`routes.js`](examples/js/express/mysql/routes.js)<br/>[`package.json`](examples/js/express/mysql/package.json)<br/>[`Dockerfile`](examples/js/express/mysql/Dockerfile) | Web: [`express`](https://www.npmjs.com/package/express)<br>Database: [`mysql`](https://www.npmjs.com/package/mysql) |
| TypeScript | `npx query2app --lang ts` | [`app.ts`](examples/ts/express/mysql/app.ts)<br/>[`routes.ts`](examples/ts/express/mysql/routes.ts)<br/>[`package.json`](examples/ts/express/mysql/package.json)<br/>[`tsconfig.json`](examples/ts/express/mysql/tsconfig.json)<br/>[`Dockerfile`](examples/ts/express/mysql/Dockerfile) | Web: [`express`](https://www.npmjs.com/package/express)<br>Database: [`mysql`](https://www.npmjs.com/package/mysql) |
| Golang | `npx query2app --lang go` | [`app.go`](examples/go/chi/mysql/app.go)<br/>[`routes.go`](examples/go/chi/mysql/routes.go)<br/>[`go.mod`](examples/go/chi/mysql/go.mod) | Web: [`go-chi/chi`](https://github.com/go-chi/chi)<br/>Database: [`go-sql-driver/mysql`](https://github.com/go-sql-driver/mysql), [`jmoiron/sqlx`](https://github.com/jmoiron/sqlx) |
| Python | `npx query2app --lang python` | [`app.py`](examples/python/fastapi/postgres/app.py)<br/>[`db.py`](examples/python/fastapi/postgres/db.py)<br/>[`routes.py`](examples/python/fastapi/postgres/routes.py)<br/>[`requirements.txt`](examples/python/fastapi/postgres/requirements.txt) | Web: [FastAPI](https://github.com/tiangolo/fastapi), [Uvicorn](https://www.uvicorn.org)<br/>Database: [psycopg2](https://pypi.org/project/psycopg2/) |
| Python | `npx query2app --lang python` | [`app.py`](examples/python/fastapi/postgres/app.py)<br/>[`db.py`](examples/python/fastapi/postgres/db.py)<br/>[`routes.py`](examples/python/fastapi/postgres/routes.py)<br/>[`requirements.txt`](examples/python/fastapi/postgres/requirements.txt)<br/>[`Dockerfile`](examples/python/fastapi/postgres/Dockerfile) | Web: [FastAPI](https://github.com/tiangolo/fastapi), [Uvicorn](https://www.uvicorn.org)<br/>Database: [psycopg2](https://pypi.org/project/psycopg2/) |

1. Run the application
| Language | Commands to run the application |
Expand Down
8 changes: 8 additions & 0 deletions examples/js/express/mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:18-bookworm
WORKDIR /opt/app
COPY package.json ./
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
RUN npm install --no-audit --no-fund
COPY *.js ./
USER node
CMD [ "npm", "start" ]
6 changes: 6 additions & 0 deletions examples/python/fastapi/postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.7-bookworm
WORKDIR /opt/app
COPY requirements.txt ./
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY *.py ./
CMD [ "uvicorn", "app:app", "--host", "0.0.0.0" ]
9 changes: 9 additions & 0 deletions examples/ts/express/mysql/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:18-bookworm
WORKDIR /opt/app
COPY package.json ./
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
RUN npm install --no-audit --no-fund
COPY tsconfig.json *.ts ./
RUN npm run build
USER node
CMD [ "npm", "start" ]
13 changes: 13 additions & 0 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ const createDependenciesDescriptor = async (destDir, { lang }) => {
return fsPromises.writeFile(resultFile, minimalPackageJson)
}

const createDockerfile = async (destDir, lang) => {
if (lang == 'go') {
return
}
const fileName = 'Dockerfile'
console.log('Generate', fileName)

const resultFile = path.join(destDir, fileName)

return fsPromises.copyFile(`${__dirname}/templates/${fileName}.${lang}`, resultFile)
}

const createTypeScriptConfig = async (destDir, lang) => {
if (lang !== 'ts') {
return
Expand Down Expand Up @@ -361,6 +373,7 @@ const main = async (argv) => {
await createEndpoints(destDir, argv, config)
await createDependenciesDescriptor(destDir, argv)
await createTypeScriptConfig(destDir, argv.lang)
await createDockerfile(destDir, lang)

console.info('The application has been generated!')
console.info(generator.usageExampleAsText())
Expand Down
8 changes: 8 additions & 0 deletions src/templates/Dockerfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM node:18-bookworm
WORKDIR /opt/app
COPY package.json ./
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
RUN npm install --no-audit --no-fund
COPY *.js ./
USER node
CMD [ "npm", "start" ]
6 changes: 6 additions & 0 deletions src/templates/Dockerfile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
FROM python:3.7-bookworm
WORKDIR /opt/app
COPY requirements.txt ./
RUN pip install --no-cache-dir --upgrade -r requirements.txt
COPY *.py ./
CMD [ "uvicorn", "app:app", "--host", "0.0.0.0" ]
9 changes: 9 additions & 0 deletions src/templates/Dockerfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM node:18-bookworm
WORKDIR /opt/app
COPY package.json ./
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
RUN npm install --no-audit --no-fund
COPY tsconfig.json *.ts ./
RUN npm run build
USER node
CMD [ "npm", "start" ]

0 comments on commit 00a1f9d

Please sign in to comment.