This repository contains the solution for a coding assigment sent out by Ingrid. The assignment involves creating a REST API in Go to calculate the duration and distance between multiple destinations and a source point using the OSRM API.
coding-assignment
├─ Dockerfile
├─ go.mod
├─ main.go
├─ osrm
│ └─ request.go
├─ README.md
└─ server
├─ handlers.go
└─ mux.go
- Go 1.22.3 or higher
- Docker (optional, for containerized deployment)
-
Clone the repository to your local machine:
$ git clone https://github.com/socketopp/coding-assignment.git
-
Navigate to the project directory:
$ cd coding-assignment`
To run the application locally, follow these steps:
-
Build the Go executable:
$ go build -o main . -
Start the server:
$ ./main
Alternatively just run the main file without building
$ go run main.go
Make sure the docker daemon is running.
To run the application using Docker, follow these steps:
-
Build the Docker image:
$ docker build -t ingrid-go-image . -
Run the Image in a Docker container:
$ docker run -d --rm --name ingrid-go-container -p 8080:8080 ingrid-go-image
-
Check the logs
$ docker logs -f ingrid-go-container 2024/05/23 08:02:09 Starting server on :8080 Building REST API's
Ensure you have Google Cloud SDK installed and authenticated and pushed your docker image to GCP container registry before, then proceed with:
-
Tag and push your Docker image to Google Container Registry:
$ docker tag ingrid-go-image gcr.io/your-project-id/ingrid-go-image:v1 $ docker push gcr.io/your-project-id/ingrid-go-image:v1
-
Deploy the image to Cloud Run:
$ gcloud run deploy ingrid-go-service --image gcr.io/your-project-id/ingrid-go-image:v1 --platform managed
You can test the API using any HTTP client or tool like cURL. Here's an example cURL command to retrieve route information:
curl 'http://localhost:8080/routes?src=13.388860,52.517037&dst=13.397634,52.529407&dst=13.428555,52.523219'
{"source":"13.388860,52.517037","routes":[{"destination":"13.397634,52.529407","distance":1886.8,"duration":260.3},{"destination":"13.428555,52.523219","distance":3804.2,"duration":389.3}]}You can run the unit tests with ok test command.
$ go test ./tests
ok home/tests 0.155sEndpoint: /routes
Method: GET
Parameters:
src: Source point coordinates (latitude,longitude)
dst: Array of destination points coordinates (latitude,longitude)
Example Request:
GET /routes?src=13.388860,52.517037&dst=13.397634,52.529407&dst=13.428555,52.523219 HTTP/1.1
Host: localhost:8080Example Response:
{
"source": "13.388860,52.517037",
"routes": [
{
"destination": "13.397634,52.529407",
"distance": 1886.8,
"duration": 260.3
},
{
"destination": "13.428555,52.523219",
"distance": 2221.6,
"duration": 322.5
}
]
}