11import * as helper from './helper.js' ;
22import Fastify from 'fastify' ;
33
4+ const endpointID = helper . id ( ) ;
5+ const cdnUrlBase = `https://cdn.jsdelivr.net/gh/securelay/jsonbin@main/${ endpointID } ` ;
46const bodyLimit = parseInt ( process . env . BODYLIMIT ) ;
57const fieldLimit = parseInt ( process . env . FIELDLIMIT ) ;
68const webhookTimeout = parseInt ( process . env . WEBHOOK_TIMEOUT ) ;
@@ -42,7 +44,7 @@ fastify.get('/', (request, reply) => {
4244fastify . get ( '/id' , ( request , reply ) => {
4345 const app = request . query . app ;
4446 if ( app == null ) {
45- reply . send ( helper . id ( ) ) ;
47+ reply . send ( endpointID ) ;
4648 } else {
4749 const OneSignalID = helper . OneSignalID ( app ) ;
4850 if ( OneSignalID ) {
@@ -172,9 +174,15 @@ fastify.post('/private/:privateKey', async (request, reply) => {
172174 const redirectOnErr = request . query . err ;
173175 try {
174176 if ( helper . validate ( privateKey ) !== 'private' ) throw 401 ;
175- await helper . privateProduce ( privateKey , JSON . stringify ( helper . decoratePayload ( request . body ) ) ) ;
177+ //await helper.privateProduce(privateKey, JSON.stringify(helper.decoratePayload(request.body)));
178+ if ( ! await helper . githubPushJSON ( privateKey , request . body ) ) throw 500 ;
176179 if ( redirectOnOk == null ) {
177- reply . send ( { message : "Done" , error : "Ok" , statusCode : reply . statusCode } ) ;
180+ reply . send ( {
181+ message : "Done" ,
182+ error : "Ok" ,
183+ statusCode : reply . statusCode ,
184+ cdn : `${ cdnUrlBase } /${ helper . genPublicKey ( privateKey ) } .json`
185+ } ) ;
178186 } else {
179187 reply . redirect ( redirectOnOk , 303 ) ;
180188 }
@@ -195,7 +203,8 @@ fastify.delete('/private/:privateKey', async (request, reply) => {
195203 const { privateKey } = request . params ;
196204 try {
197205 if ( helper . validate ( privateKey ) !== 'private' ) throw 401 ;
198- await helper . privateDelete ( privateKey ) ;
206+ //await helper.privateDelete(privateKey);
207+ if ( ! await helper . githubPushJSON ( privateKey , null , true ) ) throw 500 ;
199208 reply . code ( 204 ) ;
200209 } catch ( err ) {
201210 if ( err == 401 ) {
@@ -210,8 +219,14 @@ fastify.patch('/private/:privateKey', async (request, reply) => {
210219 const { privateKey } = request . params ;
211220 try {
212221 if ( helper . validate ( privateKey ) !== 'private' ) throw 401 ;
213- await helper . privateRefresh ( privateKey ) ;
214- reply . send ( { message : "Done" , error : "Ok" , statusCode : reply . statusCode } ) ;
222+ //await helper.privateRefresh(privateKey);
223+ if ( ! await helper . githubPushJSON ( privateKey ) ) throw 500 ;
224+ reply . send ( {
225+ message : "Done" ,
226+ error : "Ok" ,
227+ statusCode : reply . statusCode ,
228+ cdn : `${ cdnUrlBase } /${ helper . genPublicKey ( privateKey ) } .json`
229+ } ) ;
215230 } catch ( err ) {
216231 if ( err == 401 ) {
217232 callUnauthorized ( reply , 'Provided key is not Private' ) ;
@@ -225,9 +240,13 @@ fastify.get('/public/:publicKey', async (request, reply) => {
225240 const { publicKey } = request . params ;
226241 try {
227242 if ( helper . validate ( publicKey ) !== 'public' ) throw 401 ;
228- const data = await helper . publicConsume ( publicKey ) ;
229- if ( ! data ) throw 404 ;
230- reply . send ( data ) ;
243+
244+ //const data = await helper.publicConsume(publicKey);
245+ //if (!data) throw 404;
246+ //reply.send(data);
247+
248+ //reply.redirect(`https://securelay.github.io/jsonbin/${endpointID}/${publicKey}.json`, 301);
249+ reply . redirect ( `${ cdnUrlBase } /${ publicKey } .json` , 301 ) ;
231250 } catch ( err ) {
232251 if ( err == 401 ) {
233252 callUnauthorized ( reply , 'Provided key is not Public' ) ;
0 commit comments