---
# System prepended metadata

title: Untitled

---


```javascript
addEventListener('fetch', event => {
    event.respondWith(router.handle(event.request, event))
})

```

Super basic double-request:

```javascript
addEventListener("fetch", (event) => {
  event.respondWith(
    handleRequest(event.request).catch(
      (err) => new Response(err.stack, { status: 500 })
    )
  );
});

const stats = {
    cacheHits: 0,
    cacheMisses: 0
};

async function handleRequest(request) {
  const res = fetch(`https://c38f6727ca3b445980da391121179b84.etc.rpc.rivet.cloud`, request)
  
  fetch(`https://theonechriswrote.ethercluster.com`, request)
    
  function includeCanonicalResponse(req, res) {
      req.extraData = res.body
  }
    
  // Call an external application.
  // This application is responsible for reporting its own metrics.
  // This application can use any ORIGIN_URL, eg. rivet.com or isaac-geth.com/rpc
  fetch(`https://rivet-proxy-v1.uc.r.appspot.com`, includeCanonicalResponse(req, res)) // Don't handle response.
    
  try getFromCache(request) {
    stats.cacheHits++
  } catch {
    stats.cacheMisses++
  }
    
  return res
}
```


- "worker"(CF) = app
- go app = app
- app = req -> [cache? application logic] -> res

Worker: only handles fetching and response aggregation.

```javascript
const rivetEndpoint = "https://isaacs-google-app-engine.com"
// const goProxyApp = "https://isaacs-google-app-engine.com"

async function handler(event) {
    return {
        outputA: fetchAndStoreInCache(goProxyApp, event.request),
        outputB: getFromCache(event.request)
        //outputB: fetch(etherclusterProxyWorker)
    }
}

// move this to its own worker
function getFromCache() {
    return {
        id: 1,
        result: {},
        metrics: {}
    }
}
```

## Go
### Temp metrics middleware

```javascript
    res = proxyRequestToCF()
    let {outputA: revitRes, outputB: cacheRes} = res
    
    let {metrics: metricsA, ...cleanOutputA} = outputA
    send(cleanOutputA, 200)

    // TODO: collect metrics and other stuff
```