-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
53 lines (46 loc) · 990 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"net/http"
"os"
"time"
"github.com/chickenzord/reqstash/pkg/reqstash"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"gopkg.in/yaml.v2"
)
func main() {
godotenv.Overload()
var cfg reqstash.Config
if err := envconfig.Process("reqstash", &cfg); err != nil {
panic(err)
}
yaml.NewEncoder(os.Stdout).Encode(map[string]interface{}{
"config": cfg,
})
s := reqstash.MemoryStorage{
Capacity: 2,
TTL: 10 * time.Second,
}
srv := gin.Default()
srv.GET("/", func(c *gin.Context) {
reqs, err := s.ListAll()
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, gin.H{
"requests": reqs,
})
})
srv.Any("/record", func(c *gin.Context) {
req := reqstash.NewRequest(c.Request)
s.Put(req)
c.JSON(http.StatusAccepted, gin.H{
"request": req,
})
})
if err := http.ListenAndServe(":8080", srv); err != nil {
panic(err)
}
}