Archived
0

Web interface: API управления главным меню

This commit is contained in:
2017-03-12 02:23:49 +03:00
parent 9a29899811
commit 2703d971c1
9 changed files with 112 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys'
version = '0.10-SNAPSHOT'
version = '0.11-SNAPSHOT'
buildscript {
repositories {
@@ -26,7 +26,8 @@ bundle {
instructions << [
'Bundle-Name': 'ASys Web interface',
'Bundle-Activator': 'asys.webinterface.Activator',
'Import-Package': '*'
'Import-Package': '*',
'Export-Package': 'asys.webinterface.api'
]
exclude group: 'com.google.code.gson'
exclude group: 'commons-io'

View File

@@ -4,20 +4,33 @@
*/
package asys.webinterface;
import asys.webinterface.api.MainMenu;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator implements BundleActivator {
private final Logger logger = LoggerFactory.getLogger(Activator.class);
private ServiceRegistration<?> serviceMainMenu;
private WebServer webServer;
@Override
public void start(BundleContext context) throws Exception {
webServer = new WebServer(context);
MainMenuImpl mainMenu = new MainMenuImpl();
mainMenu.addItem("Модули", "#");
logger.trace("Register service: {}", MainMenu.class);
serviceMainMenu = context.registerService(MainMenu.class, mainMenu, null);
webServer = new WebServer(context, mainMenu);
webServer.start(8778);
}
@Override
public void stop(BundleContext context) throws Exception {
webServer.stop();
serviceMainMenu.unregister();
}
}

View File

@@ -0,0 +1,39 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-03-12
*/
package asys.webinterface;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.OutputStream;
public class AppJsHandler implements HttpHandler {
private final Logger logger = LoggerFactory.getLogger(AppJsHandler.class);
private String doneAppJs;
private int size;
public AppJsHandler(MainMenuImpl mainMenu) {
try {
String appJs = IOUtils.toString(getClass().getResourceAsStream("/app.js"), "UTF-8");
doneAppJs = appJs.replace("<{mainMenuItems}>", mainMenu.toReacJS());
size = doneAppJs.getBytes(IndexHandler.defaultCharset).length;
} catch (IOException e) {
logger.error("app.js", e);
}
}
@Override
public void handle(HttpExchange httpExchange) throws IOException {
httpExchange.getResponseHeaders().add("Context-Type","text/javascript;charset=utf-8");
httpExchange.sendResponseHeaders(200, size);
OutputStream responseBody = httpExchange.getResponseBody();
responseBody.write(doneAppJs.getBytes(IndexHandler.defaultCharset));
responseBody.close();
}
}

View File

@@ -7,19 +7,15 @@ package asys.webinterface;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.apache.commons.io.IOUtils;
import org.osgi.framework.BundleContext;
import java.io.*;
import java.nio.charset.Charset;
public class IndexHandler implements HttpHandler {
private static final Charset defaultCharset = Charset.forName("UTF-8");
private BundleContext context;
static final Charset defaultCharset = Charset.forName("UTF-8");
private String htmlTemplate;
public IndexHandler(BundleContext context) {
this.context = context;
public IndexHandler() {
try {
InputStream inputStream = getClass().getResourceAsStream("/index.html");
htmlTemplate = IOUtils.toString(inputStream, "UTF-8");

View File

@@ -0,0 +1,38 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-03-11
*/
package asys.webinterface;
import asys.webinterface.api.MainMenu;
import java.util.HashMap;
import java.util.Map;
import java.util.StringJoiner;
public class MainMenuImpl implements MainMenu {
private Map<String, String> items = new HashMap<>();
@Override
public void addItem(String title, String href) {
items.put(title, href);
}
public String toReacJS() {
StringJoiner sj = new StringJoiner(",");
for(Map.Entry<String, String> entry : items.entrySet()) {
sj.add(String.format(
"ce(MainMenuItem, {title: '%s', href: '%s'})",
entry.getKey().replace("'","\\'"), entry.getValue().replace("'","\\'")
));
}
return sj.toString();
}
@Override
public String toString() {
return "MainMenuImpl{" +
"items=" + items +
'}';
}
}

View File

@@ -13,14 +13,17 @@ import java.net.InetSocketAddress;
public class WebServer {
private HttpServer server;
private BundleContext context;
private MainMenuImpl mainMenu;
public WebServer(BundleContext context) {
public WebServer(BundleContext context, MainMenuImpl mainMenu) {
this.context = context;
this.mainMenu = mainMenu;
}
public void start(int port) throws IOException {
server = HttpServer.create(new InetSocketAddress(port), 0);
server.createContext("/", new IndexHandler(context));
server.createContext("/", new IndexHandler());
server.createContext("/app.js", new AppJsHandler(mainMenu));
server.createContext("/static", new StaticHandler());
server.createContext("/ajax", new AjaxHandler(context));
server.start();

View File

@@ -0,0 +1,9 @@
/*
* DmitriyMX <dimon550@gmail.com>
* 2017-03-11
*/
package asys.webinterface.api;
public interface MainMenu {
void addItem(String title, String href);
}

View File

@@ -34,7 +34,7 @@ var App = React.createClass({
}
else {
return (ce('div', null,
ce(Navbar),
ce(Navbar, {items: [<{mainMenuItems}>]}),
ce('div', {className: 'container', style: {marginTop: '60px'}},
ce(Panel, {title: 'Модули'},
ce(BundleTable, null, this.tableRows)

View File

@@ -15,7 +15,7 @@
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
<script src="/static/fetch.js"></script>
<script src="/static/components.js"></script>
<script src="/static/app.js"></script>
<script src="/app.js"></script>
</head>
<body>
<div id="app"></div>