Add the source for Go based Guestbook

This commit is contained in:
Gurpartap Singh 2014-09-12 22:54:54 +05:30
parent 9b6042ea2c
commit 4123b64840
4 changed files with 284 additions and 0 deletions

View File

@ -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")
}

View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.5.0/pure-min.css">
<link rel="stylesheet" href="/style.css">
<title>Guestbook</title>
</head>
<body>
<div class="section" id="header">
<div class="color-overlay"></div>
<div class="container">
<h1>Guestbook</h1>
<h2><script type="text/javascript">document.write(document.URL);</script></h2>
</div>
</div>
<div class="section" id="content">
<div class="container">
<div class="section-text-container" id="guestbook-entries">
<p>Waiting for database connection...</p>
</div>
</div>
</div>
<div class="section" id="content">
<div class="container">
<div class="section-text-container">
<form>
<input id="guestbook-entry-content" required="true" autocomplete="off" type="text" placeholder="" value="">
<a class="pure-button default-button" id="guestbook-submit" href="#">Submit</a>
</form>
</div>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="/script.js"></script>
</body>
</html>

View File

@ -0,0 +1,34 @@
$( document ).ready(function() {
appendGuestbookEntries = function( data ) {
$( "#guestbook-entries" ).empty();
$.each( data, function( key, val ) {
$( "#guestbook-entries" ).append( "<p>" + val + "</p>" );
});
}
handleSubmission = function() {
value = $( "#guestbook-entry-content" ).val()
if (value.length > 0) {
$( "#guestbook-entries" ).append( "<p>...</p>" );
$.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); });
})();
});

View File

@ -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%; }