Last active
August 29, 2015 14:08
-
-
Save butaji/302ba2519bb2aa08cd7d to your computer and use it in GitHub Desktop.
Simple REST based (node.js express backed) event store and Go events generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"os" | |
"math/rand" | |
) | |
func main() { | |
for i:= 0; true; i++ { | |
m := map[string]interface{} { | |
"Type": "OrderCompleted", | |
"TotalPrice": rand.Intn(1000000), | |
} | |
r, _ := json.Marshal(m) | |
os.Stdout.Write(r); | |
os.Stdout.Write([]byte("\n")); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var data = {}; | |
var express = require('express') | |
var app = express() | |
var logger = require('morgan'); | |
var bodyParser = require('body-parser'); | |
app.use(logger('dev')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: false })); | |
app.get('/events/:id/:ticks', function(req, res) { | |
var items = data[req.params['id']].filter(function (i) { | |
return i.occured > req.params['ticks'] | |
}); | |
res.send(paginator(items, req.params['id'])); | |
}); | |
var page_size = 500; | |
function paginator(items, id) { | |
if (!id) | |
return ["Bad id is come " + id]; | |
var page = items.slice(0,page_size); | |
if (page.length == page_size) | |
page.push({ | |
"type" : "next_page", | |
"url" : "http://" + server.address().address + ":" + server.address().port + "/events/" + id + "/" + page[page_size-1].occured | |
}); | |
return page; | |
} | |
app.get('/events/:id', function(req, res) { | |
res.send(paginator(data[req.params['id']] || [], req.params['id'])); | |
}); | |
app.post('/events/:id', function (req, res) { | |
var event = req.body; | |
var id = req.params['id']; | |
if (!event) { | |
res.send({ | |
msg : 'Youre missing body' | |
}); | |
return; | |
} | |
if (!data[id]) { | |
data[id] = []; | |
} | |
var envelope = { | |
id : id, | |
body : event, | |
occured : new Date().getTime() | |
}; | |
data[id].push(envelope); | |
res.send(envelope); | |
}) | |
var server = app.listen(3000, function () { | |
var host = server.address().address | |
var port = server.address().port | |
console.log('Example app listening at http://%s:%s', host, port) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"net/http" | |
"io/ioutil" | |
"os" | |
"encoding/json" | |
"time" | |
) | |
func main() { | |
explore("http://0.0.0.0:3000/events/100") | |
} | |
func explore(url string) { | |
response, err := http.Get(url) | |
fmt.Printf("GET %s", url) | |
if err != nil { | |
fmt.Printf("%s", err) | |
os.Exit(1) | |
} else { | |
defer response.Body.Close() | |
contents, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
fmt.Printf("%s", err) | |
os.Exit(1) | |
} | |
d := []map[string] interface {} {} | |
r := json.Unmarshal(contents, &d) | |
if r != nil { | |
fmt.Printf("%s", r) | |
os.Exit(1) | |
} | |
fmt.Printf(" - %d\n", len(d)) | |
n := d[len(d)-1] | |
if (n["type"] == "next_page") { | |
apply(d[0:len(d)-1]) | |
explore(n["url"].(string)) | |
} else { | |
fmt.Printf("========= break ==========\n") | |
time.Sleep(5000 * time.Millisecond) | |
explore(url) | |
} | |
} | |
} | |
func apply(items []map[string]interface{}) { | |
fmt.Printf("========= applied ==========\n") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment