Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { scope, ScopeEnum, provide, init, plugin } from 'midway';
const dataId = 'acm.test.1';
const group = 'DEFAULT_GROUP';
@provide()
@scope(ScopeEnum.Singleton)
export class ACMService {
acmText;
@plugin()
acm;
@init()
async init() {
// 放在这里是因为设置成了单例,全局只会有一份实例
const str = `example_test_${Math.random()}_${Date.now()}`;
await this.acm.publishSingle(dataId, group, str);
this.acm.subscribe({
dataId,
group
}, (data) => {
console.log('get data from acm', data);
this.acmText = data;
});
this.acmText = await this.acm.getConfig(dataId, group);
}
const NacosNamingClient = require('nacos').NacosNamingClient;
@provide()
@scope(ScopeEnum.Singleton)
export class NamingService {
nacosClient;
@config('nacosNaming')
nacosConfig;
@logger()
logger;
@init()
async init() {
this.nacosClient = new NacosNamingClient({
...this.nacosConfig,
logger: this.logger,
namespace: 'public',
});
await this.nacosClient.ready();
// 这里的服务名请从 nacos 服务上找
this.nacosClient.subscribe('nacos.test.3', hosts => {
console.log('-----------naming result start --------------');
console.log(hosts);
console.log('-----------naming result end--------------');
});
}
import { scope, ScopeEnum, provide, init, plugin } from 'midway';
const dataId = 'acm.test.1';
const group = 'DEFAULT_GROUP';
@provide()
@scope(ScopeEnum.Singleton)
export class ACMService {
acmText;
@plugin()
acm;
@init()
async init() {
// 放在这里是因为设置成了单例,全局只会有一份实例
const str = `example_test_${Math.random()}_${Date.now()}`;
await this.acm.publishSingle(dataId, group, str);
this.acm.subscribe({
dataId,
group
import { provide, init, config, scope, ScopeEnum, logger } from 'midway';
const NacosNamingClient = require('nacos').NacosNamingClient;
@provide()
@scope(ScopeEnum.Singleton)
export class NamingService {
nacosClient;
@config('nacosNaming')
nacosConfig;
@logger()
logger;
@init()
async init() {
this.nacosClient = new NacosNamingClient({
...this.nacosConfig,
logger: this.logger,
namespace: 'public',
});
await this.nacosClient.ready();
// 这里的服务名请从 nacos 服务上找
this.nacosClient.subscribe('nacos.test.3', hosts => {
import { controller, get, provide, inject, Context } from 'midway'
import { Config } from 'ykfe-utils'
import renderToStream from 'ykfe-utils/lib/renderToStream'
import { IApiService } from '../../interface'
const ssrConfig: Config = require('../../../config/config.ssr')
@provide()
@controller('/')
export class Page {
@inject()
ctx: Context
@inject('ApiService')
service: IApiService
async index () {
try {
// Page为webpack打包的chunkName,项目默认的entry为Page
this.ctx.type = 'text/html'
this.ctx.status = 200
this.ctx.apiService = this.service.index // 将service挂载到上下文对象
const config = Object.assign(this.ctx.app.config, ssrConfig)
import { controller, provide, inject, Context, get } from 'midway'
import { IApiService } from '../../interface'
@provide()
@controller('/api')
export class Api {
@inject()
ctx: Context
@inject('ApiService')
service: IApiService
@get('/getIndexData')
async index () {
try {
// Page为webpack打包的chunkName,项目默认的entry为Page
this.ctx.type = 'text/json'
this.ctx.status = 200
this.ctx.body = await this.service.index()
} catch (error) {
this.ctx.logger.error(`Page Controller renderToStream Error`, error)
}
}
}
import { controller, get, inject } from 'midway';
import { IUserAbstract, IUserResult } from '../../lib/interfaces/user.abstract';
type nextDefinition = () => void;
@controller('/user/')
export class UserController {
@inject('userService')
service: IUserAbstract;
@get('/:id')
async getUser(ctx: any, next: nextDefinition): Promise {
const id: number = ctx.request.params('id');
const user: IUserResult = await this.service.getUser({id});
ctx.body = {success: true, message: 'OK', data: user};
// do not response again
await next();
}
}
import { controller, get, provide, inject, Context } from 'midway'
import { Config } from 'ykfe-utils'
import renderToStream from 'ykfe-utils/lib/renderToStream'
import { IApiService } from '../../interface'
const ssrConfig: Config = require('../../../config/config.ssr')
@provide()
@controller('/')
export class Page {
@inject()
ctx: Context
@inject('ApiService')
service: IApiService
async index () {
try {
// Page为webpack打包的chunkName,项目默认的entry为Page
this.ctx.type = 'text/html'
this.ctx.status = 200
this.ctx.apiService = this.service.index // 将service挂载到上下文对象
const config = Object.assign(this.ctx.app.config, ssrConfig)
const stream = await renderToStream(this.ctx, config)
this.ctx.res.write('')
this.ctx.body = stream
} catch (error) {
this.ctx.logger.error(`Page Controller renderToStream Error`, error)
}
}
import { controller, provide, inject, Context, get } from 'midway'
import { IApiService } from '../../interface'
@provide()
@controller('/api')
export class Api {
@inject()
ctx: Context
@inject('ApiService')
service: IApiService
@get('/getIndexData')
async index () {
try {
// Page为webpack打包的chunkName,项目默认的entry为Page
this.ctx.type = 'text/json'
this.ctx.status = 200
this.ctx.body = await this.service.index()
} catch (error) {
this.ctx.logger.error(`Page Controller renderToStream Error`, error)
}
}
}
import { controller, get, inject } from 'midway';
import { IUserAbstract, IUserResult } from '../../lib/interfaces/user.abstract';
type nextDefinition = () => void;
@controller('/user/')
export class UserController {
@inject('userService')
service: IUserAbstract;
@get('/:id')
async getUser(ctx: any, next: nextDefinition): Promise {
const id: number = ctx.request.params('id');
const user: IUserResult = await this.service.getUser({id});
ctx.body = {success: true, message: 'OK', data: user};
// do not response again
await next();
}
}