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

View File

@@ -4,20 +4,33 @@
*/ */
package asys.webinterface; package asys.webinterface;
import asys.webinterface.api.MainMenu;
import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Activator implements BundleActivator { public class Activator implements BundleActivator {
private final Logger logger = LoggerFactory.getLogger(Activator.class);
private ServiceRegistration<?> serviceMainMenu;
private WebServer webServer; private WebServer webServer;
@Override @Override
public void start(BundleContext context) throws Exception { 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); webServer.start(8778);
} }
@Override @Override
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {
webServer.stop(); 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.HttpExchange;
import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpHandler;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.osgi.framework.BundleContext;
import java.io.*; import java.io.*;
import java.nio.charset.Charset; import java.nio.charset.Charset;
public class IndexHandler implements HttpHandler { public class IndexHandler implements HttpHandler {
private static final Charset defaultCharset = Charset.forName("UTF-8"); static final Charset defaultCharset = Charset.forName("UTF-8");
private BundleContext context;
private String htmlTemplate; private String htmlTemplate;
public IndexHandler(BundleContext context) { public IndexHandler() {
this.context = context;
try { try {
InputStream inputStream = getClass().getResourceAsStream("/index.html"); InputStream inputStream = getClass().getResourceAsStream("/index.html");
htmlTemplate = IOUtils.toString(inputStream, "UTF-8"); 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 { public class WebServer {
private HttpServer server; private HttpServer server;
private BundleContext context; private BundleContext context;
private MainMenuImpl mainMenu;
public WebServer(BundleContext context) { public WebServer(BundleContext context, MainMenuImpl mainMenu) {
this.context = context; this.context = context;
this.mainMenu = mainMenu;
} }
public void start(int port) throws IOException { public void start(int port) throws IOException {
server = HttpServer.create(new InetSocketAddress(port), 0); 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("/static", new StaticHandler());
server.createContext("/ajax", new AjaxHandler(context)); server.createContext("/ajax", new AjaxHandler(context));
server.start(); 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 { else {
return (ce('div', null, return (ce('div', null,
ce(Navbar), ce(Navbar, {items: [<{mainMenuItems}>]}),
ce('div', {className: 'container', style: {marginTop: '60px'}}, ce('div', {className: 'container', style: {marginTop: '60px'}},
ce(Panel, {title: 'Модули'}, ce(Panel, {title: 'Модули'},
ce(BundleTable, null, this.tableRows) 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="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js"></script>
<script src="/static/fetch.js"></script> <script src="/static/fetch.js"></script>
<script src="/static/components.js"></script> <script src="/static/components.js"></script>
<script src="/static/app.js"></script> <script src="/app.js"></script>
</head> </head>
<body> <body>
<div id="app"></div> <div id="app"></div>