Cleanup after initial PR and shading of src.

This commit is contained in:
jayunit100
2015-03-13 14:44:08 -04:00
parent c552e916da
commit abf3bb226e
6 changed files with 151 additions and 2415 deletions

View File

@@ -1,4 +1,5 @@
package main
/*
Copyright 2014 Google Inc. All rights reserved.
@@ -18,110 +19,107 @@ limitations under the License.
//package main
import (
"encoding/json"
"net/http"
"os"
"strings"
"fmt"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/xyproto/simpleredis"
"encoding/json"
"fmt"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/xyproto/simpleredis"
"net/http"
"os"
"strings"
)
//return the path to static assets (i.e. index.html)
func pathToStaticContents() (string) {
var static_content = os.Getenv("STATIC_FILES");
// Take a wild guess. This will work in dev environment.
if static_content == "" {
println("*********** WARNING: DIDNT FIND ENV VAR 'STATIC_FILES', guessing your running in dev.")
static_content = "../../static/"
} else {
println("=========== Read ENV 'STATIC_FILES', path to assets : " + static_content);
}
func pathToStaticContents() string {
var static_content = os.Getenv("STATIC_FILES")
// Take a wild guess. This will work in dev environment.
if static_content == "" {
println("*********** WARNING: DIDNT FIND ENV VAR 'STATIC_FILES', guessing your running in dev.")
static_content = "../../static/"
} else {
println("=========== Read ENV 'STATIC_FILES', path to assets : " + static_content)
}
//Die if no the static files are missing.
_, err := os.Stat(static_content)
if err != nil {
println("*********** os.Stat failed on " + static_content + " This means no static files are available. Dying...");
os.Exit(2);
}
return static_content;
//Die if no the static files are missing.
_, err := os.Stat(static_content)
if err != nil {
println("*********** os.Stat failed on " + static_content + " This means no static files are available. Dying...")
os.Exit(2)
}
return static_content
}
func main() {
var connection = os.Getenv("REDISMASTER_SERVICE_HOST")+":"+os.Getenv("REDISMASTER_SERVICE_PORT");
var connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT")
if connection == ":" {
print("WARNING ::: If in kube, this is a failure: Missing env variable REDISMASTER_SERVICE_HOST");
print("WARNING ::: Attempting to connect redis localhost.")
connection="127.0.0.1:6379";
} else {
print("Found redis master host "+ os.Getenv("REDISMASTER_SERVICE_PORT"));
connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT");
}
if connection == ":" {
print("WARNING ::: If in kube, this is a failure: Missing env variable REDISMASTER_SERVICE_HOST")
print("WARNING ::: Attempting to connect redis localhost.")
connection = "127.0.0.1:6379"
} else {
print("Found redis master host " + os.Getenv("REDISMASTER_SERVICE_PORT"))
connection = os.Getenv("REDISMASTER_SERVICE_HOST") + ":" + os.Getenv("REDISMASTER_SERVICE_PORT")
}
println("Now connecting to : " + connection)
/**
* Create a connection pool. ?The pool pointer will otherwise
* not be of any use.?https://gist.github.com/jayunit100/1d00e6d343056401ef00
*/
pool = simpleredis.NewConnectionPoolHost(connection)
println("Now connecting to : " + connection)
/**
* Create a connection pool. ?The pool pointer will otherwise
* not be of any use.?https://gist.github.com/jayunit100/1d00e6d343056401ef00
*/
pool = simpleredis.NewConnectionPoolHost(connection)
println("Connection pool established : " + connection)
println("Connection pool established : " + connection)
defer pool.Close()
defer pool.Close()
r := mux.NewRouter()
r := mux.NewRouter()
println("Router created ")
println("Router created ")
/**
* Define a REST path.
* - The parameters (key) can be accessed via mux.Vars.
* - The Methods (GET) will be bound to a handler function.
*/
r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
r.Path("/llen").Methods("GET").HandlerFunc(LLENHandler)
/**
* Define a REST path.
* - The parameters (key) can be accessed via mux.Vars.
* - The Methods (GET) will be bound to a handler function.
*/
r.Path("/info").Methods("GET").HandlerFunc(InfoHandler)
r.Path("/lrange/{key}").Methods("GET").HandlerFunc(ListRangeHandler)
r.Path("/rpush/{key}/{value}").Methods("GET").HandlerFunc(ListPushHandler)
r.Path("/llen").Methods("GET").HandlerFunc(LLENHandler)
//for dev environment, the site is one level up...
//for dev environment, the site is one level up...
r.PathPrefix("/").Handler(http.FileServer(http.Dir( pathToStaticContents() )))
r.PathPrefix("/").Handler(http.FileServer(http.Dir(pathToStaticContents())))
r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)
r.Path("/env").Methods("GET").HandlerFunc(EnvHandler)
list := simpleredis.NewList(pool, "k8petstore")
HandleError(nil, list.Add("jayunit100"))
HandleError(nil, list.Add("tstclaire"))
HandleError(nil, list.Add("rsquared"))
list := simpleredis.NewList(pool, "k8petstore")
HandleError(nil, list.Add("jayunit100"))
HandleError(nil, list.Add("tstclaire"))
HandleError(nil, list.Add("rsquared"))
// Verify that this is 3 on startup.
infoL := HandleError(pool.Get(0).Do("LLEN","k8petstore")).(int64)
fmt.Printf("\n=========== Starting DB has %d elements \n", infoL)
if infoL < 3 {
print("Not enough entries in DB. something is wrong w/ redis querying")
print(infoL)
panic("Failed ... ")
}
// Verify that this is 3 on startup.
infoL := HandleError(pool.Get(0).Do("LLEN", "k8petstore")).(int64)
fmt.Printf("\n=========== Starting DB has %d elements \n", infoL)
if infoL < 3 {
print("Not enough entries in DB. something is wrong w/ redis querying")
print(infoL)
panic("Failed ... ")
}
println("=========== Now launching negroni...this might take a second...")
n := negroni.Classic()
n.UseHandler(r)
n.Run(":3000")
println("Done ! Web app is now running.")
println("=========== Now launching negroni...this might take a second...")
n := negroni.Classic()
n.UseHandler(r)
n.Run(":3000")
println("Done ! Web app is now running.")
}
/**
* the Pool will be populated on startup,
* it will be an instance of a connection pool.
* Hence, we reference its address rather than copying.
*/
*/
var pool *simpleredis.ConnectionPool
/**
@@ -129,79 +127,79 @@ var pool *simpleredis.ConnectionPool
* input: key
*
* Writes all members to JSON.
*/
*/
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
println("ListRangeHandler")
println("ListRangeHandler")
key := mux.Vars(req)["key"]
key := mux.Vars(req)["key"]
list := simpleredis.NewList(pool, key)
list := simpleredis.NewList(pool, key)
//members := HandleError(list.GetAll()).([]string)
members := HandleError(list.GetLastN(4)).([]string)
//members := HandleError(list.GetAll()).([]string)
members := HandleError(list.GetLastN(4)).([]string)
print(members)
membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte)
print(members)
membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte)
print("RETURN MEMBERS = "+string(membersJSON))
rw.Write(membersJSON)
print("RETURN MEMBERS = " + string(membersJSON))
rw.Write(membersJSON)
}
func LLENHandler(rw http.ResponseWriter, req *http.Request) {
println("=========== LLEN HANDLER")
println("=========== LLEN HANDLER")
infoL := HandleError(pool.Get(0).Do("LLEN","k8petstore")).(int64)
fmt.Printf("=========== LLEN is %d ", infoL)
lengthJSON := HandleError(json.MarshalIndent(infoL, "", " ")).([]byte)
fmt.Printf("================ LLEN json is %s", infoL)
print("RETURN LEN = "+string(lengthJSON))
rw.Write(lengthJSON)
infoL := HandleError(pool.Get(0).Do("LLEN", "k8petstore")).(int64)
fmt.Printf("=========== LLEN is %d ", infoL)
lengthJSON := HandleError(json.MarshalIndent(infoL, "", " ")).([]byte)
fmt.Printf("================ LLEN json is %s", infoL)
print("RETURN LEN = " + string(lengthJSON))
rw.Write(lengthJSON)
}
func ListPushHandler(rw http.ResponseWriter, req *http.Request) {
println("ListPushHandler")
println("ListPushHandler")
/**
* Expect a key and value as input.
*
*/
key := mux.Vars(req)["key"]
value := mux.Vars(req)["value"]
/**
* Expect a key and value as input.
*
*/
key := mux.Vars(req)["key"]
value := mux.Vars(req)["value"]
println("New list " + key + " " + value)
list := simpleredis.NewList(pool, key)
HandleError(nil, list.Add(value))
ListRangeHandler(rw, req)
println("New list " + key + " " + value)
list := simpleredis.NewList(pool, key)
HandleError(nil, list.Add(value))
ListRangeHandler(rw, req)
}
func InfoHandler(rw http.ResponseWriter, req *http.Request) {
println("InfoHandler")
println("InfoHandler")
info := HandleError(pool.Get(0).Do("INFO")).([]byte)
rw.Write(info)
info := HandleError(pool.Get(0).Do("INFO")).([]byte)
rw.Write(info)
}
func EnvHandler(rw http.ResponseWriter, req *http.Request) {
println("EnvHandler")
println("EnvHandler")
environment := make(map[string]string)
for _, item := range os.Environ() {
splits := strings.Split(item, "=")
key := splits[0]
val := strings.Join(splits[1:], "=")
environment[key] = val
}
environment := make(map[string]string)
for _, item := range os.Environ() {
splits := strings.Split(item, "=")
key := splits[0]
val := strings.Join(splits[1:], "=")
environment[key] = val
}
envJSON := HandleError(json.MarshalIndent(environment, "", " ")).([]byte)
rw.Write(envJSON)
envJSON := HandleError(json.MarshalIndent(environment, "", " ")).([]byte)
rw.Write(envJSON)
}
func HandleError(result interface{}, err error) (r interface{}) {
if err != nil {
print("ERROR : " + err.Error())
//panic(err)
}
return result
if err != nil {
print("ERROR : " + err.Error())
//panic(err)
}
return result
}