From 43d6388ecbe3bfa82ee0671c2d27465dd775c1c0 Mon Sep 17 00:00:00 2001 From: Baraa Al-Masri Date: Thu, 4 Jan 2024 21:21:14 +0300 Subject: [PATCH] Add docs and images limit --- README.md | 21 +++++++++++++++++++-- images.go | 5 ++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 6c8dab9..6cab944 100755 --- a/README.md +++ b/README.md @@ -22,7 +22,24 @@ Sample response: ```json { - "text": "Great ideas often receive violent opposition from mediocre minds.", - "author": "Albert Einstein" + "text": "Great ideas often receive violent opposition from mediocre minds.", + "author": "Albert Einstein" +} +``` + +**GET https://apis.gdscasu.com/image** + +An endpoint that returns quote and who said it, from a pre defined list. + +Query parameters: + +- `count` (optional): specifies the number of returned images' paths. +- `orientation` (default is landscape): specifies the orientation of the returned image(s). + +Sample response: + +```json +{ + "image": ["/files/images/landscape1.jpg"] } ``` diff --git a/images.go b/images.go index 53969c1..580b0b8 100644 --- a/images.go +++ b/images.go @@ -2,6 +2,7 @@ package main import ( "encoding/json" + "math" "math/rand" "net/http" "os" @@ -54,12 +55,10 @@ func handleGetImage(w http.ResponseWriter, r *http.Request) { if parsedCount, err := strconv.Atoi(r.URL.Query().Get("count")); err == nil { count = parsedCount } - images := []string{getRandomImage(orientation)} - for i := 1; i < count; i++ { + for i := 1; i < int(math.Min(float64(count), float64(len(imagesPaths)))); i++ { images = append(images, getRandomImage(orientation)) } - _ = json.NewEncoder(w).Encode(map[string]any{ "images": images, })