www/api/index.js

66 lines
2.1 KiB
JavaScript

'use strict'
require('dotenv').config()
const {json} = require('express')
const express = require('express')
const CORS_OPTIONS = {
'origin': process.env.SITE_DOMAIN,
'methods': 'GET,POST,OPTIONS,DELETE',
}
const cors = require('cors')(CORS_OPTIONS)
const jsonBodyParser = express.json()
const app = express()
const httpServer = require('http').createServer(app)
const { Server } = require('socket.io')
const axios = require('axios')
const truncateXmrAddr = (addr) =>
`${addr.slice(0,4)}..${addr.slice(-4)}`
//const io = new Server(httpServer, {cors:CORS_OPTIONS})
httpServer.listen(Number(process.env.API_PORT))
// Healthcheck the api server, perchance with uptime kuma
app.get('/', (req, res) => res.sendStatus(200))
// Create new order
app.route('/order').options(cors)
.post(jsonBodyParser, cors, require('./order-add'))
// Get or delete an order
app.route('/order/:orderid').options(cors)
.get(cors, require('./order-get'))
.delete(cors, require('./order-del'))
// XMR transactions polling (just proxying it to moneropay)
app.get('/xmr-receive/:addr', cors, async (req, res) => {
const ip = req.ip.slice(7)
const url = `${process.env.MONEROPAY_URL}/receive/${req.params.addr}`
let proxRes; try {
proxRes = await axios.get(url)
} catch (err) {
return console.error(`Failed to POST ${url}\n${err}`)
}
return res.status(proxRes.status).send(proxRes.data)
})
// Callback from moneropay
// https://moneropay.eu/api/callback.html
//app.options('/moneropay-cb/:orderid', cors)
// .post('/moneropay-cb/:orderid', jsonBodyParser, cors,
// (req) => {
// console.log(`Received cb: ${req.params.orderid}\n${req.body}`)
// io.to(req.params.orderid).emit('mp-cb',req.body)
// }
// )
//io.on('connection', (socket) => {
// // TODO: Disconnect if no orderid in query
// console.log(`Client joined for order ${socket.handshake.query.orderid}`)
// socket.join(socket.handshake.query.orderid)
//})
// Send email through contact form
app.options('/contact', cors)
.post('/contact', jsonBodyParser, cors, require('./contact'))
// Send bug report through disclosure form
app.options('/bug', cors)
.post('/bug', jsonBodyParser, cors, require('./bug'))