harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-9749harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-9749Implement a Go application that acts as a simple API server. The application should listen for HTTP requests on a specified port and respond with JSON-formatted data. This application will serve as the entry point for building binaries for multiple platforms. The API should include at least one endpoint, such as /health, which returns a status message indicating that the server is running.
PORT=[port_number]
When the /health endpoint is accessed via an HTTP GET request, the application should respond with:
{
"status": "healthy"
}
The binary executable files generated by the build process should be located in the bin/ directory, named according to the following pattern:
cellosaurus-api-linux-amd64cellosaurus-api-windows-amd64.execellosaurus-api-darwin-amd64Each binary should be successfully built without errors.
/app/cmd/api/main.go
bin directory will be created during the testing process, so only focus on creating the source files and ensuring successful execution.net/http package for handling HTTP requests and responses.By implementing the above functionality, the provided test script will pass successfully, ensuring that the application can be built and run across different platforms.
The following test script will be used to verify your solution:
#!/bin/bash
ROOT="$(dirname "$0")"
cd "$ROOT/.."
rm -rf bin
package=$1
if [[ -z "$package" ]]; then
package="cmd/api/main.go"
fi
platforms=("linux/amd64" "linux/386" "windows/amd64" "windows/386" "darwin/amd64")
for platform in "${platforms[@]}"
do
platform_split=(${platform//\// })
GOOS=${platform_split[0]}
GOARCH=${platform_split[1]}
output_name='bin/cellosaurus-api-'$GOOS'-'$GOARCH
if [ $GOOS = "windows" ]; then
output_name+='.exe'
fi
echo "== Building $output_name =="
env GOOS=$GOOS GOARCH=$GOARCH go build -o $output_name $package
if [ $? -ne 0 ]; then
echo 'An error has occurred! Aborting the script execution...'
exit 1
fi
done