Created
July 29, 2020 16:14
-
-
Save jeffotoni/b3319de3f993e580e6862ccdf9f4944e to your computer and use it in GitHub Desktop.
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 ( | |
"encoding/json" | |
"log" | |
"math/rand" | |
"net/http" | |
"time" | |
) | |
type MsgError struct { | |
Msg string `json:"msg"` | |
} | |
type Actions struct { | |
ID string `json:"id"` | |
Value float64 `json:"value"` | |
PaidAt string `json:"paid_at"` | |
IPLocation string `json:"ip_location"` | |
CardHoldName string `json:"card_hold_name"` | |
Customer Customer `json:"customer"` | |
} | |
type Customer struct { | |
ID string `json:"id"` | |
Name string `json:"name"` | |
BirthDate string `json:"birth_date"` | |
State string `json:"state"` | |
Phone string `json:"phone"` | |
} | |
type ActionOutput struct { | |
ID string `json:"id"` | |
Value int `json:"value"` | |
} | |
var ( | |
PORT = ":8081" | |
msgErr MsgError | |
) | |
func Score(w http.ResponseWriter, r *http.Request) { | |
if r.Method != http.MethodPost { | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
w.Write(JsonMsg("O metodo permitido é somente POST")) | |
return | |
} | |
var a []Actions | |
err := json.NewDecoder(r.Body).Decode(&a) | |
if err != nil { | |
w.WriteHeader(http.StatusBadRequest) | |
w.Write(JsonMsg(err.Error())) | |
return | |
} | |
var aoutv []ActionOutput | |
for _, v := range a { | |
var aout ActionOutput | |
aout.ID = v.ID | |
aout.Value = ScoreFunc(v.ID) | |
aoutv = append(aoutv,aout) | |
} | |
b, err := json.Marshal(aoutv) | |
if err!=nil { | |
w.WriteHeader(http.StatusBadRequest) | |
w.Write(JsonMsg(err.Error())) | |
return | |
} | |
w.WriteHeader(200) | |
w.Write(b) | |
} | |
func ScoreFunc(id string) int { | |
score := randInt(1,98) | |
if score <= 10 && score > 0 { | |
return randInt(0,score) | |
} | |
if score < 50 { | |
return randInt(30,score+30) | |
} | |
if score >= 50 { | |
return randInt(score,100) | |
} | |
return randInt(10,30) | |
} | |
func randInt(min int, max int) int { | |
if min < max { | |
return min + rand.Intn(max-min) | |
} | |
return 0 | |
} | |
func JsonMsg(msg string) []byte { | |
msgErr.Msg = msg | |
b, err := json.Marshal(msgErr) | |
if err != nil { | |
log.Println(err) | |
return []byte(`{"msg":"` + err.Error() + `"}`) | |
} | |
return b | |
} | |
func main() { | |
mux := http.NewServeMux() | |
mux.HandleFunc("/v1/score", Score) | |
server := | |
&http.Server{ | |
Addr: PORT, | |
Handler: mux, | |
ReadTimeout: time.Millisecond * 500, | |
WriteTimeout: time.Millisecond * 700, | |
MaxHeaderBytes: 10485760, // 10MB | |
} | |
log.Printf("\nRun Server port: %s", PORT) | |
if err := server.ListenAndServe(); err != nil { | |
log.Printf("Eror while serving metrics: %s", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment