Created
November 2, 2016 11:46
-
-
Save julianfrank/8b1019de2e5c0d4074b59f2860d42be7 to your computer and use it in GitHub Desktop.
Simple Caching for single server instance in go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
"strings" | |
"time" | |
) | |
var ( | |
localMap map[string]string | |
) | |
func apiRouter(w http.ResponseWriter, r *http.Request) { | |
switch r.Method { | |
case http.MethodGet: | |
{ | |
pathLen := len(r.URL.Path) | |
pathArray := strings.Split(r.URL.Path, "/") | |
switch pathLen { | |
case 1: // Root path | |
{ | |
io.WriteString(w, "Cache Service | Unhandled Method => Method:"+r.Method+" Path:"+r.URL.Path+" Query:"+r.URL.RawQuery) | |
io.WriteString(w, "\n Current Map\n") | |
for k, v := range localMap { | |
io.WriteString(w, "\n"+k+"\t: "+v) | |
} | |
} | |
default: | |
{ | |
switch strings.ToLower(pathArray[1]) { //pathArray[0] is always"" as it comes before the first / | |
case "add": | |
{ | |
queryLen := len(r.URL.RawQuery) | |
if queryLen > 3 { // 3 Chars minimum required for key value pair | |
queryArray := strings.Split(r.URL.RawQuery, "&") //Assuming & will be used to separate the query strings | |
for _, v := range queryArray { | |
if strings.Contains(v, "=") { //Assuming = will be used to separate the key and values | |
x := strings.ToLower(strings.Split(v, "=")[0]) | |
y := strings.Split(v, "=")[1] | |
localMap[x] = y | |
} else { | |
localMap[v] = "" // HAndle cases wherevalue is not provided | |
} | |
} | |
io.WriteString(w, "{addstatus:true}") //[TODO]Make it return a proper json string | |
} else { | |
io.WriteString(w, "{addstatus:false}") //[TODO]Make it return a proper json string | |
} | |
} | |
case "check": | |
{ | |
i, ok := localMap[strings.ToLower(r.URL.RawQuery)] | |
if ok { | |
io.WriteString(w, "{value:"+i+"}") | |
} else { | |
io.WriteString(w, "{value:false}") | |
} | |
} | |
case "delete": | |
{ | |
_, ok := localMap[strings.ToLower(r.URL.RawQuery)] | |
if ok { | |
delete(localMap, strings.ToLower(r.URL.RawQuery)) | |
io.WriteString(w, "{deletestatus:true}") | |
} else { | |
io.WriteString(w, "{deletestatus:false}") | |
} | |
} | |
default: | |
{ | |
io.WriteString(w, "Cache Service | Unhandled Request => Method:"+r.Method+" Path:"+r.URL.Path+" Query:"+r.URL.RawQuery+" pathLen:"+strconv.Itoa(pathLen)+" pathArray"+strings.Join(pathArray[:], ",")) | |
} | |
} | |
} | |
} | |
} | |
default: | |
{ | |
io.WriteString(w, "This is the Cache Service") | |
} | |
} | |
} | |
func main() { | |
mux := http.NewServeMux() | |
// Initialize local key store | |
localMap = make(map[string]string) | |
// Application Route Handlers | |
mux.HandleFunc("/", apiRouter) | |
serverPort := os.Getenv("PORT") | |
if len(serverPort) < 2 { | |
serverPort = "80" | |
} | |
log.Println("CACHE Listening on " + serverPort) | |
s := &http.Server{ | |
Addr: ":" + serverPort, | |
Handler: mux, | |
ReadTimeout: 10 * time.Second, | |
WriteTimeout: 10 * time.Second, | |
MaxHeaderBytes: 1 << 20, | |
} | |
log.Fatal(s.ListenAndServe()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment