al-order
is a simple demo using Google Map Direction API to simluate the real-world operation of placing order, taking order, and listing orders.
The follow documentation will introduce its usages.
- First, clone the repo:
git clone https://github.com/DemoHn/al-order
cd al-order
- Before start, make sure your machine is equipped with
docker
anddocker-compose
. - Then, copy an
.env
file from.env.example
by promptingcp .env.example .env
NOTICE: since the redis & mysql dependencies are running in docker, the hostname should be redis
, mysql
respectively, instead of localhost
. You don't need to change DATABASE_URL
and REDIS_ADDR
in .env.example
if no special requirement.
You may need to copy your Google Map's Direction API key to GOOGLE_MAP_APIKEY
in .env
.
- Prompt
./start.sh
. This will build up & execute the docker image and its dependencies (redis
,mysql
).
- Simply prompt
./build.sh
, which will execute the following command:
docker-compose build al-order
unit test is a little bit tricky: it will run go test
directly on codebase instead of docker image.
-
Before unit test, edit
.env
to replace the original hostnamemysql
,redis
tolocalhost
since we will connect them via host network directly. -
Simply execute
./test.sh
Alternatively, you can build the project without docker.
Before start, make sure your Go version is >=1.12
.
Then build the project:
go build -o ./alorder ./cmd/main.go
Which will yield alorder
, the final executable binary file.
There're 3 options in alorder
:
-host
: server listen host. default is127.0.0.1
-sqlFile
: sql file for execution before starting server. This is usually for migration. default is""
.-port
: server listen port. default is8080
.
al-order
is a typical project using MVC architechture. It consists of 3 layers:
Request Response
| /|\
| |
| |
\|/ |
+--------------------+
| Controller | <---- Validate Input & Handle Response
+--------------------+ +-------+
| Service | <---- | Redis | (using redis for lock Taken)
+--------------------+ +-------+
| DAO (repo) | <----> using MySQL for storing data
+--------------------+
The following diagram explains the workflow of total 3 operations:
All possible errors will be shown here:
Name | Code | StatusCode | Message |
---|---|---|---|
ErrIDNotFound | 10000 | 400 | orderID Not Found |
ErrDBFatal | 10001 | 500 | fatal error: db operation error |
ErrRedisFatal | 10002 | 500 | fatal error: redis operation error |
ErrGoogleMapService | 10003 | 500 | fetch google map direction data failed |
ErrNoRoute | 10004 | 400 | there's no available route from origin -> destination |
ErrOrderHasTaken | 10005 | 400 | order has been taken by somebody else |
ErrInputValidation | 10006 | 400 | validate input data failed: xxx |
ErrUnknownFatal | 11000 | 500 | unknown fatal error |
ErrGeneralHTTP | 12000 | not fixed | general HTTP error: xx |