Hey everyone! I’m thrilled to share that Phase 2 of my Beekeeping API project is fully complete. This simple RESTful API, built in Go, is now up and running, serving hardcoded senseBox data for beekeeping monitoring. It’s been a fun journey getting it working, pushing it to GitHub, and setting up the foundation for future enhancements. Here’s the full rundown of what I’ve accomplished:
What’s Done in Phase 1?
The goal of Phase 1 was to create a basic API that retrieves senseBox data by ID. Here’s what I’ve built:
- Core API Setup
- Wrote a Go application using gorilla/mux for routing.
- Implemented a GET /api/v1/sensebox/{id} endpoint that returns JSON data (e.g., {“id”: “5eba5fbad46fb8001b799786”, “temperature”: 22.5, “humidity”: 60}) for a hardcoded senseBox ID.
- Organized the code into modular files: main.go, handler.go, sensebox.go, and version.go, all under package main.
- Versioning
- Added a –version flag that prints App Version: v0.0.1 and exits, following Semantic Versioning. You can test it with go run main.go –version.
- Tools
- Used Git for version control, VS Code as my IDE, and Docker for containerization. These tools have made development smooth and deployment portable.
- Containerization
- Created a Dockerfile with a multi-stage build to package the app into a lightweight container. Build and run it with:bash
docker build -t beekeeping-api:latest . docker run -p 8080:8080 beekeeping-api:latest
- The API is accessible at http://localhost:8080.
- Created a Dockerfile with a multi-stage build to package the app into a lightweight container. Build and run it with:bash
- Git and GitHub
- Set up a .gitignore file to exclude binaries (e.g., beekeeping-api) and temporary files, while keeping go.mod and go.sum tracked for reproducibility.
- Pushed the project to my GitHub repository: https://github.com/ref34t/beekeeping-api. Check it out!
- Testing
- Ran the app locally and tested it with curl:bash
curl http://localhost:8080/api/v1/sensebox/5eba5fbad46fb8001b799786
Output: {“id”:”5eba5fbad46fb8001b799786″,”temperature”:22.5,”humidity”:60} - Tested an invalid ID (curl http://localhost:8080/api/v1/sensebox/invalid) and got SenseBox not found as expected.
- Confirmed the Docker container works the same way.
- Ran the app locally and tested it with curl:bash
Challenges Faced
- Undefined Function Error: I hit a snag with undefined: getSenseBoxData because of file setup issues. Turns out, keeping everything in package main in the same directory fixed it—no imports needed!
- Docker Setup: Writing the Dockerfile took some tweaking to get the multi-stage build right, but it’s now efficient and small.
- Git Configuration: Figuring out .gitignore was a learning moment—committing go.mod and go.sum ensures others can build the project exactly as I do.
The Full File Structure
Here’s what’s in the repo:
beekeeping-api/
├── .gitignore # Excludes binaries and temp files
├── main.go # Server setup and routing
├── handler.go # GET endpoint logic
├── sensebox.go # SenseBox data model
├── version.go # Versioning function
├── Dockerfile # Docker configuration
├── go.mod # Module definition
├── go.sum # Dependency checksums
└── README.md # Project docs
Leave a Reply