forked from Qortal/qortal
CHANGED: added first API resources with jetty and jersey
This commit is contained in:
parent
9fb434cdd6
commit
d63ff02b97
27
pom.xml
27
pom.xml
@ -42,20 +42,43 @@
|
||||
<artifactId>commons-net</artifactId>
|
||||
<version>3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hsqldb</groupId>
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.4.0</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.core</groupId>
|
||||
<artifactId>jersey-server</artifactId>
|
||||
<version>2.27</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>4.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-server</artifactId>
|
||||
<version>9.4.11.v20180605</version>
|
||||
<classifier>config</classifier>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.containers</groupId>
|
||||
<artifactId>jersey-container-servlet</artifactId>
|
||||
<artifactId>jersey-container-servlet-core</artifactId>
|
||||
<version>2.27</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-maven-plugin</artifactId>
|
||||
<artifactId>jetty-servlet</artifactId>
|
||||
<version>9.4.11.v20180605</version>
|
||||
<type>jar</type>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish.jersey.inject</groupId>
|
||||
<artifactId>jersey-hk2</artifactId>
|
||||
<version>2.27</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
20
src/Start.java
Normal file
20
src/Start.java
Normal file
@ -0,0 +1,20 @@
|
||||
|
||||
import api.ApiService;
|
||||
import repository.DataException;
|
||||
import repository.RepositoryFactory;
|
||||
import repository.RepositoryManager;
|
||||
import repository.hsqldb.HSQLDBRepositoryFactory;
|
||||
|
||||
|
||||
public class Start {
|
||||
private static final String connectionUrl = "jdbc:hsqldb:mem:db/test;create=true;close_result=true;sql.strict_exec=true;sql.enforce_names=true;sql.syntax_mys=true";
|
||||
|
||||
public static void main(String args[]) throws DataException
|
||||
{
|
||||
RepositoryFactory repositoryFactory = new HSQLDBRepositoryFactory(connectionUrl);
|
||||
RepositoryManager.setRepositoryFactory(repositoryFactory);
|
||||
|
||||
ApiService apiService = new ApiService();
|
||||
apiService.start();
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package api;
|
||||
|
||||
//import io.swagger.jaxrs.config.DefaultJaxrsConfig;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@ -10,37 +12,46 @@ import org.eclipse.jetty.servlet.ServletHolder;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.glassfish.jersey.servlet.ServletContainer;
|
||||
|
||||
|
||||
import settings.Settings;
|
||||
|
||||
public class ApiService {
|
||||
|
||||
public Server server;
|
||||
private Server server;
|
||||
|
||||
public ApiService()
|
||||
{
|
||||
//CREATE CONFIG
|
||||
// resources to register
|
||||
Set<Class<?>> s = new HashSet<Class<?>>();
|
||||
s.add(BlocksResource.class);
|
||||
|
||||
ResourceConfig config = new ResourceConfig(s);
|
||||
|
||||
//CREATE CONTAINER
|
||||
ServletContainer container = new ServletContainer(config);
|
||||
// create RPC server
|
||||
this.server = new Server(Settings.getInstance().getRpcPort());
|
||||
|
||||
//CREATE CONTEXT
|
||||
ServletContextHandler context = new ServletContextHandler();
|
||||
context.setContextPath("/");
|
||||
context.addServlet(new ServletHolder(container),"/*");
|
||||
|
||||
//CREATE WHITELIST
|
||||
// whitelist
|
||||
InetAccessHandler accessHandler = new InetAccessHandler();
|
||||
for(String pattern : Settings.getInstance().getRpcAllowed())
|
||||
accessHandler.include(pattern);
|
||||
this.server.setHandler(accessHandler);
|
||||
|
||||
// context
|
||||
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.NO_SESSIONS);
|
||||
context.setContextPath("/");
|
||||
accessHandler.setHandler(context);
|
||||
|
||||
//CREATE RPC SERVER
|
||||
this.server = new Server(Settings.getInstance().getRpcPort());
|
||||
this.server.setHandler(accessHandler);
|
||||
// API servlet
|
||||
ServletContainer container = new ServletContainer(config);
|
||||
ServletHolder apiServlet = new ServletHolder(container);
|
||||
apiServlet.setInitOrder(1);
|
||||
context.addServlet(apiServlet, "/api/*");
|
||||
|
||||
/*
|
||||
// Setup Swagger servlet
|
||||
ServletHolder swaggerServlet = context.addServlet(DefaultJaxrsConfig.class, "/swagger-core");
|
||||
swaggerServlet.setInitOrder(2);
|
||||
swaggerServlet.setInitParameter("api.version", "1.0.0");
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
public void start()
|
||||
|
@ -1,9 +1,12 @@
|
||||
package api;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import javax.ws.rs.GET;
|
||||
import javax.ws.rs.Path;
|
||||
import javax.ws.rs.Produces;
|
||||
import javax.ws.rs.WebApplicationException;
|
||||
import javax.ws.rs.core.Context;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import repository.DataException;
|
||||
@ -13,6 +16,8 @@ import repository.RepositoryManager;
|
||||
@Path("blocks")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public class BlocksResource {
|
||||
@Context
|
||||
HttpServletRequest request;
|
||||
|
||||
@GET
|
||||
@Path("/height")
|
||||
|
@ -26,7 +26,7 @@ public class Settings {
|
||||
|
||||
//RPC
|
||||
private int rpcPort = 9085;
|
||||
private List<String> rpcAllowed = new ArrayList<String>(Arrays.asList("127.0.0.1"));
|
||||
private List<String> rpcAllowed = new ArrayList<String>(Arrays.asList("127.0.0.1", "::1")); // ipv4, ipv6
|
||||
private boolean rpcEnabled = true;
|
||||
|
||||
// Constants
|
||||
|
Loading…
Reference in New Issue
Block a user