Skip to content

Commit d184cbc

Browse files
committed
add latency and CORS
1 parent cf0adaa commit d184cbc

File tree

7 files changed

+77
-46
lines changed

7 files changed

+77
-46
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.json

.goreleaser.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ archives:
1717
amd64: 64-bit
1818
386: 32-bit
1919
darwin: macOS
20-
linux: Tux
20+
linux: linux
2121
windows: Win
2222
wrap_in_directory: true
2323
format: binary

README.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,30 @@
33
The tiniest http endpoints simulator
44

55
## Status
6-
This project is under development. No use this in production :)
6+
This project is under development. No use this in production :)
7+
8+
9+
####
10+
11+
#####endpoints.json
12+
```json
13+
{
14+
"address": ":8080",
15+
"responses": [
16+
{
17+
"path": "/backend/test/{name}",
18+
"status": 201,
19+
"latency": "400ms",
20+
"json_body": "back.json"
21+
}
22+
]
23+
}
24+
```
25+
26+
##### JSON body
27+
eg.: back.json
28+
```json
29+
{
30+
"nome": "{{ .name}}"
31+
}
32+
```

endpoints.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
module endpoints
22

33
go 1.14
4+
5+
require (
6+
github.com/go-playground/validator/v10 v10.3.0
7+
github.com/gorilla/handlers v1.4.2
8+
github.com/gorilla/mux v1.7.4
9+
github.com/kr/pretty v0.1.0 // indirect
10+
github.com/sirupsen/logrus v1.6.0
11+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
12+
)

main.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,72 @@ package main
22

33
import (
44
"encoding/json"
5-
"fmt"
5+
"github.com/gorilla/handlers"
66
"github.com/gorilla/mux"
7+
log "github.com/sirupsen/logrus"
78
"io/ioutil"
8-
"log"
99
"net/http"
1010
"text/template"
11+
"time"
1112
)
1213

1314
type ConfigFile struct {
14-
Addr string `json:"address"`
15-
Responses []Response
15+
Addr string `json:"address"`
16+
Responses []Response `json:"responses"`
1617
}
1718
type Response struct {
18-
Status int
19-
Path string
20-
JsonBody string
19+
Status int `json:"status"`
20+
Path string `json:"path"`
21+
Latency string `json:"latency"`
22+
JsonBody string `json:"json_body"`
2123
}
2224

2325
func main() {
2426
file, err := ioutil.ReadFile("endpoints.json")
2527
if err != nil {
26-
log.Fatal(err)
28+
log.Fatal(err.Error())
2729
}
2830
configFile := ConfigFile{}
2931
err = json.Unmarshal(file, &configFile)
3032
if err != nil {
31-
log.Fatal(err)
33+
log.Fatal(err.Error())
3234
}
35+
3336
router := mux.NewRouter()
37+
cors := handlers.CORS(
38+
handlers.AllowedHeaders([]string{"content-type"}),
39+
handlers.AllowedOrigins([]string{"*"}),
40+
handlers.AllowCredentials(),
41+
)
42+
router.Use(cors)
3443

3544
for _, response := range configFile.Responses {
36-
closureResponse := response
37-
fmt.Println(closureResponse.Path, "will return status", closureResponse.Status, "with body from file ", closureResponse.JsonBody)
38-
router.HandleFunc(closureResponse.Path, func(writer http.ResponseWriter, request *http.Request) {
39-
vars := mux.Vars(request)
40-
if 200 != closureResponse.Status {
41-
writer.WriteHeader(closureResponse.Status)
45+
closure := response
46+
log.Println(closure.Path, "will return status", closure.Status, "with body from file", closure.JsonBody)
47+
router.HandleFunc(closure.Path, func(writer http.ResponseWriter, request *http.Request) {
48+
if closure.Status != 200 {
49+
writer.WriteHeader(closure.Status)
4250
}
4351
writer.Header().Add("Content-Type", "application/json")
44-
jsonBytes, err := ioutil.ReadFile(closureResponse.JsonBody)
52+
53+
tmpl, err := template.ParseFiles(closure.JsonBody)
4554
if err != nil {
46-
log.Fatal(err)
55+
log.Error(err.Error())
56+
writer.WriteHeader(500)
57+
return
58+
}
59+
if closure.Latency != "" {
60+
duration, err := time.ParseDuration(closure.Latency)
61+
if err != nil {
62+
log.Error(err.Error())
63+
}
64+
time.Sleep(duration)
4765
}
4866

49-
t := template.Must(template.New("letter").Parse(string(jsonBytes)))
50-
_ = t.Execute(writer, vars)
67+
err = tmpl.Execute(writer, mux.Vars(request))
68+
if err != nil {
69+
log.Error(err.Error())
70+
}
5171
})
5272
}
5373
http.Handle("/", router)

0 commit comments

Comments
 (0)