Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 => {
database: string;
username: string;
password: string;
}
export interface IDB {
sequelize: Sequelize;
options: IDBOptions;
}
@scope(ScopeEnum.Singleton)
@async()
@provide('DB')
export default class DB implements IDB {
public sequelize: Sequelize;
@config('sequelize')
public options: IDBOptions;
@init()
public connect() {
this.sequelize = new Sequelize(
this.options.database,
this.options.username,
this.options.password,
{
dialect: this.options.dialect,
host: this.options.host,
port: this.options.port,
timezone: '+08:00',
logging: false,
},
);
import { controller, config, plugin, get, provide, Context, inject } from 'midway';
@provide()
@controller('/')
export class HomeController {
@inject()
ctx: Context;
@plugin()
jwt;
@config('jwt')
jwtConfig;
@get('/token')
async api(ctx: Context) {
const token = this.jwt.sign({ foo: 'bar' }, this.jwtConfig.secret);
ctx.body = token;
}
}
constructor(
@config() private readonly welcomeMsg: string,
) {}