|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "github.com/gorilla/mux" |
| 9 | + "math" |
| 10 | + "math/rand" |
| 11 | + "net/http" |
| 12 | + "strconv" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +var m map[string]GaussianInstance |
| 18 | + |
| 19 | +type GaussianInstance struct { |
| 20 | + Id string `json:"id"` |
| 21 | + Hash string `json:"hash"` |
| 22 | + RandHash int64 `json:"randHash"` |
| 23 | + Mu float64 `json:"mu"` |
| 24 | + Sigma float64 `json:"sigma"` |
| 25 | + R *rand.Rand `json:"r"` |
| 26 | + Count int64 `json:"count"` |
| 27 | + Time int64 `json:"time"` |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +func newRouter() *mux.Router { |
| 32 | + router := mux.NewRouter() |
| 33 | + return router |
| 34 | +} |
| 35 | + |
| 36 | +func RandomFloatRange(R *rand.Rand, min float64, max float64) float64 { |
| 37 | + return float64(min) + R.Float64()*(float64(max)-float64(min)) |
| 38 | +} |
| 39 | + |
| 40 | +func RandomGaussian(gi GaussianInstance) float64 { |
| 41 | + u1 := RandomFloatRange(gi.R, 0.0, 1.0) |
| 42 | + u2 := RandomFloatRange(gi.R, 0.0, 1.0) |
| 43 | + |
| 44 | + z1 := (math.Sqrt(-2*math.Log(u1)) * math.Cos(2*math.Pi*u2)) |
| 45 | + |
| 46 | + return z1*gi.Sigma + gi.Mu |
| 47 | +} |
| 48 | + |
| 49 | +func instantiate(gi GaussianInstance) GaussianInstance { |
| 50 | + gi.RandHash = convertHashToInt(gi.Hash) |
| 51 | + gi.R = rand.New(rand.NewSource(gi.RandHash)) |
| 52 | + gi.Mu = RandomFloatRange(gi.R, -100.0, 100.0) |
| 53 | + gi.Sigma = RandomFloatRange(gi.R, -50.0, 50.0) |
| 54 | + gi.Count = 1 |
| 55 | + gi.Time = time.Now().Unix() |
| 56 | + return gi |
| 57 | +} |
| 58 | + |
| 59 | +func FilterString(input string) (filtered string) { |
| 60 | + chars := "abcdefghijklmnopqrstuvwxyzABCDEFGHGIJKLMNOPQRSTUVWXYZ0123456789" |
| 61 | + for _, c := range input { |
| 62 | + if strings.ContainsAny(chars, fmt.Sprintf("%c", c)) == true { |
| 63 | + filtered += fmt.Sprintf("%c", c) |
| 64 | + } |
| 65 | + } |
| 66 | + return filtered |
| 67 | +} |
| 68 | + |
| 69 | +func endpoint(w http.ResponseWriter, r *http.Request) { |
| 70 | + vars := mux.Vars(r) |
| 71 | + var gi GaussianInstance |
| 72 | + gi.Id = FilterString(vars["id"]) |
| 73 | + if len(gi.Id) >= 4 && len(gi.Id) <= 16 { |
| 74 | + b := sha256.Sum256([]byte(gi.Id)) |
| 75 | + hash := hex.EncodeToString(b[:]) |
| 76 | + gi.Hash = hash |
| 77 | + if (GaussianInstance{} == m[hash]) { |
| 78 | + m[hash] = instantiate(gi) |
| 79 | + } |
| 80 | + gi = m[hash] |
| 81 | + |
| 82 | + fmt.Println(vars["id"]) |
| 83 | + |
| 84 | + if time.Now().UnixNano() - gi.Time <= 500000000 && vars["id"] != "golang" { |
| 85 | + w.WriteHeader(http.StatusTooManyRequests) |
| 86 | + return |
| 87 | + } |
| 88 | + |
| 89 | + var re []string |
| 90 | + for z := 0; z < 100; z++ { |
| 91 | + re = append(re, fmt.Sprintf("%f", RandomGaussian(m[hash]))) |
| 92 | + } |
| 93 | + |
| 94 | + gi = m[hash] |
| 95 | + gi.Count += 1 |
| 96 | + gi.Time = time.Now().UnixNano() |
| 97 | + m[hash] = gi |
| 98 | + |
| 99 | + json.NewEncoder(w).Encode(re) |
| 100 | + |
| 101 | + } else { |
| 102 | + w.WriteHeader(http.StatusBadRequest) |
| 103 | + return |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func convertHashToInt(hash string) int64 { |
| 108 | + var returnInteger string |
| 109 | + chars := "abcdefghijklmnopqrstuvwxyz" |
| 110 | + for _, c := range hash { |
| 111 | + if strings.ContainsAny(chars, fmt.Sprintf("%c", c)) == false { |
| 112 | + returnInteger += fmt.Sprintf("%c", c) |
| 113 | + } |
| 114 | + } |
| 115 | + fmt.Println(returnInteger) |
| 116 | + i, err := strconv.Atoi(returnInteger[:10]) |
| 117 | + if err != nil { |
| 118 | + fmt.Println(err) |
| 119 | + } |
| 120 | + return int64(i) |
| 121 | +} |
| 122 | + |
| 123 | +func fetch(w http.ResponseWriter, r *http.Request) { |
| 124 | + vars := mux.Vars(r) |
| 125 | + b := sha256.Sum256([]byte(FilterString(vars["id"]))) |
| 126 | + hash := hex.EncodeToString(b[:]) |
| 127 | + var gi GaussianInstance |
| 128 | + fmt.Println(m[hash]) |
| 129 | + gi = m[hash] |
| 130 | + |
| 131 | + data, err := json.Marshal(gi) |
| 132 | + fmt.Println(data) |
| 133 | + |
| 134 | + if err != nil { |
| 135 | + fmt.Println(err) |
| 136 | + } else { |
| 137 | + json.NewEncoder(w).Encode(gi) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func handleRequests() { |
| 142 | + router := newRouter().StrictSlash(true) |
| 143 | + router.HandleFunc("/{id}", middleware(endpoint)).Methods("GET") |
| 144 | + router.HandleFunc("/fetchmethisshit/{id}", middleware(fetch)).Methods("GET") |
| 145 | + err := http.ListenAndServe(":8080", router) |
| 146 | + if err != nil { |
| 147 | + fmt.Println(err) |
| 148 | + } else { |
| 149 | + fmt.Println("Serving: :8080") |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +func middleware(f http.HandlerFunc) http.HandlerFunc { |
| 154 | + return func(w http.ResponseWriter, r *http.Request) { |
| 155 | + fmt.Println(r.Host, r.URL.Path, r.Method) |
| 156 | + f(w, r) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func main() { |
| 161 | + m = make(map[string]GaussianInstance) |
| 162 | + handleRequests() |
| 163 | +} |
0 commit comments