New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Open API Module #158

Merged
merged 3 commits into from Feb 12, 2018
Jump to file or symbol
Failed to load files and symbols.
+209 −1
Diff settings

Always

Just for now

@@ -75,6 +75,7 @@ public Config map(String name) throws Exception {
public static final Config API = section("api");
public static final Config PAGES = section("pages");
public static final Config BENCHMARK = section("benchmark");
public static final Config OPENAPI = section("openapi");
static void applyConfig(Config config) {
RapidoidEnv.touch();
Copy path View file
@@ -104,7 +104,8 @@
<module>rapidoid-watch</module>
<module>rapidoid-integration-tests</module>
<module>app-maven-plugin</module>
</modules>
<module>rapidoid-openapi</module>
</modules>
<build>
<plugins>
@@ -148,6 +148,13 @@ protected void bootstrap() {
}
};
private static final ServiceBootstrap openapi = new ServiceBootstrap() {
@Override
protected void bootstrap() {
getGoodies().openapi(On.setup());
}
};
public AppBootstrap services() {
services.run();
return this;
@@ -223,6 +230,11 @@ public AppBootstrap beans() {
return this;
}
public AppBootstrap openapi() {
oauth.run();
return this;
}
static IGoodies getGoodies() {
Class<?> goodiesClass;
@@ -261,6 +273,7 @@ static void reset() {
oauth.reset();
adminCenter.reset();
beans.reset();
openapi.reset();
}
}
@@ -62,4 +62,5 @@
void echo(Setup setup);
void openapi(Setup setup);
}
Copy path View file
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>rapidoid</artifactId>
<groupId>org.rapidoid</groupId>
<version>5.5.5-SNAPSHOT</version>
</parent>
<artifactId>rapidoid-openapi</artifactId>
<packaging>jar</packaging>
<description>Rapidoid OpenAPI</description>
<dependencies>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-essentials</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-http-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-http-server</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.rapidoid</groupId>
<artifactId>rapidoid-gui</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,102 @@
/*-
* #%L
* rapidoid-openapi
* %%
* Copyright (C) 2014 - 2018 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
package org.rapidoid.openapi;
import org.rapidoid.RapidoidThing;
import org.rapidoid.config.Conf;
import org.rapidoid.config.Config;
import org.rapidoid.http.HttpRoutes;
import org.rapidoid.http.Req;
import org.rapidoid.http.ReqHandler;
import org.rapidoid.http.Route;
import org.rapidoid.log.Log;
import org.rapidoid.render.Template;
import org.rapidoid.render.Templates;
import org.rapidoid.setup.Setup;
import org.rapidoid.u.U;
import org.rapidoid.util.Msc;
import org.rapidoid.gui.GUI;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;
public class OpenAPI extends RapidoidThing {
private static final Template OPENAPI_TEMPLATE = Templates.load("openapi-template.yaml");
private static final Template OPENAPI_PATH_TEMPLATE = Templates.load("openapi-path.yaml");
private static final Template OPENAPI_VERB_TEMPLATE = Templates.load("openapi-verb.yaml");
private static final Config OPENAPI = Conf.OPENAPI;
public static void register(Setup setup) {
if (OPENAPI.isEmpty()) {
Log.warn("OpenAPI is not configured!");
}
final StringBuilder openApiYaml = new StringBuilder();
openApiYaml.append(OPENAPI_TEMPLATE.render(U.map("openapi_project_version", "1.0", "openapi_project_title", "Project Title", "openapi_project_url", "http://openapi.rapidoid.org")));
Hashtable<String, List<Route>> paths = null;
HttpRoutes routes = setup.routes();
if (routes != null && !routes.isEmpty()) {
Set<Route> routeSet = routes.all();
paths = new Hashtable<String, List<Route>>();
for (Route route: routeSet) {
String path = route.path().trim();
if (!paths.containsKey(path)) {
paths.put(path, new ArrayList<Route>());
}
List<Route> routeList = paths.get(path);
routeList.add(route);
}
}
if (paths != null && paths.size() > 0) {
Set<String> keys = paths.keySet();
for (String path: keys) {
openApiYaml.append(OPENAPI_PATH_TEMPLATE.render(U.map("route_path", path)));
List<Route> routeListPath = paths.get(path);
for (Route route: routeListPath) {
String verb = route.verb().name().toLowerCase();
openApiYaml.append(OPENAPI_VERB_TEMPLATE.render(U.map("route_verb", verb)));
}
}
}
final String openApiYamlPage = openApiYaml.toString();
setup.get(Msc.specialUri("openapi")).plain(new ReqHandler() {
@Override
public Object execute(Req req) throws Exception {
return GUI.hardcoded(openApiYamlPage);
}
});
}
}
@@ -0,0 +1 @@
${route_path}:
@@ -0,0 +1,9 @@
openapi: "3.0.0"
info:
version: ${openapi_project_version}
title: ${openapi_project_title}
license:
name: MIT
servers:
- url: ${openapi_project_url}
paths:
@@ -0,0 +1 @@
${route_verb}:
@@ -0,0 +1,25 @@
package org.rapidoid.openapi;
import org.rapidoid.setup.On;
import org.rapidoid.setup.Setup;
public class OpenAPIDemo {
public static void main(String[] args) {
Setup setup = On.setup();
On.get("/test1/").plain(OpenAPIDemo.sayHello());
On.get("/test2/saida").plain(OpenAPIDemo.sayHello());
On.get("/test2/output").plain(OpenAPIDemo.sayHello());
On.post("/test2/output").plain(OpenAPIDemo.sayHello());
On.delete("/test2/output").plain(OpenAPIDemo.sayHello());
OpenAPI.register(setup);
setup.activate();
}
public static String sayHello() {
return "Hello";
}
}
@@ -197,4 +197,13 @@ static String uri(String path) {
return Msc.specialUri(path);
}
public static void openapi(Setup setup) {
Class<?> openApiClass = Cls.getClassIfExists("org.rapidoid.oauth.OpenAPI");
U.must(openApiClass != null, "Cannot find the OpenAPI components, is module 'rapidoid-openapi' missing?");
Method bootstrap = Cls.getMethod(openApiClass, "bootstrap", Setup.class);
Cls.invokeStatic(bootstrap, setup);
}
}
@@ -116,4 +116,9 @@ public void discovery(Setup setup) {
public void echo(Setup setup) {
Goodies.echo(setup);
}
@Override
public void openapi(Setup setup) {
Goodies.openapi(setup);
}
}
ProTip! Use n and p to navigate between commits in a pull request.