Skip to content

Commit

Permalink
gh actions ci cd & change method from query to wildcard path
Browse files Browse the repository at this point in the history
  • Loading branch information
Piootrekk committed Dec 7, 2024
1 parent e17f27d commit 8919ece
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 55 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/express-hub.yaml
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions .github/workflows/fastify-hub.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 4 additions & 7 deletions .github/workflows/main.yaml → .github/workflows/render.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
1 change: 1 addition & 0 deletions express-proxy/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
8 changes: 4 additions & 4 deletions express-proxy/swagger-definition.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ info:
apis:
- "./index.js"
paths:
/get/{path}:
/link/{path}:
get:
summary: Get request with url path
tags: [Url proxy]
Expand All @@ -30,7 +30,7 @@ paths:
description: OK
500:
description: Error
/post/{path}:
/link/{path}:
post:
summary: Post request with url path
tags: [Url proxy]
Expand All @@ -56,7 +56,7 @@ paths:
description: OK
500:
description: Error
/put/{path}:
/link/{path}:
put:
summary: Put request with url path
tags: [Url proxy]
Expand All @@ -83,7 +83,7 @@ paths:
description: OK
500:
description: Error
/delete/{path}:
/link/{path}:
delete:
summary: Delete request with url path
tags: [Url proxy]
Expand Down
1 change: 1 addition & 0 deletions fastify-proxy/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
19 changes: 19 additions & 0 deletions fastify-proxy/dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
26 changes: 26 additions & 0 deletions fastify-proxy/src/config/axios.ts
Original file line number Diff line number Diff line change
@@ -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;
35 changes: 23 additions & 12 deletions fastify-proxy/src/modules/url/url.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TErrorResponse, "path"> => {
Expand All @@ -26,10 +26,13 @@ const errorHandler = (err: unknown): Omit<TErrorResponse, "path"> => {
};

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down
12 changes: 5 additions & 7 deletions fastify-proxy/src/modules/url/url.hook.ts
Original file line number Diff line number Diff line change
@@ -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 };
25 changes: 13 additions & 12 deletions fastify-proxy/src/modules/url/url.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -47,11 +48,11 @@ const urlRoutes = async (server: FastifyInstance) => {
postUrlHandler
);
server.delete(
"/",
"/*",
{
schema: {
tags: ["URL"],
querystring: urlQuerySchemaJson,
params: urlParamSchemaJson,
response: {
"2xx": urlResponseSchemaJson,
"4xx": urlResponseRerrorSchemaJson,
Expand All @@ -62,11 +63,11 @@ const urlRoutes = async (server: FastifyInstance) => {
deletetUrlHandler
);
server.put(
"/",
"/*",
{
schema: {
tags: ["URL"],
querystring: urlQuerySchemaJson,
params: urlParamSchemaJson,
body: urlBodySchemaJson,
response: {
"2xx": urlResponseSchemaJson,
Expand All @@ -78,11 +79,11 @@ const urlRoutes = async (server: FastifyInstance) => {
putUrlHandler
);
server.patch(
"/",
"/*",
{
schema: {
tags: ["URL"],
querystring: urlQuerySchemaJson,
params: urlParamSchemaJson,
body: urlBodySchemaJson,
response: {
"2xx": urlResponseSchemaJson,
Expand Down
12 changes: 6 additions & 6 deletions fastify-proxy/src/modules/url/url.schema.ts
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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<typeof urlQuerySchema>;
type TUrlParam = z.infer<typeof urlParamSchema>;
type TErrorResponse = z.infer<typeof urlResponseErrorSchema>;
type TUrlBody = z.infer<typeof urlBodySchema>;

export {
urlQuerySchemaJson,
urlParamSchemaJson,
urlResponseSchemaJson,
urlBodySchemaJson,
urlResponseRerrorSchemaJson,
};
export type { TUrlQuery, TErrorResponse, TUrlBody };
export type { TUrlParam, TErrorResponse, TUrlBody };
Loading

0 comments on commit 8919ece

Please sign in to comment.