Skip to content

Commit

Permalink
1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillTolmachev committed Aug 4, 2023
1 parent c13b3c9 commit 0e572a8
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false
25 changes: 25 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Hostname or IP for the application
HOST=localhost

# Port number for the application
PORT=3000

# API URL for QuickBlox
# To use QuickBlox, you'll need to create an app and obtain the necessary API URL.
# Follow the instructions in the article "How to Create an App and Use QuickBlox Admin Panel" to create your app and find the appropriate API URL.
# Article link: https://quickblox.com/blog/how-to-create-an-app-and-use-quickblox-admin-panel/
QUICKBLOX_API_URL=https://api.quickblox.com


# API Key for AI API
# Replace <OPENAI_API_KEY> with your actual API key.
# You can obtain your API key from the OpenAI platform.
# Visit the OpenAI documentation for more information on authentication and obtaining an API key.
# Documentation link: https://platform.openai.com/docs/api-reference/authentication
OPENAI_API_KEY=<OPENAI_API_KEY>

# Path to SSL certificates (Optional)
# If you need an HTTPS connection for the application, set the path to the SSL certificates.
# If you leave these parameters empty, the application will be launched with an HTTP connection.
SSL_KEY_FILE=''
SSL_CERT_FILE=''
46 changes: 46 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"env": {
"node": true
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
}
},
"extends": [
"airbnb",
"prettier",
"plugin:prettier/recommended"
],
"plugins": [
"prettier"
],
"rules": {
"camelcase": "off",
"no-shadow": "off",
"no-underscore-dangle": "off",
"padding-line-between-statements": [
"error",
{
"blankLine": "always",
"prev": "*",
"next": "if"
},
{
"blankLine": "always",
"prev": "*",
"next": "return"
},
{
"blankLine": "always",
"prev": ["const", "let", "var"],
"next": "*"
},
{
"blankLine": "any",
"prev": ["const", "let", "var"],
"next": ["const", "let", "var"]
}
]
}
}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Node.js
node_modules/

# IDE
.vscode/

# Logs
*.log

# Environment variables
.env
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/node_modules
/public
package.json
package-lock.json
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 80,
"tabWidth": 2,
"useTabs": false,
"semi": false,
"singleQuote": true,
"trailingComma": "all",
"endOfLine": "lf",
"arrowParens": "always"
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 IlliaChemolosov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "qbaiproxy",
"version": "0.0.1",
"description": "",
"main": "server.js",
"scripts": {
"start": "node server.js",
"inspect": "node --inspect server.js",
"lint": "eslint ."
},
"author": "",
"license": "MIT",
"dependencies": {
"axios": "^1.4.0",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2"
},
"devDependencies": {
"eslint": "^8.46.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-prettier": "^8.9.0",
"eslint-plugin-prettier": "^5.0.0",
"prettier": "^3.0.0"
}
}
112 changes: 112 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
const fs = require('fs')
const https = require('https')
const express = require('express')
const cors = require('cors')
const bodyParser = require('body-parser')
const axios = require('axios').default

require('dotenv').config()

const app = express()
const httpsAgent = new https.Agent({
rejectUnauthorized: false,
})

const openAIApi = axios.create({
baseURL: 'https://api.openai.com',
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
},
})

app.use(cors())
app.use(bodyParser.json())

const qbTokenValidation = async (token) => {
try {
if (!token) return false

const { data } = await axios.get('session.json', {
baseURL: process.env.QUICKBLOX_API_URL,
headers: {
'QB-Token': token,
},
httpsAgent,
})

return Boolean(data.session && data.session.user_id)
} catch (error) {
return false
}
}

app.use(async (req, res) => {
try {
const excludedHeaders = [
'qb-token',
'accept',
'host',
'user-agent',
'content-length',
]
const headers = { ...req.headers }
const isValidToken = await qbTokenValidation(headers['qb-token'])

if (!isValidToken) {
res.status(403).send({
error: {
message: 'Invalid QB-Token header',
},
})

return
}

excludedHeaders.forEach((header) => {
delete headers[header]
})

const options = {
method: req.method,
url: req.originalUrl,
headers,
data: req.body,
httpsAgent,
}

const { data } = await openAIApi.request(options)

res.send(data)
} catch (error) {
let status = 500
let errorData = {
error: {
message: error.message,
},
}

if ('response' in error) {
status = error.response.status
errorData = error.response.data
}

res.status(status).send(errorData)
}
})

const runServerListener = () => {
console.log(`Server is running at port ${process.env.PORT}`)
}

if (process.env.SSL_KEY_FILE && process.env.SSL_CERT_FILE) {
const certificates = {
key: fs.readFileSync(process.env.SSL_KEY_FILE),
cert: fs.readFileSync(process.env.SSL_CERT_FILE),
}

https
.createServer(certificates, app)
.listen(process.env.PORT, process.env.HOST, runServerListener)
} else {
app.listen(process.env.PORT, process.env.HOST, runServerListener)
}

0 comments on commit 0e572a8

Please sign in to comment.