Subscripciones iniciales y cliente para leer webhook
This commit is contained in:
1
.env
1
.env
@@ -12,5 +12,6 @@ SERVER_PORT=3300
|
||||
|
||||
# CLIENTE
|
||||
# Cliente simulado que va a recibir los webhooks
|
||||
# puramente de debug para el servicio
|
||||
CLIENT_IP="127.0.0.1"
|
||||
CLIENT_PORT=3000
|
||||
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1 +1,3 @@
|
||||
node_modules
|
||||
dist
|
||||
|
||||
|
||||
29
README.md
Normal file
29
README.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Proyecto de automatizacion de desarrollo
|
||||
|
||||
Tiene como objetivo levantar un servidor de Webhooks para servir al
|
||||
desarrollo de shopify de forma local.
|
||||
|
||||
Componentes:
|
||||
|
||||
- Servidor de webhooks que emite peticiones
|
||||
- Cliente (servidor express) que escucha al servidor para desarrollo
|
||||
|
||||
Objetivos:
|
||||
|
||||
- [x] Generar eventos por API REST
|
||||
- [x] Subscripción a topics por API REST
|
||||
- [x] Documentación de la API REST con Bruno
|
||||
- [x] Firma de mesajes con HMAC sha256
|
||||
- [ ] Generar eventos periodicos
|
||||
- [ ] Generar ordenes de shopify proceduralmente
|
||||
- [ ] Generar ciclos de vida realistas create -> update -> delete
|
||||
- [ ] Dockerizar
|
||||
- [ ] Añadir clientes iniciales
|
||||
|
||||
Webhooks Soportados:
|
||||
|
||||
- [ ] orders/create
|
||||
- [ ] orders/update
|
||||
- [ ] orders/delete
|
||||
|
||||
Propuestas:
|
||||
27
src/client.ts
Normal file
27
src/client.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import { handleWebhook } from '#controllers/webhook.js';
|
||||
import { verifyWebhookSignature } from '#middleware/verifySig.js';
|
||||
|
||||
const client = express();
|
||||
|
||||
// Middleware
|
||||
client.use(cors());
|
||||
|
||||
client.use(express.json({
|
||||
verify: (req: any, res, buf) => {
|
||||
req.rawBody = buf;
|
||||
}
|
||||
}));
|
||||
client.use(express.urlencoded({ extended: true }));
|
||||
|
||||
// Webhook reception
|
||||
client.post('/order/create', verifyWebhookSignature, handleWebhook);
|
||||
|
||||
// Health check
|
||||
client.get('/health', (req, res) => {
|
||||
res.status(200).send({ resp: 'OK' });
|
||||
});
|
||||
|
||||
export default client;
|
||||
@@ -2,7 +2,7 @@ import dotenv from 'dotenv';
|
||||
dotenv.config();
|
||||
|
||||
export const config = {
|
||||
port: process.env.PORT || 3000,
|
||||
port: process.env.PORT || 3300,
|
||||
webhookSecret: process.env.WEBHOOK_SECRET || 'default_secret_change_me',
|
||||
env: process.env.NODE_ENV || 'development',
|
||||
};
|
||||
@@ -13,3 +13,7 @@ export const webhooksConfig = {
|
||||
clientIP: process.env.CLIENT_IP,
|
||||
clientPort: process.env.CLIENT_PORT,
|
||||
}
|
||||
|
||||
export const clientConfig = {
|
||||
port: process.env.CLIENT_PORT ?? 3000
|
||||
}
|
||||
|
||||
55
src/config/initial_hosts.ts
Normal file
55
src/config/initial_hosts.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Configuracion de los host iniciales y a que topics escuchan
|
||||
*/
|
||||
|
||||
import { SubscriptionData, SubscriptorData } from "#controllers/subscriptions.js"
|
||||
import { webhooksConfig } from "."
|
||||
|
||||
|
||||
type SetupSubscription = SubscriptorData & {
|
||||
topic: string,
|
||||
options: {
|
||||
/* En ms. Cada cuanto se envia un mensaje nuevo, para solo se envia un mensaje */
|
||||
period?: number,
|
||||
/* Numero de mensajes, para undefiend solo se manda uno, para <= 0 seran infinitos */
|
||||
mesages?: number,
|
||||
}
|
||||
}
|
||||
|
||||
export function generateSubscriptions(initialHosts?: SetupSubscription[]) {
|
||||
const initialhs = initialHosts || INITIAL_HOSTS
|
||||
const topicSubscriberMap = new Map<string, SubscriptionData>()
|
||||
|
||||
for (const setup of initialhs) {
|
||||
const topic = setup.topic
|
||||
if (topicSubscriberMap.get(topic) == undefined) {
|
||||
topicSubscriberMap.set(topic, {
|
||||
topic: topic,
|
||||
subscriptors: []
|
||||
})
|
||||
}
|
||||
topicSubscriberMap.get(topic)!.subscriptors.push({
|
||||
...setup
|
||||
})
|
||||
}
|
||||
|
||||
return topicSubscriberMap
|
||||
}
|
||||
|
||||
|
||||
export const INITIAL_HOSTS: SetupSubscription[] = [
|
||||
{
|
||||
topic: "order/create",
|
||||
host: "localhost",
|
||||
port: "3000",
|
||||
endpoint: "/order/create",
|
||||
method: "POST",
|
||||
secretkey: webhooksConfig.apiSecret || "1234",
|
||||
open: new Date,
|
||||
options: {
|
||||
period: 1000
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
import { Router } from 'express';
|
||||
import { ordersHandlerBuilder } from '#controllers/orders.webhook.js';
|
||||
import { SubscriptionManager, subscriptonHandlerBuilder } from '#controllers/subscriptions.js';
|
||||
import { generateSubscriptions } from '#config/initial_hosts.js';
|
||||
|
||||
const webhookRouter = Router();
|
||||
|
||||
const subscriptions = new SubscriptionManager()
|
||||
const baseSubscriptions = generateSubscriptions()
|
||||
const subscriptions = new SubscriptionManager(baseSubscriptions)
|
||||
|
||||
// subto
|
||||
webhookRouter.post('/subto', subscriptonHandlerBuilder(subscriptions));
|
||||
|
||||
@@ -1,9 +1,16 @@
|
||||
import app from './app';
|
||||
import { config } from './config/index';
|
||||
import { clientConfig, config } from './config/index';
|
||||
import client from './client'
|
||||
|
||||
const PORT = config.port;
|
||||
const CLIENT_PORT = clientConfig.port;
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running on port ${PORT} in ${config.env} mode`);
|
||||
console.log(`Webhook endpoint: http://localhost:${PORT}/api/webhooks`);
|
||||
});
|
||||
|
||||
|
||||
client.listen(CLIENT_PORT, () => {
|
||||
console.log(`Client ins running on port ${CLIENT_PORT}`)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user