Skip to content

Commit

Permalink
Merge branch 'release/0.4.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
fmvilas committed Nov 20, 2018
2 parents efdfb03 + 8332d50 commit 91eba18
Show file tree
Hide file tree
Showing 19 changed files with 833 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ const registerHelpers = config => new Promise((resolve, reject) => {
*/
const registerPartials = config => new Promise((resolve, reject) => {
const partials_dir = path.resolve(config.templates, PARTIALS_DIRNAME);

if (!fs.existsSync(partials_dir)) return resolve();

const walker = xfs.walk(partials_dir, {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asyncapi-generator",
"version": "0.3.3",
"version": "0.4.0",
"description": "The AsyncAPI generator. It can generate documentation, code, anything!",
"main": "./lib/generator.js",
"bin": {
Expand Down
26 changes: 26 additions & 0 deletions templates/java-spring/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
.gradle
/build/
!gradle/wrapper/gradle-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
47 changes: 47 additions & 0 deletions templates/java-spring/.helpers/handlebars.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = (Handlebars, _) => {

//capitalizeFirstLetter
Handlebars.registerHelper('capitalize', (str) => {
return _.capitalize(str);
});

Handlebars.registerHelper('camelCase', (str) => {
return _.camelCase(str);
});

Handlebars.registerHelper('upperFirst', (str) => {
return _.upperFirst(str);
});

//convert string to java constant format: lightMeasuredPublish -> LIGHT_MEASURED_PUBLISH
Handlebars.registerHelper('javaConst', (str) => {
return _.snakeCase(str).toUpperCase();
});

Handlebars.registerHelper('compare', (lvalue, operator, rvalue, options) => {
if (arguments.length < 4) throw new Error('Handlerbars Helper "compare" needs 3 parameters');

const operators = {
'==': (l,r) => { return l == r; },
'===': (l,r) => { return l === r; },
'!=': (l,r) => { return l != r; },
'<': (l,r) => { return l < r; },
'>': (l,r) => { return l > r; },
'<=': (l,r) => { return l <= r; },
'>=': (l,r) => { return l >= r; },
typeof: (l,r) => { return typeof l === r; }
};

if (!operators[operator]) throw new Error(`Handlerbars Helper 'compare' doesn't know the operator ${operator}`);
const result = operators[operator](lvalue,rvalue);

if (result) return options.fn(this);

return options.inverse(this);
});

// Handlebars.registerHelper('json', function(context) {
// return JSON.stringify(context);
// });

};
128 changes: 128 additions & 0 deletions templates/java-spring/.partials/AmqpConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.asyncapi.infrastructure;

import com.asyncapi.service.MessageHandlerService;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.amqp.dsl.Amqp;
import org.springframework.integration.amqp.outbound.AmqpOutboundEndpoint;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.messaging.MessageChannel;

@Configuration
public class Config {

@Value("${amqp.broker.host}")
private String host;

@Value("${amqp.broker.port}")
private int port;

@Value("${amqp.broker.username}")
private String username;

@Value("${amqp.broker.password}")
private String password;

{{#each asyncapi.topics as |topic key|}}
{{#if topic.subscribe}}
@Value("${amqp.exchange.{{~topic.x-service-name~}}}")
private String {{topic.x-service-name}}Exchange;

{{/if}}
{{/each}}
{{#each asyncapi.topics as |topic key|}}
{{#if topic.subscribe}}
@Value("${amqp.queue.{{~topic.x-service-name~}}}")
private String {{topic.x-service-name}}Queue;

{{/if}}
{{/each}}

@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
connectionFactory.setPort(port);
return connectionFactory;
}

@Bean
public AmqpAdmin amqpAdmin() {
return new RabbitAdmin(connectionFactory());
}

@Bean
public Declarables exchanges() {
return new Declarables(
{{#each asyncapi.topics as |topic key|}}
{{#if topic.publish}}
new TopicExchange({{topic.x-service-name}}Exchange, true, false){{#unless @last}},{{/unless}}
{{/if}}
{{/each}}
);
}

@Bean
public Declarables queues() {
return new Declarables(
{{#each asyncapi.topics as |topic key|}}
{{#if topic.subscribe}}
new Queue({{topic.x-service-name}}Queue, true, false, false){{#unless @last}},{{/unless}}
{{/if}}
{{/each}}
);
}

// consumer

@Autowired
MessageHandlerService messageHandlerService;
{{#each asyncapi.topics as |topic key|}}

{{#if topic.subscribe}}
@Bean
public IntegrationFlow {{camelCase topic.x-service-name}}Flow() {
return IntegrationFlows.from(Amqp.inboundGateway(connectionFactory(), {{topic.x-service-name}}Queue))
.handle(messageHandlerService::handle{{upperFirst topic.x-service-name}})
.get();
}
{{/if}}
{{/each}}

// publisher

@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
}
{{#each asyncapi.topics as |topic key|}}

{{#if topic.publish}}
@Bean
public MessageChannel {{camelCase topic.x-service-name}}OutboundChannel() {
return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel = "{{camelCase topic.x-service-name}}OutboundChannel")
public AmqpOutboundEndpoint {{camelCase topic.x-service-name}}Outbound(AmqpTemplate amqpTemplate) {
AmqpOutboundEndpoint outbound = new AmqpOutboundEndpoint(amqpTemplate);
outbound.setExchangeName({{topic.x-service-name}}Exchange);
outbound.setRoutingKey("#");
return outbound;
}
{{/if}}
{{/each}}
}
106 changes: 106 additions & 0 deletions templates/java-spring/.partials/MqttConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.asyncapi.infrastructure;

import com.asyncapi.service.MessageHandlerService;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.dsl.IntegrationFlows;
import org.springframework.integration.endpoint.MessageProducerSupport;
import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
import org.springframework.integration.mqtt.inbound.MqttPahoMessageDrivenChannelAdapter;
import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
import org.springframework.integration.mqtt.support.DefaultPahoMessageConverter;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.util.StringUtils;

@Configuration
public class Config {

@Value("${mqtt.broker.host}")
private String host;

@Value("${mqtt.broker.port}")
private int port;

@Value("${mqtt.broker.username}")
private String username;

@Value("${mqtt.broker.password}")
private String password;

{{#each asyncapi.topics as |topic key|}}
{{#if topic.subscribe}}
@Value("${mqtt.topic.{{~topic.x-service-name~}}Topic}")
private String {{topic.x-service-name}}Topic;

{{/if}}
{{/each}}

@Bean
public MqttPahoClientFactory mqttClientFactory() {
DefaultMqttPahoClientFactory factory = new DefaultMqttPahoClientFactory();
MqttConnectOptions options = new MqttConnectOptions();
options.setServerURIs(new String[] { host + ":" + port });
if (!StringUtils.isEmpty(username)) {
options.setUserName(username);
}
if (!StringUtils.isEmpty(password)) {
options.setPassword(password.toCharArray());
}
factory.setConnectionOptions(options);
return factory;
}

// consumer

@Autowired
MessageHandlerService messageHandlerService;
{{#each asyncapi.topics as |topic key|}}

{{#if topic.subscribe}}
@Bean
public IntegrationFlow {{camelCase topic.x-service-name}}Flow() {
return IntegrationFlows.from({{camelCase topic.x-service-name}}Inbound())
.handle(messageHandlerService::handle{{upperFirst topic.x-service-name}})
.get();
}

@Bean
public MessageProducerSupport {{camelCase topic.x-service-name}}Inbound() {
MqttPahoMessageDrivenChannelAdapter adapter = new MqttPahoMessageDrivenChannelAdapter("{{camelCase topic.x-service-name}}Subscriber",
mqttClientFactory(), {{topic.x-service-name}}Topic);
adapter.setCompletionTimeout(5000);
adapter.setConverter(new DefaultPahoMessageConverter());
return adapter;
}
{{/if}}
{{/each}}

// publisher
{{#each asyncapi.topics as |topic key|}}

{{#if topic.publish}}
@Bean
public MessageChannel {{camelCase topic.x-service-name}}OutboundChannel() {
return new DirectChannel();
}

@Bean
@ServiceActivator(inputChannel = "{{camelCase topic.x-service-name}}OutboundChannel")
public MessageHandler {{camelCase topic.x-service-name}}Outbound() {
MqttPahoMessageHandler pahoMessageHandler = new MqttPahoMessageHandler("{{camelCase topic.x-service-name}}Publisher", mqttClientFactory());
pahoMessageHandler.setAsync(true);
pahoMessageHandler.setDefaultTopic({{topic.x-service-name}}Topic);
return pahoMessageHandler;
}
{{/if}}
{{/each}}

}
Loading

0 comments on commit 91eba18

Please sign in to comment.