Originally constructed for the March 2017 presentation at the ATX Golang meetup.
Sometimes the best way to get a new language into your organization is with a port of something you've already got.
I've used this stack and structure a few times for microservices. I find it easy to deal with and flexible enough to carry forward some of the (sadly necessary) warts that might exist in legacy projects.
- Boot
- Not a whole lot, but not nothin
- Settle down, this is is just here to demo a structure
- Use a Makefile to make common tasks easy
- Use Make to install non-app deps like Ginkgo, Goose, Glide
- Use
jq
for grabbing config info in Make tasks - Config lives in
$HOME/.<APP_NAME>
- DB info lives in
config.json
in the config dir - Glide is the package manager
- Gorilla Mux - URL routing/multiplexing
- Logrus - configurable logging
- Negroni - middleware management
- Viper - unified configuration
- Gorm - ORM-style DB access
- Ginkgo - BDD-style testing (just for DB unit tests)
- Glide - dependency management
Here's how I structure into subpackages and directories:
-
controllers
app.go
- routes and the attendant middleware wire-upgithub_user_controller.go
- an example of dividing up thecontrollers
package into handlers corresponding to groups of routes
-
db
db.go
- interface and concrete type declarations and connection establishmentgithub_user.go
- an example of dividing up thedb
package into type-specific files
-
middleware
request_id.go
- an example of using middleware to tag incoming requests with a unique stringlogging.go
- an example of using Logrus-powered middleware as a replacement forlog
-
_testing
main_suite.go
- setup for a Ginkgo test suitegithub_user_test.go
- an example of using Ginkgo for database unit testing of concrete-type DB methods