Register to microservices system
1. Initilization
Clone PMC github repo Template
Replace module name from
pmc-ecm-service-templatetopmc-ecm-service-exampleInstall needed modules
make initInstall dependent packages
make mod
2. Introduce about sample module
api/sample/sample.proto has 3 sample methods:
Hello: A public method where clients can make requests without Authorization header.HelloAuthen: A public method where clients can make requests must include an Authorization header.HelloAuthenExternalService: A public method where services can make requests must include an Authorization header.internal/data/sample.go is a Sample repository to handle request to database from biz layer
internal/biz/sample.go is a Sample business module for composing business logics.
internal/service/sample.go is a Sample service which implements API definition
3. Protobuf Message Description (Important)
In
HelloAuthenRequestproto message, we haveauthorizationfield:message HelloAuthenRequest {
int64 sample_id = 1 [json_name = "sample_id", (validate.rules).int64 = { gt: 0 }];
// @gotags: module_id:"100" method_id:"1"
string authorization = 100 [(validate.rules).string = { len: 0 }];
}module_id: is anidentitycolumn of tablepermissionson Authorization Servicemethod_id: is apermission_indexcolumn of tablepermission_detailson Authorization ServiceBefore start develop an authorize module, we need request to Authorization Service get a
module_id. After that, we replace currentmodule_idvalue from "100" to "[NEW_ID]" such as "101" andmethod_idvalue from "1" to "[PERMISSION_INDEX]" what we define in each authorization field.Note:
method_idmust be from 1 to 63, increase 1 measure when we define a new function.If
module_idis empty, our method will ignore check authorize althought we was definemethod_id.
Curl to create a Permission:
curl --location 'https://pmc-ecm-authorization-api.dev.pharmacity.io/api/authorization/permission' \
--header 'Accept-Language: vi' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer [ACCESS_TOKEN]' \
--data '{
"name": "New Module Name",
"route": "/api/example",
"note": "",
"service_id": "[SERVICE_ID]",
"is_active": true,
"details": [
{
"permission_index": 0,
"name": "ALL",
"method": "",
"route": ""
},
{
"permission_index": 1,
"name": "Method 1",
"method": "GET",
"route": "/method-1"
},
{
"permission_index": 2,
"name": "Method 2",
"method": "GET",
"route": "/method-2"
},
{
"permission_index": 3,
"name": "Method 3",
"method": "PUT",
"route": "/method-3"
},
{
"permission_index": 4,
"name": "Method 4",
"method": "PUT",
"route": "/method-4"
}
]
}'
3. Start develop a module
Export GOPATH to VSCode/IDE on Linux/MacOS
export GOPATH="$HOME/go"
PATH="$GOPATH/bin:$PATH"General all internal rules
make allRun
kratos run