gotrix is a blueprint for a go web project, solving the most common problems. Its not meant to be used as a library but rather to be copied and used as starting point. This frees from having to fulfill every need at every point in the lib.
A small to middle-sized web application with cli, api and frontend using postgres as db.
benbjohnson/egofor view renderingjulienschmidt/httprouterfor routing
jgroeneveld/trial/assertsmall and clean assertions
-
The access/presentation layer (
cli/web/api/web/frontend)- provides handlers that translate and validate user input into calls for the
app/servicelayer. - translates output to display for the user
- provides handlers that translate and validate user input into calls for the
-
The service layer (
app/service)- provides use cases, validates input and executes any calls to the persistence layer
- or any other data sources
-
The persistence layer (
app/db) provides access to the database in a structured way.
appapplication level logiccfgconfigurationcmdbinarieslibreusable library componentsscriptscontainsgo runable scriptswebweb handlers and web-related logic (e.g. views)
apperrorssee Errorsdbdatabase persistence layer and connection managementdb/migrationssee Migrationsmodelprovides data structures that are managed bydb.serviceservice layer containing use cases for the application logic.
config.gocontains the definition and loading of the config from file, env and defaultsdefaults.gocontains the default configuration for the different environments.
gtmigrateruns the migrationsgtserverstarts the webserver
.......
egocontains a wrapped version ofbenbjohnson/egoso that every developer uses the same versiongoassetsa script to bundle assets into the binary
apirelated handlers, serializers etc.frontendrelated handlers, views, assets etc.webtestglobal tests for the web layer like end to end testsrouter.gomain entry point for the web layer
lib/errorsis used to wrap all unknown error sources to add stacktrace information.app/apperrprovides application level errors (apperr.Validation,apperr.RecordNotFound)lib/web/httperrconverts application level errors into http errors with status codes. They can be rendered by api or html middlewares into error responses.
gtmigrate runs migrations on the configured postgres database. Migrations are defined in app/db/migrations.
The name of the migration file 001_create_expenses.go is just for ordering in the folder.
The first param of Migrations.Add is the ordering in which the migrations are run and, combined with the description, the id in the database. (id, description) must be unique.
To allow for test helpers that are importable from other packages and to
prevent cyclic dependencies, all tests will be contained in special *test packages.
All tests for the db package are contained in db/dbtext for example.
dbtest -> db
dbtest -> fabricate
fabricate -> db
This way fabricate can be used by dbtest which both depend on db.
Furthermore, testing helpers are not directly mixed with the production code.
Sometimes this *test packages need to contain an empty.go file so that go get does not break for this package.