diff --git a/examples/guestbook-go/src/main.go b/examples/guestbook-go/src/main.go new file mode 100644 index 00000000000..098feaa176d --- /dev/null +++ b/examples/guestbook-go/src/main.go @@ -0,0 +1,104 @@ +package main + +import ( + "encoding/json" + "net/http" + "os" + "strings" + + "github.com/codegangsta/negroni" + "github.com/gorilla/mux" + "github.com/xyproto/simpleredis" +) + +var pool *simpleredis.ConnectionPool + +func ListRangeHandler(rw http.ResponseWriter, req *http.Request) { + members, err := simpleredis.NewList(pool, mux.Vars(req)["key"]).GetAll() + if err != nil { + panic(err) + } + + membersJSON, err := json.MarshalIndent(members, "", " ") + if err != nil { + panic(err) + } + + rw.WriteHeader(200) + rw.Header().Set("Content-Type", "application/json") + rw.Write([]byte(membersJSON)) +} + +func ListPushHandler(rw http.ResponseWriter, req *http.Request) { + set := simpleredis.NewList(pool, mux.Vars(req)["key"]) + err := set.Add(mux.Vars(req)["value"]) + if err != nil { + panic(err) + } + + members, err := set.GetAll() + if err != nil { + panic(err) + } + + membersJSON, err := json.MarshalIndent(members, "", " ") + if err != nil { + panic(err) + } + + rw.WriteHeader(200) + rw.Header().Set("Content-Type", "application/json") + rw.Write([]byte(membersJSON)) +} + +func InfoHandler(rw http.ResponseWriter, req *http.Request) { + info, err := pool.Get(0).Do("INFO") + if err != nil { + panic(err) + } + + infoString := string(info.([]uint8)) + + rw.WriteHeader(200) + rw.Write([]byte(infoString)) +} + +func EnvHandler(rw http.ResponseWriter, req *http.Request) { + getenvironment := func(data []string, getkeyval func(item string) (key, val string)) map[string]string { + items := make(map[string]string) + for _, item := range data { + key, val := getkeyval(item) + items[key] = val + } + return items + } + environment := getenvironment(os.Environ(), func(item string) (key, val string) { + splits := strings.Split(item, "=") + key = splits[0] + val = strings.Join(splits[1:], "=") + return + }) + + envJSON, err := json.MarshalIndent(environment, "", " ") + if err != nil { + panic(err) + } + + rw.WriteHeader(200) + rw.Write([]byte(envJSON)) +} + +func main() { + pool = simpleredis.NewConnectionPoolHost(os.Getenv("SERVICE_HOST") + ":" + os.Getenv("REDIS_MASTER_SERVICE_SERVICE_PORT")) + defer pool.Close() + + r := mux.NewRouter() + r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler) + r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler) + r.Path("/info").Methods("GET").HandlerFunc(InfoHandler) + r.Path("/env").Methods("GET").HandlerFunc(EnvHandler) + + n := negroni.Classic() + n.UseHandler(r) + n.Run(":3000") +} diff --git a/examples/guestbook-go/src/public/index.html b/examples/guestbook-go/src/public/index.html new file mode 100644 index 00000000000..e1301d79796 --- /dev/null +++ b/examples/guestbook-go/src/public/index.html @@ -0,0 +1,39 @@ + + + + + + + + + Guestbook + + + +
+
+
+

Waiting for database connection...

+
+
+
+
+
+
+
+ + Submit +
+
+
+
+ + + + diff --git a/examples/guestbook-go/src/public/script.js b/examples/guestbook-go/src/public/script.js new file mode 100644 index 00000000000..2383a791acc --- /dev/null +++ b/examples/guestbook-go/src/public/script.js @@ -0,0 +1,34 @@ +$( document ).ready(function() { + + appendGuestbookEntries = function( data ) { + $( "#guestbook-entries" ).empty(); + $.each( data, function( key, val ) { + $( "#guestbook-entries" ).append( "

" + val + "

" ); + }); + } + + handleSubmission = function() { + value = $( "#guestbook-entry-content" ).val() + if (value.length > 0) { + $( "#guestbook-entries" ).append( "

...

" ); + $.getJSON( "rpush/guestbook/" + value, appendGuestbookEntries); + } + return false; + } + + // Event handlers. + $( "#guestbook-submit" ).click(handleSubmission); + $( "#guestbook-entry-content" ).keypress(function (e) { + if (e.which == 13) { + return handleSubmission(); + } + }); + + // Poll every second. + (function fetchGuestbook(){ + + $.getJSON("lrange/guestbook").done(appendGuestbookEntries).always(function() { setTimeout(fetchGuestbook, 1000); }); + + })(); + +}); diff --git a/examples/guestbook-go/src/public/style.css b/examples/guestbook-go/src/public/style.css new file mode 100644 index 00000000000..20e25aac27d --- /dev/null +++ b/examples/guestbook-go/src/public/style.css @@ -0,0 +1,107 @@ +html, body { + background: #EEE; } + +html, button, input, select, textarea, +.pure-g [class*="pure-u"], +.pure-g-r [class*="pure-u"] { + font-family: "Helvetica", sans-serif; + color: #585A5C; + line-height: 1.4; + position: relative; } + +h1, h2, p, a, .pure-button { + margin: 0; + color: #585A5C; + font-weight: 400; } + h1 strong, h2 strong, h3 strong, p strong, li strong, a strong, .pure-button strong { + font-weight: 700; } + +h1 { + font-size: 5em; + font-weight: 300; + color: #585A5C; } + +h2 { + font-size: 2.25em; + font-weight: 300; + color: #585A5C; } + +p { + font-size: 1.2em; + line-height: 1.7; } + +input { + font-size: 1.125em; + outline: none; } + +.default-button { + position: relative; + font-weight: 400; + padding: 0.75em 2em; + white-space: normal; + background: #1699e3; + border-radius: 1000px; + font-size: 1.25em; + color: #FFF; + text-transform: uppercase; } + .default-button strong { + font-weight: 700; } + +.section { + position: relative; + zoom: 1; + width: 100%; + overflow: hidden; } + +.container { + position: relative; + overflow: hidden; + margin: 0 auto; + padding: 0 1em; + max-width: 64em; + text-align: center; } + +.section-text-container { + max-width: 48em; + margin: 0 auto; } + +.color-overlay { + position: absolute; + width: 100%; + height: 100%; + top: 0; + left: 0; + opacity: 0.9; + -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=" 90 ")"; + filter: alpha(opacity=90); + zoom: 1; } + +#header { + padding: 2em 0 0 0; } + #header h1, #header h2, #header a { + color: #585A5C; } + #header .color-overlay { + background: #eee; } + #header .container { + overflow: visible; } + +#content { + background: #eee; + padding: 1em 0; } + #content form { + text-align: center; + margin: 3em auto 0 auto; + max-width: 20em; } + #content input { + font-size: 1.25em; + display: block; + width: 80%; + padding: 0.75em 10%; + margin-bottom: 1em; + border-radius: 1000px; + text-align: center; + border: 0; + box-shadow: inset 0 0 0 2px #ccc; + -webkit-appearance: none; } + #content button { + width: 100%; }