Skip to content

Instantly share code, notes, and snippets.

@Attumm
Last active March 14, 2019 07:11
Show Gist options
  • Select an option

  • Save Attumm/e271a0d8b9c9930e550ebeea322bca85 to your computer and use it in GitHub Desktop.

Select an option

Save Attumm/e271a0d8b9c9930e550ebeea322bca85 to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
)
//Item bla
type Item struct {
ID int
Name string
Timestamp int64
Bla int
Bla1 int
Bla2 int
Bla3 int
Bla4 int
Bla5 int
Bla6 int
Bla7 int
Bla8 int
Bla9 int
}
func (i Item) getMapID() string {
return time.Unix(i.Timestamp, 0).Format("2006-01-02T15")
}
func (i Item) getStrWeek() string {
return time.Unix(i.Timestamp, 0).Weekday().String()
}
func (i Item) isWeekend() bool {
return int(time.Unix(i.Timestamp, 0).Weekday()) >= 5
}
func rest(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
search := filterEmpty(strings.Split(r.URL.Path, "/"))
if len(search) > 2 {
ErrorResponse(w, "File(s) not found", http.StatusNotFound)
return
}
if len(search) == 2 {
if search[0] == "func" {
if f, ok := registerFuncs[search[1]]; ok {
json.NewEncoder(w).Encode(f())
return
} else {
ErrorResponse(w, "File(s) not found", http.StatusNotFound)
return
}
}
item := toplevel[search[0]][search[1]]
json.NewEncoder(w).Encode(item)
return
}
var items cacheType
if len(search) == 1 {
if val, ok := toplevel[search[0]]; ok {
items = val
} else {
ErrorResponse(w, "File(s) not found", http.StatusNotFound)
return
}
}
w.Header().Set("Total-Items", strconv.Itoa(len(items)))
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(items)
}
type cacheType map[string][]*Item
type filterFunc func() []*Item
var toplevel = make(map[string]cacheType)
var registerFuncs = make(map[string]filterFunc)
func getItems() {
cache := make(cacheType)
//amount := 10000 // 10k
//amount := 100000 // 100k
//amount := 1000000 // 1m
amount := 10000000 // 10m
//amount:= 100000000 // 100m
for i := 0; i < amount; i++ {
item := &Item{
ID: i,
Name: fmt.Sprintf("Name: %d", i),
Timestamp: int64(1552500000 + (i *10)),
Bla: i,
Bla1: i,
Bla2: i,
Bla3: i,
Bla4: i,
Bla5: i,
Bla6: i,
Bla7: i,
Bla8: i,
Bla9: i,
}
cache[item.getMapID()] = append(cache[item.getMapID()], item)
}
toplevel["all"] = cache
fmt.Println("registerd the 'all' cache")
}
func main() {
registerFuncs["weekends"] = filterWeekend
getItems()
go getWeekdays()
go etExtra("bla")
go getExtra("bla1")
go getExtra("bla2")
http.HandleFunc("/", rest)
fmt.Println("starting server")
log.Fatal(http.ListenAndServe("0:8080", nil))
}
func filterEmpty(ss []string) []string {
newS := []string{}
for _, item := range ss {
if len(item) >= 1 {
newS = append(newS, item)
}
}
return newS
}
func ErrorResponse(w http.ResponseWriter, reason string, httpStatus int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(httpStatus)
json.NewEncoder(w).Encode(ErrorMsg{Error: "Error", Reason: reason, HTTPStatus: httpStatus})
}
//ErrorMsg Response structs
type ErrorMsg struct {
Error string
Reason string
HTTPStatus int
}
func getExtra(msg string) {
bla := make(cacheType)
for _, v := range toplevel["all"] {
for _, item := range v {
newKey := item.getStrWeek()
bla[newKey] = append(bla[newKey], item)
}
}
toplevel[msg] = bla
fmt.Println("registered", msg, "to cache")
}
func getWeekdays() {
weekend := make(cacheType)
weekday := make(cacheType)
for _, v := range toplevel["all"] {
for _, item := range v {
newKey := item.getStrWeek()
if item.isWeekend() {
weekend[newKey] = append(weekend[newKey], item)
} else {
weekday[newKey] = append(weekday[newKey], item)
}
}
}
fmt.Println("registered 'weekend' to cache")
fmt.Println("registered 'weekday' to cache")
toplevel["weekend"] = weekend
toplevel["weekday"] = weekday
}
func filterWeekend() []*Item {
ll := []*Item{}
for _, v := range toplevel["all"] {
for _, item := range v {
if item.isWeekend() {
ll = append(ll, item)
}
}
}
return ll
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment