@@ -9,7 +9,6 @@ const { random } = require('lodash')
99const BURN_ADDRESS = 'tz1burnburnburnburnburnburnburjAYjjX'
1010
1111require ( 'dotenv' ) . config ( )
12- const { Semaphore } = require ( 'prex' )
1312
1413const reducer = ( accumulator , currentValue ) => parseInt ( accumulator ) + parseInt ( currentValue )
1514
@@ -51,43 +50,38 @@ const randomFeed = async (counter, res) => {
5150 } )
5251}
5352
54- const getFeed = async ( counter , featured ) => {
55- /* const now_time = Date.now()
56- const immutable = (typeof max_time !== 'undefined') && (max_time < now_time)
57- max_time = (typeof max_time !== 'undefined') ? max_time : customFloor(now_time, ONE_MINUTE_MILLIS)
58- */
53+ const getFeed = async ( res , counter , featured , max_time ) => {
54+ const now_time = Date . now ( )
55+ const immutable = ( typeof max_time !== 'undefined' ) && ( max_time < now_time )
56+ max_time = ( typeof max_time !== 'undefined' ) ? max_time : customFloor ( now_time , ONE_MINUTE_MILLIS )
57+
5958 console . log ( `feed, featured: ${ featured } ` )
6059 var arr
6160 if ( featured ) {
62- arr = await conseilUtil . getFeaturedArtisticUniverse ( 0 )
61+ arr = await conseilUtil . getFeaturedArtisticUniverse ( 0 , max_time )
6362 } else {
64- arr = await conseilUtil . getArtisticUniverse ( 0 )
63+ arr = await conseilUtil . getArtisticUniverse ( 0 , max_time )
6564 }
6665
6766 var feed = offset ( desc ( arr ) , counter )
68- // console.log(feed)
6967 feed = await feed . map ( async e => {
7068 e . token_info = await getIpfsHash ( e . ipfsHash )
7169 e . token_id = parseInt ( e . objectId )
7270 console . log ( e )
7371 return e
7472 } )
75- //console.log(feed)
76- /* var cache_time
77- if (immutable) {
78- cache_time = 60 * 10
79- }
80- else {
81- cache_time = (int)(((max_time + ONE_MINUTE_MILLIS) - now_time) / 1000)
82- } */
73+ var cache_time
74+ if ( immutable ) {
75+ cache_time = 60 * 10
76+ }
77+ else {
78+ cache_time = Math . floor ( ( ( max_time + ONE_MINUTE_MILLIS ) - now_time ) / 1000 )
79+ }
8380 var promise = Promise . all ( feed . map ( e => e ) )
8481 return promise . then ( async ( results ) => {
8582 var aux_arr = results . map ( e => e )
86-
87- //res.set('Cache-Control', `public, max-age=${cache_time}`)
88-
89- // console.log(aux_arr)
90- return aux_arr
83+ res . set ( 'Cache-Control' , `public, max-age=${ cache_time } ` )
84+ res . json ( { result : aux_arr } )
9185 } )
9286}
9387
@@ -144,6 +138,7 @@ const getTzLedger = async (tz, res) => {
144138 promise . then ( async results => {
145139 var result = results . map ( e => e )
146140 console . log ( result )
141+
147142 res . json ( {
148143 result : _ . uniqBy ( result , ( e ) => {
149144 return e . token_id
@@ -175,6 +170,7 @@ const hDAOFeed = async (counter, res) => {
175170 var set = _ . orderBy ( hDAO , [ 'hDAO_balance' ] , [ 'desc' ] )
176171 var objkts = await ( offset ( set , counter ) ) . map ( async e => await mergehDAO ( e ) )
177172
173+ res . set ( 'Cache-Control' , `public, max-age=300` )
178174 var promise = Promise . all ( objkts . map ( e => e ) )
179175 promise . then ( results => {
180176 var result = results . map ( e => e )
@@ -186,45 +182,14 @@ const hDAOFeed = async (counter, res) => {
186182}
187183
188184// list of restricted addresses
189- const restrictedAdddressesCacheTimeLimit = ONE_MINUTE_MILLIS // the blockchain updates about once a minute
190- let restrictedAddressesCache = null
191- const restrictedAddressesLock = new Semaphore ( 1 )
192185const getRestrictedAddresses = async ( ) => {
193- await restrictedAddressesLock . wait ( )
194- if ( restrictedAddressesCache && Date . now ( ) - restrictedAddressesCache . expires < restrictedAdddressesCacheTimeLimit ) {
195- restrictedAddressesLock . release ( )
196- // console.log('ADDRESS restrictions from CACHE')
197- return restrictedAddressesCache . data
198- }
199-
200186 const list = await axios . get ( 'https://raw.githubusercontent.com/hicetnunc2000/hicetnunc/main/filters/w.json' ) . then ( res => res . data )
201- restrictedAddressesCache = {
202- expires : Date . now ( ) ,
203- data : list
204- }
205- restrictedAddressesLock . release ( )
206- // console.log('ADDRESS restrictions from NEW')
207187 return list
208188}
209189
210190// list of restricted objkts
211- const restrictedObjectsCacheTimeLimit = ONE_MINUTE_MILLIS // the blockchain updates about once a minute
212- let restrictedObjectsCache = null
213- const restrictedObjectsLock = new Semaphore ( 1 )
214191const getRestrictedObjkts = async ( ) => {
215- await restrictedObjectsLock . wait ( )
216- if ( restrictedObjectsCache && Date . now ( ) - restrictedObjectsCache . expires < restrictedObjectsCacheTimeLimit ) {
217- restrictedObjectsLock . release ( )
218- // console.log('OBJKT restrictions from CACHE')
219- return restrictedObjectsCache . data
220- }
221-
222192 const list = await axios . get ( 'https://raw.githubusercontent.com/hicetnunc2000/hicetnunc/main/filters/o.json' ) . then ( res => res . data )
223- restrictedObjectsCache = {
224- expires : Date . now ( ) ,
225- data : list
226- }
227- restrictedObjectsLock . release ( )
228193 return list
229194}
230195
@@ -233,81 +198,100 @@ const app = express()
233198app . use ( express . json ( ) )
234199app . use ( cors ( { origin : '*' } ) )
235200
236- // used for very simple caching of the feed
237- const feedCacheTimeLimit = ONE_MINUTE_MILLIS // the blockchain updates about once a minute
238- const feedCache = { }
239- const feedLocks = { }
201+ const feedfeatured = async ( req , res ) => {
202+ const feedOffset = req . body . counter || 0
203+ const isFeatured = req . path === '/featured'
240204
241- const getFeedLock = ( key ) => {
242- if ( ! feedLocks [ key ] ) {
243- feedLocks [ key ] = new Semaphore ( 1 )
244- }
245- return feedLocks [ key ]
205+ await getFeed ( res , feedOffset , isFeatured , req . body . max_time )
246206}
247207
248208app . post ( '/feed|/featured' , async ( req , res ) => {
249- const feedOffset = req . body . counter || 0
250- const isFeatured = req . path === '/featured'
251- const lockKey = `${ feedOffset } -${ isFeatured ? 'featured' : '' } `
252-
253- await getFeedLock ( lockKey ) . wait ( )
254- if ( feedCache [ lockKey ] && Date . now ( ) - feedCache [ lockKey ] . expires < feedCacheTimeLimit ) {
255- getFeedLock ( lockKey ) . release ( )
256- // console.log('Feed from CACHE')
257- return res . json ( { result : feedCache [ lockKey ] . data } )
258- }
209+ await feedfeatured ( req , res )
210+ } )
259211
260- const aux_arr = await getFeed ( feedOffset , isFeatured )
261- feedCache [ lockKey ] = {
262- expires : Date . now ( ) ,
263- data : aux_arr
264- }
265- getFeedLock ( lockKey ) . release ( )
266- // console.log('Feed from NEW')
267- return res . json ( { result : aux_arr } )
212+ app . get ( '/feed|/featured' , async ( req , res ) => {
213+ await feedfeatured ( req , res )
268214} )
269215
270- app . post ( '/random' , async ( req , res ) => {
216+
217+ // Random
218+
219+ const rand = async ( req , res ) => {
220+ res . set ( 'Cache-Control' , `public, max-age=300` )
271221 await randomFeed ( parseInt ( req . body . counter ) , res )
222+ }
223+
224+ app . post ( '/random' , async ( req , res ) => {
225+ await rand ( req , res )
272226} )
273227
274- app . post ( '/tz' , async ( req , res ) => {
228+ app . get ( '/random' , async ( req , res ) => {
229+ await rand ( req , res )
230+ } )
275231
232+
233+ // TZ
234+
235+ const tz = async ( req , res ) => {
276236 // list of restricted addresses
277237 var list = await getRestrictedAddresses ( )
278238
239+ res . set ( 'Cache-Control' , `public, max-age=120` )
279240 list . includes ( req . body . tz )
280241 ?
281242 res . json ( { result : [ ] } )
282243 :
283244 await getTzLedger ( req . body . tz , res )
245+ }
284246
247+ app . post ( '/tz' , async ( req , res ) => {
248+ await tz ( req , res )
249+ } )
250+ app . get ( '/tz' , async ( req , res ) => {
251+ await tz ( req , res )
285252} )
286253
287- app . post ( '/objkt' , async ( req , res ) => {
254+ // OBJEKT
255+
256+ const objkt = async ( req , res ) => {
288257
289258 // list of restricted objkts
290259 var list = await getRestrictedObjkts ( )
291260
261+ res . set ( 'Cache-Control' , `public, max-age=120` )
292262 list . includes ( parseInt ( req . body . objkt_id ) )
293263 ?
294264 res . json ( { result : [ ] } )
295265 :
296266 res . json ( { result : await getObjktById ( req . body . objkt_id ) } )
267+ }
268+
269+ app . post ( '/objkt' , async ( req , res ) => {
270+ await objkt ( req , res )
271+
272+ } )
273+ app . get ( '/objkt' , async ( req , res ) => {
274+ await objkt ( req , res )
297275} )
298276
277+
278+
279+
299280app . get ( '/recommend_curate' , async ( req , res ) => {
300281 const amt = await conseilUtil . getRecommendedCurateDefault ( )
301282 res . set ( 'Cache-Control' , `public, max-age=300` )
302283 res . json ( { amount : amt } )
303284} )
304285
286+
287+ // HDAO
305288app . post ( '/hdao' , async ( req , res ) => {
306289 await hDAOFeed ( parseInt ( req . body . counter ) , res )
307290} )
308291
309- // const testhdao = async () => await hDAOFeed(parseInt(0))
310- //testhdao()
292+ app . get ( '/hdao' , async ( req , res ) => {
293+ await hDAOFeed ( parseInt ( req . body . counter ) , res )
294+ } )
311295
312296//app.listen(3001)
313297console . log ( 'SERVER RUNNING ON localhost:3001' )
0 commit comments