In this example, we create a web service that allows you to add and retrieve your list of friends.
- Golang 1.20+
- GNU Makefile
- Docker for Mac
$GOBIN
&$GOROOT
are correctly setGO111MODULE
is "on"
In this example, the source code contains the fully finished service. This step by step will walk through the steps involved in writing this service.
See Instructions in skeleton-service
At this point, the controllers are implemented with no-op implementation.
In pkg/init/pakcage.go
initialize the data
and cockroach
package.
func Use() {
// ...
// data related
data.Use()
cockroach.Use()
// ...
}
In application.yml
configure the connection properties to the database.
data:
logging:
level: warn
slow-threshold: 5s
cockroach:
host: ${db.cockroach.host:localhost}
port: ${db.cockroach.port:26257}
sslmode: ${db.cockroach.sslmode:disable}
username: ${spring.datasource.username:root}
Password: ${spring.datasource.password:root}
database: skeleton
In pkg/model/friend.go
define the golang struct that maps to the table in the database
In pkg/repository/friend.go
define the CRUD repository for this model, and define the constructor for this repository.
In pkg/repository/package.go
define the Use()
method that would provide the repository instance for injection.
In pkg/init/package.go
initialize the repository package so that it's available for injection.
func Use() {
// ...
repository.Use()
}
Update the controller constructor so that it injects a repository instance.
type ExampleFriendsController struct {
friendRepo *repository.FriendsRepository
}
type exampleFriendsControllerDI struct {
fx.In
FriendsRepo *repository.FriendsRepository // This tells go-lanai that the NewExampleFriendsController constructor requires a FriendRepository instance
}
func NewExampleFriendsController(di exampleFriendsControllerDI) web.Controller {
return &ExampleFriendsController{
friendRepo: di.FriendsRepo, // Sets the controller's friendRepo reference so that it can be used later.
}
}
In each of the controller's method, use the friendRepo
reference to carry out the database operations.
In cmd/skeleton-service-migrate/migrate.go
add the main method for the data migration application.
In pkg/migrate
add the data migration implementation. pkg/migrate/package.go
contains the boilder plate code to setting
up the go-lanai
components needed for running a data migration. pkg/migrate/migration_v1
includes the migration steps.
In this example there is only one step, which is to create the database table like this.
DROP TABLE IF EXISTS "public"."users";
CREATE TABLE friends
(
id UUID NOT NULL DEFAULT gen_random_uuid(),
first_name STRING NOT NULL,
last_name STRING NOT NULL,
CONSTRAINT "primary" PRIMARY KEY (id ASC)
);
This will create the table if it has not been created. Re-running this command will only execute migration steps that has not been executed already.
go run cmd/skeleton-service-migrate/migrate.go
go run cmd/skeleton-service/main.go
Navigate to http://localhost:9898/skeleton/swagger. Use the GET API to retrieve all the items stored in the database. Use the POST API to add more items to the database. You also need to run auth service in order to authenticate the user.