wip
(req *http.Request)
| 34 | |
| 35 | // wip |
| 36 | func fireHandler(req *http.Request) *http.Response { |
| 37 | var err error |
| 38 | |
| 39 | apiKey := req.Header.Get("X-Api-Key") |
| 40 | if apiKey != validApiKey { |
| 41 | log.Warningf("invalid api key: %s", apiKey) |
| 42 | |
| 43 | return &http.Response{ |
| 44 | StatusCode: http.StatusForbidden, |
| 45 | Body: nil, |
| 46 | Header: make(http.Header), |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | //unmarshal data |
| 51 | if fireResponses == nil { |
| 52 | page1, err := os.ReadFile("testdata/fire-page1.json") |
| 53 | if err != nil { |
| 54 | panic("can't read file") |
| 55 | } |
| 56 | |
| 57 | page2, err := os.ReadFile("testdata/fire-page2.json") |
| 58 | if err != nil { |
| 59 | panic("can't read file") |
| 60 | } |
| 61 | |
| 62 | fireResponses = []string{string(page1), string(page2)} |
| 63 | } |
| 64 | //let's assume we have two valid pages. |
| 65 | page := 1 |
| 66 | if req.URL.Query().Get("page") != "" { |
| 67 | page, err = strconv.Atoi(req.URL.Query().Get("page")) |
| 68 | if err != nil { |
| 69 | log.Warningf("no page ?!") |
| 70 | return &http.Response{StatusCode: http.StatusInternalServerError} |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | //how to react if you give a page number that is too big ? |
| 75 | if page > len(fireResponses) { |
| 76 | log.Warningf(" page too big %d vs %d", page, len(fireResponses)) |
| 77 | |
| 78 | emptyResponse := `{ |
| 79 | "_links": { |
| 80 | "first": { |
| 81 | "href": "https://cti.api.crowdsec.net/v1/fire/" |
| 82 | }, |
| 83 | "self": { |
| 84 | "href": "https://cti.api.crowdsec.net/v1/fire/?page=3&limit=3" |
| 85 | } |
| 86 | }, |
| 87 | "items": [] |
| 88 | } |
| 89 | ` |
| 90 | |
| 91 | return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(emptyResponse))} |
| 92 | } |
| 93 |