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/ego
for view renderingjulienschmidt/httprouter
for routing
jgroeneveld/trial/assert
small 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/service
layer. - 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.
app
application level logiccfg
configurationcmd
binarieslib
reusable library componentsscripts
containsgo run
able scriptsweb
web handlers and web-related logic (e.g. views)
apperrors
see Errorsdb
database persistence layer and connection managementdb/migrations
see Migrationsmodel
provides data structures that are managed bydb
.service
service layer containing use cases for the application logic.
config.go
contains the definition and loading of the config from file, env and defaultsdefaults.go
contains the default configuration for the different environments.
gtmigrate
runs the migrationsgtserver
starts the webserver
.......
ego
contains a wrapped version ofbenbjohnson/ego
so that every developer uses the same versiongoassets
a script to bundle assets into the binary
api
related handlers, serializers etc.frontend
related handlers, views, assets etc.webtest
global tests for the web layer like end to end testsrouter.go
main entry point for the web layer
lib/errors
is used to wrap all unknown error sources to add stacktrace information.app/apperr
provides application level errors (apperr.Validation
,apperr.RecordNotFound
)lib/web/httperr
converts 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.