Skip to content

Commit 00a1f9d

Browse files
committed
feat: generate Dockerfiles for Express and FastAPI apps
Part of #42
1 parent 7db66f3 commit 00a1f9d

File tree

8 files changed

+62
-3
lines changed

8 files changed

+62
-3
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ Generates the endpoints (or a whole app) from a mapping (SQL query -> URL)
4747
1. Generate code
4848
| Language | Command for code generation | Example of generated files | Libraries |
4949
| -----------| ----------------------------| ---------------------------| --------- |
50-
| 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) |
51-
| 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) |
50+
| 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) |
51+
| 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) |
5252
| 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) |
53-
| 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/) |
53+
| 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/) |
5454

5555
1. Run the application
5656
| Language | Commands to run the application |

examples/js/express/mysql/Dockerfile

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:18-bookworm
2+
WORKDIR /opt/app
3+
COPY package.json ./
4+
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
5+
RUN npm install --no-audit --no-fund
6+
COPY *.js ./
7+
USER node
8+
CMD [ "npm", "start" ]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3.7-bookworm
2+
WORKDIR /opt/app
3+
COPY requirements.txt ./
4+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
5+
COPY *.py ./
6+
CMD [ "uvicorn", "app:app", "--host", "0.0.0.0" ]

examples/ts/express/mysql/Dockerfile

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:18-bookworm
2+
WORKDIR /opt/app
3+
COPY package.json ./
4+
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
5+
RUN npm install --no-audit --no-fund
6+
COPY tsconfig.json *.ts ./
7+
RUN npm run build
8+
USER node
9+
CMD [ "npm", "start" ]

src/cli.js

+13
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,18 @@ const createDependenciesDescriptor = async (destDir, { lang }) => {
321321
return fsPromises.writeFile(resultFile, minimalPackageJson)
322322
}
323323

324+
const createDockerfile = async (destDir, lang) => {
325+
if (lang == 'go') {
326+
return
327+
}
328+
const fileName = 'Dockerfile'
329+
console.log('Generate', fileName)
330+
331+
const resultFile = path.join(destDir, fileName)
332+
333+
return fsPromises.copyFile(`${__dirname}/templates/${fileName}.${lang}`, resultFile)
334+
}
335+
324336
const createTypeScriptConfig = async (destDir, lang) => {
325337
if (lang !== 'ts') {
326338
return
@@ -361,6 +373,7 @@ const main = async (argv) => {
361373
await createEndpoints(destDir, argv, config)
362374
await createDependenciesDescriptor(destDir, argv)
363375
await createTypeScriptConfig(destDir, argv.lang)
376+
await createDockerfile(destDir, lang)
364377

365378
console.info('The application has been generated!')
366379
console.info(generator.usageExampleAsText())

src/templates/Dockerfile.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
FROM node:18-bookworm
2+
WORKDIR /opt/app
3+
COPY package.json ./
4+
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
5+
RUN npm install --no-audit --no-fund
6+
COPY *.js ./
7+
USER node
8+
CMD [ "npm", "start" ]

src/templates/Dockerfile.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM python:3.7-bookworm
2+
WORKDIR /opt/app
3+
COPY requirements.txt ./
4+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
5+
COPY *.py ./
6+
CMD [ "uvicorn", "app:app", "--host", "0.0.0.0" ]

src/templates/Dockerfile.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM node:18-bookworm
2+
WORKDIR /opt/app
3+
COPY package.json ./
4+
ENV NPM_CONFIG_UPDATE_NOTIFIER=false
5+
RUN npm install --no-audit --no-fund
6+
COPY tsconfig.json *.ts ./
7+
RUN npm run build
8+
USER node
9+
CMD [ "npm", "start" ]

0 commit comments

Comments
 (0)