Download source code:
https://github.com/challenai/wtfcache
Expose popular HTTP API for users #
There are different ways to expose HTTP API in golang,
for example, the net/http
, fasthttp
,
we can use some high level web framework to expose too: gin
, martini
, echo
.
The fasthttp
provides competitive performance when the throughput is higher,
The web framework is much easier to use and maintain.
In this project, we employ the basic net/http
from golang standard library.
The only thing we need to to do is implement func Handle(w http.ResponseWriter, req *http.Request)
in the case of standard library.
Therefore, we can route our request as follows:
func CacheRoute(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case http.MethodGet:
HandleGetKey(w, req)
case http.MethodPost:
HandleSetKey(w, req)
case http.MethodDelete:
HandleDelKey(w, req)
default:
HandleNotFound(w, req)
}
}
func HandleGetKey(w http.ResponseWriter, req *http.Request) {
// cache.Get(req.Path)
}
func HandleSetKey(w http.ResponseWriter, req *http.Request) {
// cache.Set(req.Body.Key, req.Body.Value)
}
func HandleDelKey(w http.ResponseWriter, req *http.Request) {
// cache.Del(req.Path)
}
func HandleNotFound(w http.ResponseWriter, req *http.Request) {
// w.Write("not found")
}
now, we can bootstrap our http cache server to store keys in both memory and disk!