Skip to content

Commit

Permalink
refactor docs & deploy
Browse files Browse the repository at this point in the history
  • Loading branch information
Piootrekk committed Feb 2, 2024
1 parent 49a66a2 commit 3a7ca67
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 150 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Proxy CI/CD

on:
push:
branches:
- main

jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
steps:
- name: Render deploy
uses: johnbeynon/[email protected]
with:
service-id: ${{ secrets.RENDER_HOOK }}
api-key: ${{ secrets.RENDER_KEY }}
137 changes: 26 additions & 111 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,69 @@
const express = require("express");
const axios = require("axios");
const swaggerJsdoc = require("swagger-jsdoc");
const swaggerUi = require("swagger-ui-express");
const YAML = require("yamljs");

const app = express();
const port = 3001;
app.listen(port, () => console.log(`Server listening on port ${port}`));
app.use(express.json());
const swaggerDocument = YAML.load("./swagger-definition.yaml");

const swaggerOptions = {
definition: {
openapi: "3.1.0",
info: {
title: "Express API Proxy with Swagger",
version: "1.0.0",
description: "A simple Express API proxy with Swagger documentation",
},
},
apis: ["./index.js"],
};

const swaggerSpec = swaggerJsdoc(swaggerOptions);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.listen(port, () => console.log(`Server listening on port ${port}`));

app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
next();
});


app.get("/", (req, res) => {
res.redirect("/api-docs");
});

/**
* @swagger
* /get/{path}:
* get:
* summary: Get request with url path
* tags: [Url proxy]
* description: Normal or encoded url path
* parameters:
* - in: path
* name: path
* schema:
* type: string
* required: true
* examples:
* examlpe1:
* value: https%3A%2F%2Fsteamcommunity.com%2Fmarket%2Fpriceoverview%2F%3Fcurrency%3D6%26market_hash_name%3DUnusual%2520Mean%2520Captain%26appid%3D440%26language%3Dpolish
* example2:
* value: https://steamcommunity.com/market/priceoverview/?currency=6&market_hash_name=Unusual%20Mean%20Captain&appid=440&language=polish
* responses:
* 200:
* description: OK
* 500:
* description: Error
*/
app.get("/get/:path(*)", async (req, res) => {
try {
const decodedPath = decodeURIComponent(req.params.path);
console.log("Received request to /getNormal with path:", decodedPath);
console.log("Received request to /get with path:", decodedPath);

const response = await axios.get(decodedPath, {
params: req.query,
});

res.json(response.data);
} catch (error) {
console.error("Error in /getNormal endpoint:", error);
console.error("Error in /get endpoint:", error);
res.status(500).json({
error: "An error occurred while processing the API request.",
path: req.params.path,
});
}
});

/**
* @swagger
* /post/{path}:
* post:
* summary: Post request with url path
* tags: [Url proxy]
* description: Normal or encoded path
* parameters:
* - in: path
* name: path
* schema:
* type: string
* required: true
* responses:
* 200:
* description: OK
* 500:
* description: Error
*/
app.post("/post/:path(*)", async (req, res) => {
try {
const requestData = req.body;
console.log(requestData);
const decodedPath = decodeURIComponent(req.params.path);
console.log("Received POST request to /postNormal with path:", decodedPath);
console.log("Received POST request to /post with path:", decodedPath);

const response = await axios.post(decodedPath, req.body, {
headers: {
"Content-Type": "application/json",
},
});

const response = await axios.post(decodedPath, req.body);
res.json(response.data);
} catch (error) {
console.error("Error in /postNormal endpoint:", error);
console.error("Error in /post endpoint:", error);
res.status(500).json({
error: "An error occurred while processing the API request.",
path: req.params.path,
});
}
});

/**
* @swagger
* /put/{path}:
* put:
* summary: Put request with url path
* tags: [Url proxy]
* description: Normal or encoded path
* parameters:
* - in: path
* name: path
* schema:
* type: string
* required: true
* responses:
* 200:
* description: OK
* 500:
* description: Error
*/
app.put("/put/:path(*)", async (req, res) => {
try {
const decodedPath = decodeURIComponent(req.params.path);
Expand All @@ -137,51 +72,31 @@ app.put("/put/:path(*)", async (req, res) => {
const response = await axios.put(decodedPath, req.body);
res.json(response.data);
} catch (error) {
console.error("Error in /putNormal endpoint:", error);
console.error("Error in /put endpoint:", error);
res.status(500).json({
error: "An error occurred while processing the API request.",
path: req.params.path,
});
}
});
/**
* @swagger
* /delete/{path}:
* delete:
* summary: Delete request with url path
* tags: [Url proxy]
* description: Normal or encoded path
* parameters:
* - in: path
* name: path
* schema:
* type: string
* required: true
* responses:
* 200:
* description: OK
* 500:
* description: Error
*/

app.delete("/delete/:path(*)", async (req, res) => {
try {
const decodedPath = decodeURIComponent(req.params.path);
console.log(
"Received DELETE request to /deleteNormal with path:",
decodedPath
);
console.log("Received DELETE request to /delete with path:", decodedPath);

const response = await axios.delete(decodedPath);
res.json(response.data);
} catch (error) {
console.error("Error in /deleteNormal endpoint:", error);
console.error("Error in /delete endpoint:", error);
res.status(500).json({
error: "An error occurred while processing the API request.",
path: req.params.path,
});
}
});

app.get("/api-docs", (req, res) => {
res.json(swaggerSpec);
app.post("/test", (req, res) => {
console.log(req.body);
res.json(req.body);
});
103 changes: 65 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"axios": "^1.6.3",
"express": "^4.18.2",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0"
"swagger-ui-express": "^5.0.0",
"yamljs": "^0.3.0"
},
"devDependencies": {
"nodemon": "^3.0.2"
Expand Down
Loading

0 comments on commit 3a7ca67

Please sign in to comment.