Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
MoleculerJS 어댑터로 API Gateway 함께와 아래의 피어 모듈을 활용 할 수 있습니다.
moleculer-iam - OIDC 및 Identity Provider를 제공하는 IAM 모듈, 인증 컨텍스트에 연동 가능
moleculer-file - 파일 업로드/다운로드/관리 모듈 (GCP Storage bucket backend / File System backend 지원)
moleculer-i18n - 국제화 데이터베이스 관리 및 조회 모듈 (MySQL/Maria DBMS 필요)
moleculer-console - 확장 가능한 Admin Console WebApp (React.js)
API document component 기본 제공 (moleculer-api 호환)
IAM component 기본 제공 (moleculer-iam 호환)
File management component 기본 제공 (moleculer-file 호환)
Translation component 기본 제공 (moleculer-i18n 호환)
Integration Process
병합 규칙은 다음과 같습니다.
discover
discover
발생 즉시 병합 요청이 생성됩니다.
프로토콜 플러그인을 따라 스키마 포맷에 대한 검증을 거칩니다.
포맷 에러는 병합 요청 메세지 리스트에 포함됩니다.
접근 제어를 우회하는 엔드포인트에 대한 경고가 병합 요청 메세지 리스트에 포합됩니다.
검증 성공시 병합 요청이 큐에 삽입되면서 특정 시간(2초를 기본값) 동안 debounce 후 순차적으로 처리됩니다.
큐에서 특정 서비스에 대한 병합 요청이 다수인 경우 마지막 요소만 유효합니다.
프로토콜 플러그인에서 integrationDependencyResolver
가 구현된 경우 (eg. GraphQL) 큐 안에서 처리 순서가 조정 될 수 있습니다.
검증 실패시 report
단계로 건너 뜁니다.
hash
서비스 API 스키마에서 branch
, description
, deprecated
등의 메타 정보를 제외한 스키마 객체 전체를 MD5 해싱하여 고유한 버전 해시를 생성합니다.
게이트웨이에 버전이 존재하지 않는 경우에는 스키마를 업데이트하기로 합니다.
서비스 노드의 연결이 끊긴 경우는 연관된 스키마의 노드풀에서 노드를 삭제합니다.
이 때 연관된 스키마들에서 노드풀이 빈 스키마들을 제거하기로 합니다.
update
master
브랜치의 스키마는 경우에는 모든 브랜치에 병합이 시도됩니다.
이 때 master
외의 브랜치에서 스키마의 서비스명이 중복되는 경우 병합하지 않습니다. 기존 스키마의 우선 순위가 높습니다.
이외의 브랜치에 병합하는 경우에는 주어진 브랜치에만 병합이 시도됩니다.
프로토콜 플러그인을 따라 각 프로토콜별 핸들러를 생성합니다.
병합에 성공하면 생성된 Gateway API 스키마 버전에 latest
태그 및 8 글자의 숏 해시(eg. abcdefgh
)가 태그로 부착됩니다.
옵션이 활성화된 경우 브랜치별 API Catalog를 업데이트합니다.
옵션이 활성화된 경우 브랜치별 Service Catalog를 업데이트합니다.
report
병합 요청의 메세지 리스트를 기반으로 디버그 메세지를 생성합니다.
병합 요청의 출처 노드로 디버그 메세지를 report
합니다.
Branch Strategy Diagram
브랜치간 병합 전략을 표로 도식하면 다음과 같습니다.
표에서 a@v1
는 a
서비스 스키마 중 v1
버전을 의미합니다.
이벤트/브랜치
master
dongwook
비고
initial schema
(empty)
N/A
a
added to master
a@v1
N/A
a
updated to master
a@v2
N/A
b
added to dongwook
a@v2
a@v2
b@v1
새로운 b
서비스를 dongwook
브랜치로 분기해서 작업; 충돌하지 않는 스키마들은 복제되지 않고 참조됩니다.
a
updated to master
a@v3
a@v3
b@v1
master
브랜치의 업데이트는 모든 브랜치로 전파됩니다.
a
updated to dongwook
a@v3
a@v4
b@v1
master
이외의 브랜치의 업데이트는 자기 브랜치로만 전파됩니다.
a
updated to master
a@v3-2
a@v4
b@v1
충돌하는 경우 자기 브랜치 스키마의 우선 순위가 높습니다.
c
added to master
a@v3-2
c@v1
a@v5
b@v1
c@v1
master
브랜치의 업데이트는 모든 브랜치로 전파됩니다.
b
added to master
a@v3-2
b@v1
c@v1
a@v5
b@v1
c@v1
개발된 b
서비스를 dongwook
브랜치에서 master
브랜치로 변경
a
updated to master
a@v5
b@v1
c@v1
a@v5
b@v1
c@v1
수정된 a
서비스를 dongwook
브랜치에서 master
브랜치로 변경
dongwook
branch removed
a@v5
b@v1
c@v1
N/A
Routing Rule
Gateway 웹 서버는 [/~BRANCH[@TAG]]/<ENDPOINT>
의 규칙대로 API 엔드포인트를 라우트합니다. 첫번째 경로 조각을 브랜치(master
를 기본값으로)로 두번째 경로 조각을 태그(latest
를 기본값으로)로 이하 경로를 API 엔드포인트로 해석합니다.
라우트 예시
간략
브랜치 포함
태그 포함
GET /players/1
GET /~master/players/1
GET /~master@latest/players/1
-
-
GET /~master@h4g3f2e1/players/1
-
GET /~dongwook/players/1
GET /~dongwook@latest/players/1
-
-
GET /~dongwook@a4b3c2d1/players/1
POST /graphql
POST /~master/graphql
GET /~master@latest/graphql
-
POST /~dongwook/graphql
GET /~dongwook@latest/graphql
-
GET /~ws-dev/chat
GET /~ws-dev@latest/chat
테이블의 각 행은 동일한 버전의 핸들러로 연결됩니다.
브랜치 이름 규칙
영문 소문자 및 숫자, -
, _
만 허용됩니다.
기존 스키마 엔드포인트나 플러그인의 base 경로와 중복될 수 없습니다.
태그 이름 규칙
영문 소문자 및 숫자로만 구성됩니다.
GraphQL API 맵핑에는 call
, publish
, subscribe
, map
커넥터를 이용 할 수 있습니다.
TypeDefs
GraphQL: {
typeDefs: `
"""Soccer Player"""
type Player implements Node {
id: ID!
email: String!
name: String!
photoURL: String
position: String
"""A team player belongs to"""
team: Team
}
extend type Query {
"""Current Player"""
viewer: Player
player(id: ID!): Player
}
extend type Subscription {
playerMessage: String!
playerUpdated: Player
}
`,
GraphQL 프로토콜에서 typeDefs
속성에 서비스에 필요한 정의(scalar
를 제외한 타입, 인터페이스, 열거형 등 모든 형태)을 추가하거나 기존 타입(API Gateway에서 제공하는 기본 타입과 분산 서비스에서 제공한 타입들)을 확장 할 수 있습니다.
Resolvers
resolvers: {
이하 리졸버에 각 타입들의 필드를 call
, publish
, subscribe
, map
커넥터에 맵핑합니다.
Player: {
리졸버가 할당되지 않은 필드들은 source 객체에서 동일한 이름의 속성으로부터 주입됩니다.
Call
team: {
call: {
action: "team.get",
params: {
id: "@source.teamId",
},
},
},
GraphQL API의 Query
및 Mutation
타입의 필드들에는 publish
및 call
또는 map
커넥터를 이용 할 수 있습니다. params
맵핑에는 @source
, @args
, @context
, @info
를 이용 할 수 있습니다.
Map
position: `({ source, args, context, info }) => source.position.toUpperCase()`,
`
GraphQL 프로토콜에서 map
커넥터 (Inline JavaScript Function String)는 간략하게 field: { map: <FN_STRING> }
대신에 field: <FN_STRING>
방식으로 작성 할 수 있습니다.
// be noted that special field __isTypeOf got only three arguments
__isTypeOf: `({ source, context, info }) => return source.someSpecialFieldForThisType != null`,
// be noted that special field __resolveType got only three arguments
__resolveType: `
({ source, context, info }) => {
if (source.someSpecialFieldForThisType != null) {
return "TypeA";
} else {
return "TypeB";
}
}
`,
},
위처럼 Union, Interface 구현 타입을 해석하기 위한 특수 필드에도 Inline JavaScript Function String를 사용합니다.
Batched Call (DataLoader)
Query: {
viewer: {
call: {
action: "player.get",
params: {
id: "@context.user.player.id[]",
},
},
},
player: {
call: {
action: "player.get",
params: {
id: "@args.id[]",
},
},
},
},
위처럼 인증 정보를 포함한 @context
나 GraphQL 필드 인자인 @args
를 활용해 동일한 액션을 서로 다른 방식으로 맵핑 할 수 있습니다.
또한 call
메소드는 GraphQL 요청에서 발생하기 쉬운 N+1 쿼리를 방지하기 위해 요청을 배치로 처리 할 수 있도록 설계되었습니다. (ref. Dataloader)
한 컨텍스트에서 여러번 호출되는 액션에 배칭을 지원하면 응답 속도를 획기적으로 높힐 수 있습니다. 배칭을 활성화하기 위해서는 call
커넥터의 batchedParams
필드에 배치 처리가 가능한 필드의 이름을 작성하고, 연결된 서비스 액션이 배열로 들어오는 인자 묶음을 처리 할 수 있도록 합니다.
query {
viewer {
id
email
}
one: player(id: 1) {
id
email
}
two: player(id: 2) {
id
email
}
three: player(id: 3) {
id
email
}
}
위와 같은 GraphQL 요청은 player.get
액션을 { id: [context.user.player.id, 1, 2, 3], ...(other common params) }
페이로드와 함께 한번만 호출하게 됩니다. 연결된 액션이 [{ ... }, { ... }, { ... }, { ... }]
묶음으로 응답을 주면 각 필드에 해당하는 응답이 할당됩니다.
만약 id: 3
인 플레이어가 없는 경우 배치 요청을 처리하는 과정에서 에러를 발생시켜 제어 흐름을 멈추는 대신에, 에러를 발생키지않고 배치 응답에 포함시키고 나머지 제어 흐름을 마무리합니다. [{ ... }, { ... }, { ... }, { message: "...", isBatchError: true, ... }]
처럼 isBatchError: true
속성을 갖는 에러 객체를 응답에 포함합니다.
Subscribe
Subscription: {
playerMessage: {
subscribe: {
events: ["player.message"],
},
},
GraphQL API의 Subscription
타입의 필드에서는 subscribe
커넥터를 사용 할 수 있습니다. params
맵핑에는 마찬가지로 @source
, @args
, @context
, @info
를 이용 할 수 있습니다. @source
에 이벤트 객체가 맵핑됩니다.
@source
객체는 { event, payload }
로 구성됩니다. Broker에 따라 기타 속성이 추가 될 수 있습니다.
playerMessage: {
subscribe: {
events: ["player.message"],
map: `({ source, args, context, info }) => source.payload.message`,
},
},
},
},
},
subscribe
커넥터에서는 위처럼 수신된 이벤트 페이로드를 다시 map
커넥터로 변환 할 수 있습니다. subscribe
커넥터 안에서 map
커넥터가 사용되지 않는 경우 이벤트 객체 전체(source
)를 반환합니다.
yarn dev [example=simple]
- Start development (nodemon with ts-node)
yarn build
- Uses typescript to transpile service to javascript
yarn lint
- Run TSLint
yarn test
- Run tests & generate coverage report
yarn test --watch
- Watch and run tests
Please send pull requests improving the usage and fixing bugs, improving documentation and providing better examples, or providing some testing, because these things are important.
A dynamic API Gateway which updates REST endpoints, GraphQL schema, WebSocket handlers and access control policies by integrating metadata of discovered remote services.
moleculer-api는 MSA 환경에서 마이크로 서비스들의 API 스키마 조각을 수집하고, 무중단으로 통합 API를 업데이트하여 제공하는 웹 서버 컴포넌트입니다.
서비스 API 스키마는 분산 서비스 프로시저의 호출, 또는 중앙 메시징 서비스에 대한 이벤트 발행 및 구독을 응용 프로토콜(REST, GraphQL, WebSocket 등)에 맵핑합니다. 서비스 API 스키마는 JSON 포맷으로 구성되어있으며, 응용 프로토콜 별 API 맵핑과 그에 대한 접근 제어로 구성되어 있습니다.
{
branch: "master",
policy: {},
protocol: {
REST: {
basePath: "/storage",
routes: [
{
path: "/",
method: "GET",
call: {
action: "storage.get",
params: {
offset: "@query.offset:number",
limit: "@query.limit:number",
},
},
},
{
path: "/upload",
method: "POST",
call: {
action: "storage.create",
params: {
file: "@body.file",
meta: {
tags: {
identityId: "@context.auth.identity.sub",
},
allowedContentTypes: ["text/*", "image/*", "application/pdf"],
private: false,
},
},
},
},
{
path: "/upload-stream",
method: "POST",
description: "not a production purpose, need a wrapper action to make this safe",
call: {
action: "storage.createWithStream",
params: "@body.file",
implicitParams: false,
},
},
{
path: "/download/:id",
method: "GET",
call: {
action: "storage.getURL",
params: {
id: "@path.id",
expiryHours: "@query.expiryHours:number",
prompt: "@query.prompt:boolean",
promptAs: "@query.promptAs:string",
},
map: `({ response }) => ({
$status: response ? 303 : 404,
$headers: response ? { "Location": response } : undefined,
})`,
},
},
{
path: "/:id",
method: "GET",
call: {
action: "storage.find",
params: {
id: "@path.id",
},
map: `({ response }) => ({
$status: response ? 200 : 404,
$body: response,
})`,
},
},
{
path: "/:id",
method: "PUT",
call: {
action: "storage.update",
params: {
id: "@path.id",
// id, name, tags, private, contentType
},
},
},
{
path: "/:id",
method: "DELETE",
call: {
action: "storage.delete",
params: {
id: "@path.id",
},
},
},
],
},
},
}
{
branch: "master",
policy: {},
protocol: {
GraphQL: {
typeDefs: `
extend type Query {
storage: StorageQuery!
}
type StorageQuery {
files(offset: Int = 0, limit: Int = 10): FileList!
file(id: ID!): File
}
type File {
id: ID!
name: String!
contentType: String!
tags: JSON!
private: Boolean!
byteSize: Int!
size: String!
url(expiryHours: Int = 2, prompt: Boolean = false, promptAs: String): String!
updatedAt: DateTime!
createdAt: DateTime!
}
type FileList {
offset: Int!
limit: Int!
# it is not exact value
total: Int!
entries: [File!]!
hasNext: Boolean!
hasPrev: Boolean!
}
extend type Mutation {
storage: StorageMutation!
}
type StorageMutation {
upload(file: Upload!): File!
update(id: ID!, name: String, tags: JSON, private: Boolean): File!
delete(id: ID!): Boolean!
}
`,
resolvers: {
Query: {
storage: `() => ({})`,
},
Mutation: {
storage: `() => ({})`,
},
StorageMutation: {
upload: {
call: {
action: "storage.create",
params: {
file: "@args.file",
meta: {
allowedContentTypes: [],
private: false,
},
},
},
},
update: {
call: {
action: "storage.update",
params: {},
},
},
delete: {
call: {
action: "storage.delete",
params: {},
}
},
},
StorageQuery: {
file: {
call: {
action: "storage.find",
params: {
id: "@args.id[]",
},
},
},
files: {
call: {
action: "storage.get",
params: {
offset: "@args.offset",
limit: "@args.limit",
},
},
},
},
File: {
url: {
call: {
action: "storage.getURL",
params: {
id: "@source.id[]",
expiryHours: "@args.expiryHours",
prompt: "@args.prompt",
promptAs: "@args.promptAs",
},
},
},
size: (({ source }: any) => {
const { byteSize = 0 } = source;
const boundary = 1000;
if (byteSize < boundary) {
return `${byteSize} B`;
}
let div = boundary;
let exp = 0;
for (let n = byteSize / boundary; n >= boundary; n /= boundary) {
div *= boundary;
exp++;
}
const size = byteSize / div;
return `${isNaN(size) ? "-" : size.toLocaleString()} ${"KMGTPE"[exp]}B`;
}).toString(),
},
FileList: {
hasPrev: `({ source }) => source.offset > 0`,
}
},
},
},
}
{
branch: "master",
policy: {
call: [
{
actions: ["user.update"],
description: "A user can update the user profile which is belongs to own account.",
scope: ["user.write"],
filter: (({ context, params }) => {
console.log(context);
if (params.id) {
return context.auth.identity.sub === params.id;
}
return false;
}).toString(),
},
],
},
protocol: {
GraphQL: {
typeDefs: `
extend type Query {
viewer: User
user(id: Int, identityId: String, username: String, where: JSON): User
}
extend type Mutation {
createUser(input: UserInput!): User!
updateUser(input: UserInput!): User!
}
input SportsUserInput {
id: Int
username: String
birthdate: Date
gender: Gender
name: String
pictureFileId: String
}
type User {
id: Int!
username: String!
name: String!
birthdate: Date
gender: Gender
pictureFile: File
}
`,
resolvers: {
User: {
pictureFile: {
call: {
if: "({ source }) => !!source.pictureFileId",
action: "storage.find",
params: {
id: "@source.pictureFileId[]",
},
},
},
},
Mutation: {
createUser: {
call: {
action: "user.create",
params: "@args.input",
implicitParams: false,
},
},
updateUser: {
call: {
action: "user.update",
params: "@args.input",
implicitParams: false,
},
},
},
},
},
},
}
{
branch: "master",
policy: {},
protocol: {
REST: {
description: "update user's FCM registration token",
basePath: "/notification",
routes: [
{
method: "PUT",
path: "/update-token",
call: {
action: "notification.updateToken",
params: {
identityId: "@context.auth.identity.sub",
token: "@body.token",
},
},
},
],
},
},
}
{
branch: "master",
policy: {},
protocol: {
WebSocket: {
basePath: "/chat",
description: "...",
routes: [
/* bidirectional streaming chat */
{
path: "/message-stream/:roomId",
call: {
action: "chat.message.stream",
params: {
roomId: "@path.roomId",
},
},
},
/* pub/sub chat */
{
path: "/message-pubsub/:roomId",
subscribe: {
events: `({ path }) => ["chat.message." + path.roomId]`,
},
publish: {
event: `({ path }) => "chat.message." + path.roomId`,
params: "@message",
},
},
/* pub/sub video */
{
path: "/video-pubsub",
subscribe: {
events: ["chat.video"],
},
publish: {
event: "chat.video",
params: {
id: "@context.id",
username: "@query.username",
data: "@message",
},
filter: `({ params }) => params.id && params.username && params.data`,
},
},
/* streaming video */
{
path: "/video-stream/:type",
call: {
action: "chat.video.stream",
params: {
id: "@context.id",
type: "@path.type",
},
},
},
],
},
},
}
Gateway는 특정 서비스 API 스키마의 추가, 제거 및 업데이트시 기존 통합 API 스키마에 병합을 시도하고, 성공시 무중단으로 라우터를 업데이트하며 그 결과를 원격 서비스에 다시 보고합니다.
분산 서비스의 API 스키마를 수집하고 병합하여 API를 실시간으로 업데이트
Polyglot 하거나 서로 다른 서비스 브로커에 기반한 서비스들의 API 스키마를 수집하고 병합 할 수 있음
개발 편의 및 충돌 방지를 위한 API 브랜칭 및 버저닝 기능
상태 검사 및 API 문서 생성 (WIP)
미들웨어 방식의 요청 흐름 제어
Error
Logging
Body Parser
Helmet
CORS
Serve Static File
HTTP/HTTPS/HTTP2
(확장 가능)
미들웨어 방식의 컨텍스트 생성 제어
Authn/Authz
Locale
Correlation ID
IP Address
User-Agent
Request
(확장 가능)
응용 프로토콜 플러그인
REST
GraphQL
WebSocket
(확장 가능)
접근 제어 정책 플러그인
OAuth2 scope 기반 접근 제어
JavaScript FBAC; Function Based Access Control 기반 접근 제어
(확장 가능)
The project is available under the MIT license.
Configure and run the gateway server.
Add moleculer-api
package from npm.
And write entry script like above. For now, let's skip setting detailed options.
With default options configuration, A gateway will run on localhost 8080 port with HTTP protocol. And basic placeholder scheme for GraphQL plugin, playground for GraphQL and a default status check endpoint will be set.
An endpoint to show the status each API integrations. It will show the integration status from oldest version to latest version with detailed integrations belong to each versions by each branches (only master branch initially).
A playground endpoint which is set from GraphQL Protocol Plugin.
API Gateway constructor options.
APIGatewayOptions type is a kind of container for all the subordinate components' options.
Options for the gateway itself rather inner components.
Service Broker options are consist of common properties and delegator specific properties. The common properties show below.
Specific options for the Service Broker Delegator. Can choose only one among supported delegators. Currently delegator is supported.
delegator can be configured with moleculer broker own options and few extra options like below.
Service Registry options are consist of own options for the registry itself and Protocol, Policy, this two type of Plugin constructor options.
A ProtocolPlugin handles mapping Public API to calling internal services' procedure, publishing and subscribing event messages.
WIP
A PolicyPlugin handles access controls (authorization) while calling internal services' procedure, publishing and subscribing event messages.