A RESTful api that's based on Marvel API that returns:
- all the Marvel character
idsin a JSON array of numbers. - the
id,nameanddescriptionof 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/restapigo get -u github.com/gorilla/muxgo get -u github.com/rs/corsTo 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.shor
cd "$GOPATH"/src/github.com/albertleng/restapi || exit
go build -o restapi
echo "Go build done"
./restapi GET /charactersGET /characters/{characterId}main_test.go contains tests for /characters and /characters/{characterId}.
# run test
go test -vThis 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/characterswithoffsetstarting with length ofids(and increment of 100 in subsequent calls) andlimitof 100 until there is no character returned. These character ids are appended to bothids.txtandids. The updatedidsis returned as a response to the caller. (Notes: the calls tohttps://gateway.marvel.com/v1/public/charactersare 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
idsas 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,nameanddescription, read from cache and return it as response to calls to/characters/{characterId}to reduce latency. - Add
TLS/httpsto 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.