Skip to main content

Register to microservices system

1. Initilization

  • Clone PMC github repo Template

  • Replace module name from pmc-ecm-service-template to pmc-ecm-service-example

  • Install needed modules

    make init
  • Install 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 HelloAuthenRequest proto message, we have authorization field:

      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 an identity column of table permissions on Authorization Service

    method_id : is a permission_index column of table permission_details on Authorization Service

  • Before start develop an authorize module, we need request to Authorization Service get a module_id. After that, we replace current module_id value from "100" to "[NEW_ID]" such as "101" and method_id value from "1" to "[PERMISSION_INDEX]" what we define in each authorization field.

    Note:

    • method_id must be from 1 to 63, increase 1 measure when we define a new function.

    • If module_id is empty, our method will ignore check authorize althought we was define method_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 all
  • Run

    kratos run