Now that we aren't on the frontpage, I can finally be myself and use dumb emojis everywhere!!! (This is kinda outdated, TODO update)
This is a big project. If you dive in head-first without much knowledge you'll probably find your way around, but keep on reading just a little bit and you'll be able to really own this.
-
Server starts
. Router starts receiving HTTP Requests, mapping them to the corresponding Endpoint. -
HTTP Requests
are transformed into our custom Requests, for exampleCreateUserRequest
(with fields like Username or Password). Those requests are validated. -
Our Request is transformed into a
Model
, the key structures of our program. They're our DB Schema and also where we perform operations. AUserModel
, for example, has methods like .Create or .Update, taking theRepository
as parameter to communicate with the DB. -
A
CreateUserResponse
is generated with the operations' results, and finally transformed into anHTTP Response
.
HTTP Request ➡️ CreateUserRequest ➡️ UserModel ➡️ Repository ➡️ Database ➡️ CreateUserResponse ➡️ HTTP Response
-
You start the server
. All of its modules are also initialized and if there are no errors, the server will run. -
A
Router
will be in place listening for any wandering HTTP Request that happens to fall beneath its path. Oh wow! A newHTTP Request
came in, and it's pointed towards the Create User Endpoint. It'll run theMiddleware
and send it on its way. -
The HTTP Request's data is used to create our own custom
CreateUserRequest
(with fields like Username or Password). Validations are executed here. -
If validations pass, that CreateUserRequest will be passed along the way and eventually converted into a
Model
, in this case aUserModel
. -
Models are the key players of our app, they define the DB Schema and also serve as Business Objects, which means that much of the logic of the program will be inside of the methods of this UserModel.
-
Methods like
.Create
or.Update
are called, which take theRepository
as parameter and use it to talk to theDatabase
. Also methods like HashPassword that just modify fields inside of the Model. -
The UserModel is filled with the resulting data, and then transformed into a
CreateUserResponse
. Middleware executes again, and our custom Response is transformed into a beautifulHTTP Response
.
HTTP Request ➡️ Middleware ➡️ CreateUserRequest ➡️ Validations ➡️ UserModel ➡️ Repository ➡️ Database ➡️ CreateUserResponse ➡️ Middleware HTTP ➡️ Response
3 Layers:
- Starts with the Middleware, on
prometheus.go
. - If the endpoint is private, it goes through
token_validation.go
. - Goes then through the Endpoint, on
endpoints.go
- The
handler.go
is called which controls the request's lifecycle. - Our Custom Request is built and validated on
requests_builders.go
andrequests_validations.go
. - The handler then calls the
service_xxx.go
file.
- The Request is transformed into a Model, in
requests_to_models.go
. - The model's methods are called, on
models_actions_xxx.go
. - The repository is sometimes passed as parameter, calling the methods on
repository_xxx.go
.
- The methods on
repository_xxx.go
call the database and return the operation's result.
- The Model is transformed into a Response Model, and the Response is passed back to the handler.
- Any errors are handled and logged on the
http_errors_mapper.go
. - The Response is transformed into an HTTP Request and returned to the client.