diff --git a/.github/workflows/express-hub.yaml b/.github/workflows/express-hub.yaml new file mode 100644 index 0000000..ff103b8 --- /dev/null +++ b/.github/workflows/express-hub.yaml @@ -0,0 +1,14 @@ +name: Registry express proxy to Docker Hub +on: [workflow_dispatch] +jobs: + dockerize: + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: build image + run: docker build ./express-proxy -t piotrekdockerxd/express-proxy:latest + - name: push image to docker hub + run: | + docker login -u piotrekdockerxd -p ${{ secrets.DOCKER_HUB_TOKEN }} + docker push piotrekdockerxd/express-proxy:latest diff --git a/.github/workflows/fastify-hub.yaml b/.github/workflows/fastify-hub.yaml new file mode 100644 index 0000000..a26be44 --- /dev/null +++ b/.github/workflows/fastify-hub.yaml @@ -0,0 +1,14 @@ +name: Registry fastify proxy to Docker Hub +on: [workflow_dispatch] +jobs: + dockerize: + runs-on: ubuntu-latest + steps: + - name: checkout + uses: actions/checkout@v4 + - name: build image + run: docker build ./fastify-proxy -t piotrekdockerxd/fastify-proxy:latest + - name: push image to docker hub + run: | + docker login -u piotrekdockerxd -p ${{ secrets.DOCKER_HUB_TOKEN }} + docker push piotrekdockerxd/fastify-proxy:latest diff --git a/.github/workflows/main.yaml b/.github/workflows/render.yaml similarity index 72% rename from .github/workflows/main.yaml rename to .github/workflows/render.yaml index 0bb0a35..5a45bd1 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/render.yaml @@ -1,15 +1,12 @@ -name: My Deploy - -on: - push: - branches: [main] - pull_request: - branches: [main] +name: Render Deploy +on: [workflow_dispatch] jobs: build: runs-on: ubuntu-latest steps: + - name: checkout + uses: actions/checkout@v4 - name: Deploy to production uses: johnbeynon/render-deploy-action@v0.0.8 with: diff --git a/express-proxy/.dockerignore b/express-proxy/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/express-proxy/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/express-proxy/swagger-definition.yaml b/express-proxy/swagger-definition.yaml index 96689e3..26478cd 100644 --- a/express-proxy/swagger-definition.yaml +++ b/express-proxy/swagger-definition.yaml @@ -6,7 +6,7 @@ info: apis: - "./index.js" paths: - /get/{path}: + /link/{path}: get: summary: Get request with url path tags: [Url proxy] @@ -30,7 +30,7 @@ paths: description: OK 500: description: Error - /post/{path}: + /link/{path}: post: summary: Post request with url path tags: [Url proxy] @@ -56,7 +56,7 @@ paths: description: OK 500: description: Error - /put/{path}: + /link/{path}: put: summary: Put request with url path tags: [Url proxy] @@ -83,7 +83,7 @@ paths: description: OK 500: description: Error - /delete/{path}: + /link/{path}: delete: summary: Delete request with url path tags: [Url proxy] diff --git a/fastify-proxy/.dockerignore b/fastify-proxy/.dockerignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/fastify-proxy/.dockerignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/fastify-proxy/dockerfile b/fastify-proxy/dockerfile new file mode 100644 index 0000000..223aa44 --- /dev/null +++ b/fastify-proxy/dockerfile @@ -0,0 +1,19 @@ +FROM node:latest + +WORKDIR /app + +COPY package.json yarn.lock ./ + +RUN yarn --pure-lockfile + +COPY . . + +RUN yarn build + +ARG PORT=3000 + +ENV PORT=$PORT + +EXPOSE $PORT + +CMD ["yarn", "start"] \ No newline at end of file diff --git a/fastify-proxy/src/config/axios.ts b/fastify-proxy/src/config/axios.ts new file mode 100644 index 0000000..21d74cc --- /dev/null +++ b/fastify-proxy/src/config/axios.ts @@ -0,0 +1,26 @@ +import axios, { AxiosInstance, InternalAxiosRequestConfig } from "axios"; + +const createAxiosWithAbortSupport = (timeout: number): AxiosInstance => { + const axiosInstance = axios.create({ + timeout: timeout, + }); + + axiosInstance.interceptors.request.use((config) => { + const controller = new AbortController(); + config.signal = controller.signal; + config.cancelToken = new axios.CancelToken((cancel) => { + (config as any).cancelRequest = () => { + controller.abort(); + cancel("Request canceled"); + }; + }); + + return config; + }); + + return axiosInstance; +}; + +const axiosInstance = createAxiosWithAbortSupport(10000); + +export default axiosInstance; diff --git a/fastify-proxy/src/modules/url/url.controller.ts b/fastify-proxy/src/modules/url/url.controller.ts index 6e4f96f..4075756 100644 --- a/fastify-proxy/src/modules/url/url.controller.ts +++ b/fastify-proxy/src/modules/url/url.controller.ts @@ -6,7 +6,7 @@ import { postUrlData, putUrlData, } from "./url.service"; -import { TUrlQuery, TErrorResponse, TUrlBody } from "./url.schema"; +import { TErrorResponse, TUrlBody, TUrlParam } from "./url.schema"; import { AxiosError } from "axios"; const errorHandler = (err: unknown): Omit => { @@ -26,10 +26,13 @@ const errorHandler = (err: unknown): Omit => { }; const getUrlHandler = async ( - request: FastifyRequest<{ Querystring: TUrlQuery }>, + request: FastifyRequest<{ Params: TUrlParam }>, reply: FastifyReply ) => { - const { link } = request.query; + // const link = decodeURIComponent(request.params.link); + const link = decodeURIComponent(request.params["*"]); + + console.log(`LINK:` + link); try { const response = await getUrlData(link); reply.status(200).send(response.data); @@ -43,10 +46,13 @@ const getUrlHandler = async ( }; const deletetUrlHandler = async ( - request: FastifyRequest<{ Querystring: TUrlQuery }>, + request: FastifyRequest<{ Params: TUrlParam }>, reply: FastifyReply ) => { - const { link } = request.query; + // const link = request.params.link; + const link = decodeURIComponent(request.params["*"]); + + console.log(link); try { const response = await deleteUrlData(link); reply.status(200).send(response.data); @@ -60,12 +66,13 @@ const deletetUrlHandler = async ( }; const postUrlHandler = async ( - request: FastifyRequest<{ Querystring: TUrlQuery; Body: TUrlBody }>, + request: FastifyRequest<{ Params: TUrlParam; Body: TUrlBody }>, reply: FastifyReply ) => { - const { link } = request.query; - const data = request.body; + // const link = request.params.link; + const link = decodeURIComponent(request.params["*"]); + const data = request.body; try { const response = await postUrlData(link, data); reply.status(response.status).send(response.data); @@ -79,10 +86,12 @@ const postUrlHandler = async ( }; const patchUrlHandler = async ( - request: FastifyRequest<{ Querystring: TUrlQuery; Body: TUrlBody }>, + request: FastifyRequest<{ Params: TUrlParam; Body: TUrlBody }>, reply: FastifyReply ) => { - const { link } = request.query; + // const link = request.params.link; + const link = decodeURIComponent(request.params["*"]); + const data = request.body; try { const response = await patchUrlData(link, data); @@ -97,10 +106,12 @@ const patchUrlHandler = async ( }; const putUrlHandler = async ( - request: FastifyRequest<{ Querystring: TUrlQuery; Body: TUrlBody }>, + request: FastifyRequest<{ Params: TUrlParam; Body: TUrlBody }>, reply: FastifyReply ) => { - const { link } = request.query; + // const link = request.params.link; + const link = decodeURIComponent(request.params["*"]); + const data = request.body; try { diff --git a/fastify-proxy/src/modules/url/url.hook.ts b/fastify-proxy/src/modules/url/url.hook.ts index 6065a5e..35ac9cc 100644 --- a/fastify-proxy/src/modules/url/url.hook.ts +++ b/fastify-proxy/src/modules/url/url.hook.ts @@ -1,19 +1,17 @@ import { FastifyReply, FastifyRequest } from "fastify"; -import { TUrlQuery } from "./url.schema"; +import { TUrlParam } from "./url.schema"; const preHandler = async ( - request: FastifyRequest<{ - Querystring: TUrlQuery; - }>, + request: FastifyRequest<{ Params: TUrlParam }>, reply: FastifyReply ) => { - const rawQuery = request.raw.url?.split("?link=")[1]; + const rawQuery = request.originalUrl.replace("/url/", ""); if (!rawQuery) { reply.status(400).send({ error: 'Missing "link" parameter' }); return; } - const encodeURL = decodeURIComponent(rawQuery); - request.query = { link: encodeURL }; + request.params["*"] = rawQuery; + console.log(`RAWQUERY`, rawQuery); }; export { preHandler }; diff --git a/fastify-proxy/src/modules/url/url.route.ts b/fastify-proxy/src/modules/url/url.route.ts index 58a3287..aa287f0 100644 --- a/fastify-proxy/src/modules/url/url.route.ts +++ b/fastify-proxy/src/modules/url/url.route.ts @@ -8,34 +8,35 @@ import { } from "./url.controller"; import { urlBodySchemaJson, - urlQuerySchemaJson, + urlParamSchemaJson, urlResponseRerrorSchemaJson, urlResponseSchemaJson, } from "./url.schema"; import { preHandler } from "./url.hook"; + const urlRoutes = async (server: FastifyInstance) => { server.get( - "/", + "/*", { schema: { tags: ["URL"], - querystring: urlQuerySchemaJson, + params: urlParamSchemaJson, response: { "2xx": urlResponseSchemaJson, "4xx": urlResponseRerrorSchemaJson, "5xx": urlResponseRerrorSchemaJson, }, }, - preHandler, + preHandler: preHandler, }, getUrlHandler ); server.post( - "/", + "/*", { schema: { tags: ["URL"], - querystring: urlQuerySchemaJson, + params: urlParamSchemaJson, body: urlBodySchemaJson, response: { "2xx": urlResponseSchemaJson, @@ -47,11 +48,11 @@ const urlRoutes = async (server: FastifyInstance) => { postUrlHandler ); server.delete( - "/", + "/*", { schema: { tags: ["URL"], - querystring: urlQuerySchemaJson, + params: urlParamSchemaJson, response: { "2xx": urlResponseSchemaJson, "4xx": urlResponseRerrorSchemaJson, @@ -62,11 +63,11 @@ const urlRoutes = async (server: FastifyInstance) => { deletetUrlHandler ); server.put( - "/", + "/*", { schema: { tags: ["URL"], - querystring: urlQuerySchemaJson, + params: urlParamSchemaJson, body: urlBodySchemaJson, response: { "2xx": urlResponseSchemaJson, @@ -78,11 +79,11 @@ const urlRoutes = async (server: FastifyInstance) => { putUrlHandler ); server.patch( - "/", + "/*", { schema: { tags: ["URL"], - querystring: urlQuerySchemaJson, + params: urlParamSchemaJson, body: urlBodySchemaJson, response: { "2xx": urlResponseSchemaJson, diff --git a/fastify-proxy/src/modules/url/url.schema.ts b/fastify-proxy/src/modules/url/url.schema.ts index 13ca44e..e64a585 100644 --- a/fastify-proxy/src/modules/url/url.schema.ts +++ b/fastify-proxy/src/modules/url/url.schema.ts @@ -1,8 +1,8 @@ import { z } from "zod"; import zodToJsonSchema from "zod-to-json-schema"; -export const urlQuerySchema = z.object({ - link: z.string().url({ message: "Has to be URL" }), +export const urlParamSchema = z.object({ + "*": z.string().url(), }); const urlBodySchema = z.record(z.any()); @@ -15,18 +15,18 @@ const urlResponseErrorSchema = z.object({ const urlResponseSchema = z.any(); const urlResponseRerrorSchemaJson = zodToJsonSchema(urlResponseErrorSchema); -const urlQuerySchemaJson = zodToJsonSchema(urlQuerySchema); +const urlParamSchemaJson = zodToJsonSchema(urlParamSchema); const urlResponseSchemaJson = zodToJsonSchema(urlResponseSchema); const urlBodySchemaJson = zodToJsonSchema(urlBodySchema); -type TUrlQuery = z.infer; +type TUrlParam = z.infer; type TErrorResponse = z.infer; type TUrlBody = z.infer; export { - urlQuerySchemaJson, + urlParamSchemaJson, urlResponseSchemaJson, urlBodySchemaJson, urlResponseRerrorSchemaJson, }; -export type { TUrlQuery, TErrorResponse, TUrlBody }; +export type { TUrlParam, TErrorResponse, TUrlBody }; diff --git a/fastify-proxy/src/modules/url/url.service.ts b/fastify-proxy/src/modules/url/url.service.ts index d4c7267..ba9b621 100644 --- a/fastify-proxy/src/modules/url/url.service.ts +++ b/fastify-proxy/src/modules/url/url.service.ts @@ -1,28 +1,28 @@ -import axios from "axios"; +import axiosInstance from "../../config/axios"; import { TJsonable } from "../../types/jsonable.types"; const getUrlData = async (link: string) => { - const response = await axios.get(link); + const response = await axiosInstance.get(link); return response; }; const deleteUrlData = async (link: string) => { - const response = await axios.delete(link); + const response = await axiosInstance.delete(link); return response; }; const postUrlData = async (link: string, body: TJsonable) => { - const response = await axios.post(link, body); + const response = await axiosInstance.post(link, body); return response; }; const putUrlData = async (link: string, body: TJsonable) => { - const response = await axios.put(link, body); + const response = await axiosInstance.put(link, body); return response; }; const patchUrlData = async (link: string, body: TJsonable) => { - const response = await axios.patch(link, body); + const response = await axiosInstance.patch(link, body); return response; }; diff --git a/fastify-proxy/src/server.ts b/fastify-proxy/src/server.ts index 1f1c86b..9d1449b 100644 --- a/fastify-proxy/src/server.ts +++ b/fastify-proxy/src/server.ts @@ -4,7 +4,7 @@ import env from "./config/env"; const main = async () => { const server = await buildApp(); try { - await server.listen({ port: env.PORT }); + await server.listen({ port: env.PORT, host: "0.0.0.0" }); } catch (error) { server.log.error("Can't startup server"); process.exit(1);