Skip to content

Commit

Permalink
alias: send sync message to web server
Browse files Browse the repository at this point in the history
  • Loading branch information
paolini committed Oct 16, 2024
1 parent 62f2a55 commit f8e184d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dm-manager",
"version": "1.9.1",
"version": "1.9.2",
"private": true,
"dependencies": {
"@babel/core": "^7.16.0",
Expand Down
2 changes: 1 addition & 1 deletion server/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Options {
UNIPI_TOKEN: "",
UNIPI_TOKENARPI: "",
UNIPI_TOKENARPILINK: "",

WEB_PAGES_SERVER_UPDATE_URL: "http://web.cdc:8000/alias/reload",
}
Object.entries(options).forEach(([key, val]) => {
this[key] = process.env[key] || val
Expand Down
28 changes: 28 additions & 0 deletions server/controllers/processes/urls.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
const express = require('express')
const assert = require('assert')
const { ObjectId } = require('mongoose').Types
const axios = require('axios')

const Url = require('../../models/Url')
const { log } = require('../middleware')

const router = express.Router()
module.exports = router

const config = require('../../config')
const { sync } = require('resolve')


router.get('/', async (req, res) => {
if (!req.user) {
res.status(401).json({
Expand All @@ -23,6 +28,20 @@ router.get('/', async (req, res) => {
res.json({ data })
})

async function sync_web_server() {
// make a GET request to http://web.cdc:8000/alias/reload
// and check for error code
const url = config.WEB_PAGES_SERVER_UPDATE_URL
try {
const response = await axios.get(url)
console.log(`sync_web_server: ${response.status} ${response.statusText}`)
return true
} catch(error) {
console.error(`sync_web_server: ${error}`)
return false
}
}

router.delete('/:id', async (req, res) => {
console.log(`my DELETE ${req.params.id}`)
try {
Expand All @@ -40,6 +59,9 @@ router.delete('/:id', async (req, res) => {
error: "Cannot delete urls created by other users"
})
}

if (!await sync_web_server()) return res.status(500).json({error: "web server sync failed"})

} catch(error) {
res.status(400).json({
error: error.message
Expand Down Expand Up @@ -157,6 +179,9 @@ router.patch('/:id', async (req, res) => {
await url.save()
// await notifySeminar(seminar)
await log(req, was, payload)

if (!await sync_web_server()) return res.status(500).json({error: "web server sync failed"})

res.send({})
}
catch (error) {
Expand Down Expand Up @@ -186,6 +211,9 @@ router.put('/', async (req, res) => {
await url.save()
// await notifySeminar(seminar)
await log(req, {}, payload)

if (!await sync_web_server()) return res.status(500).json({error: "web server sync failed"})

res.send({})
}
catch (error) {
Expand Down

0 comments on commit f8e184d

Please sign in to comment.