A RESTful api that's based on Marvel API that returns:
- all the Marvel character
ids
in a JSON array of numbers. - the
id
,name
anddescription
of a character.
- Sign up for a free Marvel Developer API at https://developer.marvel.com/.
- Go version 1.15 or greater.
- Install mux router to implement a request router and dispatcher for matching incoming requests to their respective handler.
- Install Go CORS handler to handle cors requests.
- This api is developed and verified in Ubuntu 18.04.5 LTS
go get -u github.com/albertleng/restapi
go get -u github.com/gorilla/mux
go get -u github.com/rs/cors
To run this project, you will need to add the following environment variables:
MARVEL_API_PRIVATE_KEY
MARVEL_API_PUBLIC_KEY
Run the build.sh
to build and run the rest api
./build.sh
or
cd "$GOPATH"/src/github.com/albertleng/restapi || exit
go build -o restapi
echo "Go build done"
./restapi
GET /characters
GET /characters/{characterId}
main_test.go
contains tests for /characters
and /characters/{characterId}
.
# run test
go test -v
This api uses a simple text file ids.txt
to store the character ids
.
In response to a /characters
call, there will be two scenarios:
The api reads all the character ids
from ids.txt
into an integer array, ids
.
- If it is the first call after the api starts, the api makes calls to
https://gateway.marvel.com/v1/public/characters
withoffset
starting with length ofids
(and increment of 100 in subsequent calls) andlimit
of 100 until there is no character returned. These character ids are appended to bothids.txt
andids
. The updatedids
is returned as a response to the caller. (Notes: the calls tohttps://gateway.marvel.com/v1/public/characters
are to account for the fact that there were new Marvel character(s) added where are not inids.txt
) - If it is not the first call after the api starts, the api returns
ids
as a response to the caller.
The api makes calls to https://gateway.marvel.com/v1/public/characters
with offset
starting with 0
(and increment
of 100 in subsequent calls) and limit
of 100 until there is no character returned. In each call, the character ids are
appended to an integer array, ids
. The file ids.txt
is created, and ids
is written to ids.txt
. The ids
is
returned as a response to the caller.
1011334
1017100
1009144
1010699
1009146
1016823
1009148
1009149
1010903
...
- Add caching of
id
,name
anddescription
, read from cache and return it as response to calls to/characters/{characterId}
to reduce latency. - Add
TLS/https
to encrypt requests and responses. - Refactor codes for better readability and maintainability.
- Key Improvements:
- No project structure
- Caching strategy is not complete. No cache invalidation unless app is restarted.
- Does not pull data from marvel in parallel which means very slow API call.
- No error handling, app crashes when sending an invalid character id.
- Could have made it easier to run by using go mod.
- No unit testing for cache logic
- The code structure is not good. No separation of concern, not much abstraction to make it easier to maintain.
- Caching strategy is not accounting for updated characters.
- Cache refresh requires app restart.
- HTTP request is performed sequentially.