order-up
Code candidates will extend as part of their technical interview process. The
order-up service handles all order-specific calls including creating orders,
checking the status on orders, etc. This service is part of a larger microservice
backend for a online marketplace.
Getting started
You also will need to install Go. Then clone this
this repository and run go mod tidy
within this repository to download all
necessary dependencies locally.
Project Structure
Top-level
The top-level only contains a single main.go
file which holds the main
function. If you ran go build ./.
that would produce a order-up
binary that
would start by executing the main
function in main.go
.
api package
The api
package handles incoming HTTP requests with a REST paradigm and calls
various functions based on the path. This package uses the storage
package to
perform the necessary functionality for each API call. The tests use a mocked
storage instance.
storage package
The storage
package contains the database calls necessary for persisting and
retrieving orders. The methods are missing the bodies of the functions since
you're expected to fill them in with whatever database and implementation you
think satisfies the tests and documented functionality.
mocks package
The mocks
package just contains a helper function for mocking an external
service by accepting an http.Handler and returning a *http.Client as well as
generated code for mocking a *storage.Instance
. This simply makes the tests
easier in the api
package.
Relevant Go commands
go mod tidy
downloads all dependencies
and update go.mod
file with any new dependencies
go test -v -race ./...
tests all
files and subdirectories. You can instead do go test -v ./storage/...
to only
test the storage package. Any public function with the format TestX(*testing.T)
will automatically be called by go test
. Typically these functions are placed
in X_test.go
files. You can pass a regex to -run
like -run ^TestInsertOrder$
in order to just run tests matching the regex.
go fmt ./...
reformats the go files according to the gofmt spec
go vet ./...
prints out most-likely errors or mistakes in Go code
go get $package
adds a new dependency to the current project
Databases
The easiest way to run databases locally for testing is using
Docker.
You can use any database you're familiar with these are just some examples.
Alternatively you can sign up for an online free tier of a hosted version of
these databases (like MongoDB Atlas, or CockroachDB Cloud) if that's easier.
MongoDB
docker run --rm -it -p 27017:27017 mongo
PostgreSQL
docker run --rm -it -p 5432:5432 -e POSTGRES_PASSWORD=password postgres
Redis
docker run --rm -it -p 6379:6379 redis
Relevant Database Packages
You can use any database or driver you're familiar with these are just some
examples.
Remember when you're adding new packages to run go mod tidy
to ensure the
go.mod and go.sum files are updated.