Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"flag"
"fmt"
"log"
"net/http"
"sync"
"time"
"github.com/apokalyptik/quicktrie"
"github.com/gorilla/mux"
_ "net/http/pprof"
)
var listenOn = "127.0.0.1:8888"
var t = trie.NewKVTrie()
var m sync.RWMutex
func init() {
flag.StringVar(&listenOn, "listen", listenOn, "The address to listen on for web requests")
}
func main() {
flag.Parse()
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
r := mux.NewRouter()
r.HandleFunc("/get/{key}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
m.RLock()
defer m.RUnlock()
if ok, v := t.Get(vars["key"]); ok {
fmt.Fprint(w, v.(string))
} else {
http.NotFound(w, r)
}
})
r.HandleFunc("/set/{key}/{value}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
m.Lock()
defer m.Unlock()
t.Set(vars["key"], vars["value"])
})
r.HandleFunc("/del/{key}", func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
m.Lock()
defer m.Unlock()
t.Del(vars["key"])
})
go func() {
ticker := time.Tick(time.Millisecond)
for {
when := <-ticker
t.Set("now", when.String())
}
}()
r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "hello")
})
log.Fatal(http.ListenAndServe(listenOn, r))
}
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.