Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: manager #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,31 @@ coverage/*
data/medias/*
data/stores/*
data/sessions/*
.env

# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*

node_modules
dist
dist-ssr
*.local

# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

./.env
32 changes: 30 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
"scripts": {
"cloud": "node dist/cloud.js",
"lint": "eslint src",
"lint:ui": "eslint ui",
"test": "jest --coverage",
"build": "./node_modules/typescript/bin/tsc -p .",
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
"dev": "./node_modules/nodemon/bin/nodemon.js src/index.ts",
"dev:frontend": "vite",
"build:frontend": "vite build",
"dev:backend": "./node_modules/nodemon/bin/nodemon.js src/index.ts",
"dev": "concurrently 'yarn:dev:backend' 'yarn:dev:frontend' ",
"web-dev": "./node_modules/nodemon/bin/nodemon.js src/web.ts",
"worker-dev": "./node_modules/nodemon/bin/nodemon.js src/worker.ts",
"cloud-dev": "./node_modules/nodemon/bin/nodemon.js src/cloud.ts",
Expand All @@ -40,9 +44,17 @@
"@types/ws": "^8.5.4",
"@typescript-eslint/eslint-plugin": "^5.53.0",
"@typescript-eslint/parser": "^5.53.0",
"@vitejs/plugin-vue": "^4.0.0",
"vite": "^4.2.0",
"vite-plugin-vuetify": "^1.0.0",
"unplugin-fonts": "^1.0.3",
"roboto-fontface": "*",
"concurrently": "^7.6.0",
"sass": "^1.60.0",
"eslint": "^8.34.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-vue": "^9.3.0",
"jest": "^29.4.3",
"jest-fetch-mock": "^3.0.3",
"jest-mock-extended": "^3.0.3",
Expand All @@ -60,10 +72,12 @@
"@google-cloud/text-to-speech": "^4.2.2",
"@redis/client": "^1.5.6",
"@whiskeysockets/baileys": "^6.6.0",
"@mdi/font": "7.0.96",
"amqplib": "^0.10.3",
"awesome-phonenumber": "^6.2.0",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"ejs": "^3.1.8",
"jimp": "^0.22.10",
"jschardet": "^3.0.0",
"link-preview-js": "^3.0.4",
Expand All @@ -77,6 +91,20 @@
"uuid": "^9.0.0",
"vcf": "^2.1.1",
"xlsx": "^0.18.5",
"yaml": "^2.2.1"
"yaml": "^2.2.1",
"axios": "^1.6.0",
"vue": "^3.2.0",
"vue-i18n": "^9.8.0",
"vue-router": "^4.0.0",
"vue3-markdown": "^1.1.9",
"vuetify": "^3.4.0",
"ansi-colors": "^4.1.3",
"cli-progress": "^3.12.0",
"core-js": "^3.29.0",
"inquirer": "^8.0.0",
"mongoose": "^8.0.3",
"optimist": "^0.6.1",
"pinia": "^2.0.0",
"semver": "^7.5.4"
}
}
Binary file added public/favicon.ico
Binary file not shown.
Binary file added public/unoapi_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 27 additions & 0 deletions src/admin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import express from 'express'
import fs from 'fs/promises'
import path from 'path'

const router = express.Router()

const environment = process.env.NODE_ENV

router.get('/', async (_req, res) => {
const data = {
environment,
manifest: await parseManifest(),
}

res.render('index.html.ejs', data)
})

const parseManifest = async () => {
if (environment !== 'production') return {}

const manifestPath = path.join(path.resolve(), 'dist', 'manifest.json')
const manifestFile = await fs.readFile(manifestPath, 'utf8')

return JSON.parse(manifestFile)
}

export default router
12 changes: 12 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express, { Application } from 'express'
import { Request, Response, NextFunction, Router } from 'express'
import path from 'path'

import { router } from './router'

Expand All @@ -11,6 +12,9 @@ import { Outgoing } from './services/outgoing'
import middleware from './services/middleware'
import injectRoute from './services/inject_route'

import admin from './admin'
import assets from './assets'

export class App {
public server: Application

Expand Down Expand Up @@ -42,6 +46,14 @@ export class App {
middleware: middleware,
injectRoute: injectRoute,
) {
if (process.env.NODE_ENV === 'production') {
this.server.use('/', express.static(path.join(path.resolve(), 'dist')))
} else {
this.server.use('/', express.static(path.join(path.resolve(), 'public')))
this.server.use('/ui/assets', assets)
}
this.server.use('/node_modules', express.static(path.join(path.resolve(), 'node_modules')))
this.server.use(admin)
const roter = router(incoming, outgoing, baseUrl, getConfig, getClient, middleware, injectRoute)
this.server.use(roter)
}
Expand Down
16 changes: 16 additions & 0 deletions src/assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import express from 'express'

const router = express.Router()

const supportedAssets = ['svg', 'png', 'jpg', 'png', 'jpeg', 'mp4', 'ogv']

const assetExtensionRegex = () => {
const formattedExtensionList = supportedAssets.join('|')
return new RegExp(`/.+\.(${formattedExtensionList})$`)
}

router.get(assetExtensionRegex(), (req, res) => {
res.redirect(303, `http://localhost:5173/ui/assets${req.path}`)
})

export default router
42 changes: 42 additions & 0 deletions src/controllers/manager_controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Request, Response } from 'express'
import fs from 'fs'
import logger from '../services/logger'
import { clients } from '../services/client_baileys'

const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'))

export class ManagerController {
public async info(req: Request, res: Response) {
logger.debug('manager info method %s', req.method)
logger.debug('manager info headers %s', JSON.stringify(req.headers))
logger.debug('manager info params %s', JSON.stringify(req.params))
logger.debug('manager info body %s', JSON.stringify(req.body))
logger.debug('manager info query', JSON.stringify(req.query))
try {
return res.status(200).json({
message: 'Welcome to the UnoAPI Manager!',
version: packageJson.version,
documentation: `${req.protocol}://${req.get('host')}/docs`,
manager: `${req.protocol}://${req.get('host')}/manager`,
})
} catch (e) {
return res.status(500).json({ status: 'error', message: e.message })
}
}

public async list(req: Request, res: Response) {
logger.debug('info method %s', req.method)
logger.debug('info headers %s', JSON.stringify(req.headers))
logger.debug('info params %s', JSON.stringify(req.params))
logger.debug('info body %s', JSON.stringify(req.body))
logger.debug('info query', JSON.stringify(req.query))
try {
const keys = Object.keys(clients)
return res.status(200).json({
keys,
})
} catch (e) {
return res.status(500).json({ status: 'error', message: e.message })
}
}
}
3 changes: 3 additions & 0 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { TemplatesController } from './controllers/templates_controller'
import { MessagesController } from './controllers/messages_controller'
import { MediaController } from './controllers/media_controller'
import { SessionController } from './controllers/session_controller'
import { ManagerController } from './controllers/manager_controller'

export const router = (
incoming: Incoming,
Expand All @@ -27,13 +28,15 @@ export const router = (
injectRoute: injectRoute = async (router: Router) => {},
) => {
const router: Router = Router()
const managerController = new ManagerController()
const messagesController = new MessagesController(incoming, outgoing)
const mediaController = new MediaController(baseUrl, getConfig)
const templatesController = new TemplatesController(getConfig)
const sessionController = new SessionController(incoming, outgoing, getConfig, getClient)

//Routes
router.get('/ping', indexController.ping)
router.get('/manager', middleware, managerController.info.bind(managerController))
router.get('/:phone/session', middleware, sessionController.info.bind(sessionController))
router.post('/:phone/session', middleware, sessionController.create.bind(sessionController))
router.delete('/:phone/session', middleware, sessionController.delete.bind(sessionController))
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"declaration": true,
"lib": ["es2020", "esnext.array", "DOM", "ES2021.String"]
},
"include": ["src/**/*"],
"include": ["src/**/*", "ui/**/*"],
"ts-node": {
"compilerOptions": {
"module": "commonjs"
Expand Down
22 changes: 22 additions & 0 deletions ui/App.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<template>
<router-view />
</template>

<script setup>
//
</script>

<style lang="scss">
@for $i from 0 through 8 {
.gap-x-#{$i} {
column-gap: #{$i * 0.25}rem;
}
.gap-y-#{$i} {
row-gap: #{$i * 0.25}rem;
}

.gap-#{$i} {
gap: #{$i * 0.25}rem;
}
}
</style>
Binary file added ui/assets/chatwoot/chatwoot_api.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/chatwoot/chatwoot_api_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/chatwoot/chatwoot_api_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/chatwoot/chatwoot_api_3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/logo.ico
Binary file not shown.
Binary file added ui/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/assets/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/logo_black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/assets/logo_black.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/logo_green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/assets/logo_green.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/logo_square.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/logo_white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions ui/assets/logo_white.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ui/assets/qrcode-pix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading