-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.go
More file actions
109 lines (92 loc) · 2.36 KB
/
main.go
File metadata and controls
109 lines (92 loc) · 2.36 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/valyala/fasthttp"
"log"
)
var (
addr = flag.String("addr", "localhost:6060", "TCP address to listen to")
token = flag.String("token", "", "TheCatAPI token")
)
func main() {
log.Print("- Loading cat-api-wrapper")
maxBodySize := 100 * 1024 * 1024 // 100 MiB
s := &fasthttp.Server{
Handler: requestHandler,
Name: "cat-api-wrapper",
MaxRequestBodySize: maxBodySize,
}
if err := s.ListenAndServe(*addr); err != nil {
log.Fatalf("- Error in ListenAndServe: %s", err)
}
}
func requestHandler(ctx *fasthttp.RequestCtx) {
switch string(ctx.Method()) {
case fasthttp.MethodGet:
handleGet(ctx)
case fasthttp.MethodPost:
handleApi(ctx)
default:
handleError(ctx, 400, "Bad Method")
}
}
type CatApiObj struct {
Breeds []string `json:"breeds"`
ID string `json:"id"`
URL string `json:"url"`
Width int64 `json:"width"`
Height int64 `json:"height"`
}
func requestCat(ctx *fasthttp.RequestCtx) CatApiObj {
body, err := RequestUrl("https://api.thecatapi.com/v1/images/search", "x-api-key", *token)
if err != nil {
panic(err)
}
log.Printf("body: %s\n", body)
var objs []CatApiObj
if err := json.Unmarshal(body, &objs); err != nil {
panic(err)
}
return objs[0]
}
func handleGet(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/api/random":
cat := requestCat(ctx)
fmt.Fprintf(ctx, "%s\n", cat.URL)
default:
handleError(ctx, 400, "Bad GET path")
}
}
// handleApi will handle POST requests to the API
func handleApi(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/api/created":
ctx.SetStatusCode(201)
fmt.Fprintf(ctx, "201 Created\n")
default:
handleError(ctx, 400, "Bad API path")
}
}
func handleError(ctx *fasthttp.RequestCtx, status int, msg string) {
ctx.SetStatusCode(status)
fmt.Fprintf(ctx, "%v %s\n", status, msg)
}
// RequestUrl will return the bytes of the body of url
func RequestUrl(url string, header string, value string) ([]byte, error) {
req := fasthttp.AcquireRequest()
defer fasthttp.ReleaseRequest(req)
req.SetRequestURI(url)
req.Header.Set(header, value)
resp := fasthttp.AcquireResponse()
defer fasthttp.ReleaseResponse(resp)
// Perform the request
err := fasthttp.Do(req, resp)
if err != nil {
fmt.Printf("Client get failed: %s\n", err)
return nil, err
}
return resp.Body(), nil
}