Skip to content

Instantly share code, notes, and snippets.

@johnaxel johnaxel/main.go forked from kmshultz/main.go
Created Sep 10, 2017

Embed
What would you like to do?
Adds expvar and some dogstatsd to kubernete's examples/guestbook-go/main.go
/*
Copyright 2014 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/json"
_ "expvar"
"log"
"net/http"
"os"
"strings"
"github.com/DataDog/datadog-go/statsd"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
"github.com/xyproto/simpleredis"
)
var (
masterPool *simpleredis.ConnectionPool
slavePool *simpleredis.ConnectionPool
dogstatsd *statsd.Client
)
func ListRangeHandler(rw http.ResponseWriter, req *http.Request) {
key := mux.Vars(req)["key"]
list := simpleredis.NewList(slavePool, key)
members := HandleError(list.GetAll()).([]string)
membersJSON := HandleError(json.MarshalIndent(members, "", " ")).([]byte)
rw.Write(membersJSON)
}
func ListPushHandler(rw http.ResponseWriter, req *http.Request) {
dogstatsd.Incr("request_count", []string{"endpoint:submit"}, 1)
key := mux.Vars(req)["key"]
value := mux.Vars(req)["value"]
list := simpleredis.NewList(masterPool, key)
HandleError(nil, list.Add(value))
ListRangeHandler(rw, req)
}
func InfoHandler(rw http.ResponseWriter, req *http.Request) {
dogstatsd.Incr("request_count", []string{"endpoint:info"}, 1)
info := HandleError(masterPool.Get(0).Do("INFO")).([]byte)
rw.Write(info)
}
func EnvHandler(rw http.ResponseWriter, req *http.Request) {
dogstatsd.Incr("request_count", []string{"endpoint:env"}, 1)
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)
}
func HandleError(result interface{}, err error) (r interface{}) {
if err != nil {
panic(err)
}
return result
}
func main() {
masterPool = simpleredis.NewConnectionPoolHost("redis-master:6379")
defer masterPool.Close()
slavePool = simpleredis.NewConnectionPoolHost("redis-slave:6379")
defer slavePool.Close()
var err error
dogstatsd, err = statsd.New(os.Getenv("DOGSTATSD_HOST_IP") + ":8125")
if err != nil {
log.Printf("Cannot get a DogStatsD client.")
} else {
dogstatsd.Namespace = "guestbook."
dogstatsd.Event(&statsd.Event{
Title: "Guestbook application started.",
Text: "Guestbook application started.",
})
}
go func() {
http.ListenAndServe(":2999", http.DefaultServeMux)
}()
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")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.