Skip to content
This repository has been archived by the owner on Apr 3, 2022. It is now read-only.

Custom binIDs [WIP: Needs some frontend work still] #115

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions geobinserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,15 @@ func TestCreateHandler(t *testing.T) {
assertBodyContainsKey(w.Body, "expires", t)
}

func TestBinHandler404(t *testing.T) {
// Test 404 for nonexistant bin
req, err := http.NewRequest("POST", "http://testing.geobin.io/nonexistant_bin", nil)
func TestCustomBinHandler(t *testing.T) {
req, err := http.NewRequest("POST", "http://testing.geobin.io/my_awesome_bin_id", nil)
if err != nil {
t.Error(err)
}
w := httptest.NewRecorder()
createGeobinServer().ServeHTTP(w, req)

assertResponseNotFound(w, t)
assertResponseOK(w, t)
}

func TestBinHandlerEmptyBody(t *testing.T) {
Expand Down Expand Up @@ -218,7 +217,7 @@ func TestBinHandler200(t *testing.T) {
assertResponseOK(w, t)
}

func TestBinHistoryReturnsErrorForInvalidBin(t *testing.T) {
func TestBinHistoryCreatesNonExistantBin(t *testing.T) {
binId := "neverland"

// Check history for our bin
Expand All @@ -230,7 +229,7 @@ func TestBinHistoryReturnsErrorForInvalidBin(t *testing.T) {
w := httptest.NewRecorder()
createGeobinServer().ServeHTTP(w, req)

assertResponseCode(w, http.StatusNotFound, t)
assertResponseOK(w, t)
}

func TestBinHistoryWorksAsIntended(t *testing.T) {
Expand Down
65 changes: 47 additions & 18 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ import (
"github.com/nu7hatch/gouuid"
)

func (gb *geobinServer) createBin(n string, w http.ResponseWriter) (time.Time, error) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function needs a doc comment!

var err error
t := time.Now()

// Save to redis
if _, err = gb.ZAdd(n, redis.Z{Score: 0, Member: ""}); err != nil {
log.Println("Failure to ZADD to", n, err)
http.Error(w, "Could not generate new Geobin!", http.StatusInternalServerError)
return t, err
}

// Set expiration
d := 48 * time.Hour
if _, err = gb.Expire(n, d); err != nil {
log.Println("Failure to set EXPIRE for", n, err)
http.Error(w, "Could not generate new Geobin!", http.StatusInternalServerError)
return t, err
}

return t.Add(d), nil
}

// createHandler handles requests to /api/1/create. It creates a randomly generated bin_id,
// creates an entry in redis for it, with a 48 hour expiration time and writes a json object
// to the response with the following structure:
Expand All @@ -34,27 +56,16 @@ func (gb *geobinServer) createHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Save to redis
if _, err = gb.ZAdd(n, redis.Z{Score: 0, Member: ""}); err != nil {
log.Println("Failure to ZADD to", n, err)
http.Error(w, "Could not generate new Geobin!", http.StatusInternalServerError)
return
}

// Set expiration
d := 48 * time.Hour
if _, err = gb.Expire(n, d); err != nil {
log.Println("Failure to set EXPIRE for", n, err)
http.Error(w, "Could not generate new Geobin!", http.StatusInternalServerError)
var t time.Time
if t, err = gb.createBin(n, w); err != nil {
return
}
exp := time.Now().Add(d).Unix()

// Create the json response and encoder
encoder := json.NewEncoder(w)
bin := map[string]interface{}{
"id": n,
"expires": exp,
"expires": t.Unix(),
}

// encode the json directly to the response writer
Expand Down Expand Up @@ -112,8 +123,10 @@ func (gb *geobinServer) binHandler(w http.ResponseWriter, r *http.Request) {
}

if !exists {
http.NotFound(w, r)
return
// create it
if _, err = gb.createBin(name, w); err != nil {
return
}
}

var body []byte
Expand Down Expand Up @@ -164,8 +177,10 @@ func (gb *geobinServer) historyHandler(w http.ResponseWriter, r *http.Request) {
}

if !exists {
http.NotFound(w, r)
return
// create it
if _, err = gb.createBin(name, w); err != nil {
return
}
}

set, err := gb.ZRevRange(name, "0", "-1")
Expand Down Expand Up @@ -203,6 +218,20 @@ func (gb *geobinServer) wsHandler(w http.ResponseWriter, r *http.Request) {
path := strings.Split(r.URL.Path, "/")
binName := path[len(path)-1]

exists, err := gb.Exists(binName)
if err != nil {
http.Error(w, "Internal error", http.StatusInternalServerError)
return
}

if !exists {
if _, err := gb.createBin(binName, w); err != nil {
log.Println("Failure to create bin while trying to SUBSCRIBE to", binName, err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
}

// start pub subbing
if err := gb.Subscribe(binName); err != nil {
log.Println("Failure to SUBSCRIBE to", binName, err)
Expand Down
8 changes: 4 additions & 4 deletions static/app/bin/binList.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@

<div class="panel" ng-if="validBin && isEmpty(history)">
<div class="panel-heading">
<h4>No History Yet</h4>
<p>Try sending a request!</p>
<h4>No History</h4>
<p>This bin is empty. Try sending a request!</p>
</div>

<ul class="list-group">
Expand All @@ -63,12 +63,12 @@ <h4>No History Yet</h4>

<div class="alert alert-info" ng-if="!validBin">
<h4><i class="fa fa-warning"></i> Geobin "{{binId}}" Not Found!</h4>
<p>Either this bin has expired, or it never existed! Either way there's nothing to see here. You can either go back home or create a new geobin.</p>
<p>This is an invalid bin name! You can either go back home or create a new geobin.</p>
<hr>
<p>
<button type="button" class="btn btn-primary"
analytics-on="click"
analytics-event="Create Geobin (expired)"
analytics-event="Create Geobin (invalid)"
ng-click="create()"><i class="fa fa-magic"></i> Create a New Geobin</button>
<a href="/" class="btn btn-link"><i class="fa fa-home"></i> Go Back Home</a>
</p>
Expand Down
26 changes: 25 additions & 1 deletion static/app/home/home.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="topic">
<div class="container">
<h1>Geobin</h1>
<p>Inspect HTTP Requests with Geographic Data</p>
<p>Visualize HTTP Requests with Geographic Data</p>
</div>
</div>

Expand All @@ -20,6 +20,30 @@ <h2 class="panel-title">What is Geobin?</h2>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">How do I use it?</h2>
</div>
<div class="panel-body">

<p>You can create a new temporary bin by clicking the "<a href="" analytics-on="click" analytics-event="Create Geobin" ng-click="create()">Create a New Geobin</a>" button above to get
a new randomly generated Bin ID, or you can use an arbitrary custom Bin ID of your choosing.</p>

<p>To verify that your custom ID valid and is not currently being used by someone else go put the ID in after the slash:
<a href="http://{{host}}/my_custom_bin_id">http://{{host}}/my_custom_bin_id</a>.</p>

<p>After you have a URL, post some data to it! We currently only accept JSON formatted data in the body of HTTP POST requests.</p>

<p>Anything you post to geobin will expire after 48 hours. The history list to the right shows the Geobins you've viewed recently and the amount
of time until their last piece of data will expire.</p>

<p>It is also worth noting that Geobin is built to be a tool for quickly visualizing data in a temporary fashion. Its primary
purpose is to be a debugging tool. As such, there is no access control for the bins. Anyone with the URL can POST to or view
the data in the bin. Given that, it is a good idea to avoid using a custom bin ID that has a high probability of a clash with
others. Please keep this in mind when posting data to a bin, <strong>it is not secured or protected in any fashion</strong>.</p>
</div>
</div>

<div class="panel panel-default">
<div class="panel-heading">
<h2 class="panel-title">How do we find geographic data?</h2>
Expand Down
1 change: 1 addition & 0 deletions static/app/home/homeCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ angular.module('Geobin.controllers')
.controller('HomeCtrl', ['$scope', 'api', 'store',
function ($scope, api, store) {
document.title = 'Geobin';
$scope.host = window.location.host;
$scope.create = api.create;
$scope.bins = store.local.session.history;
$scope.enabled = store.local.enabled;
Expand Down