nanoserver started
This commit is contained in:
parent
b565450820
commit
cd312207b8
5 changed files with 197 additions and 9 deletions
65
OsmAnd/assets/server/go.html
Normal file
65
OsmAnd/assets/server/go.html
Normal file
|
@ -0,0 +1,65 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="cache-control" content="max-age=0" />
|
||||
<meta http-equiv="cache-control" content="no-cache" />
|
||||
<meta http-equiv="expires" content="0" />
|
||||
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
|
||||
<meta http-equiv="pragma" content="no-cache" />
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>OsmAnd - Offline Mobile Maps and Navigation</title>
|
||||
<link rel="stylesheet" type="text/css" href="/css/site.css?v=4"/>
|
||||
<!-- <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" /> -->
|
||||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.1.0/dist/leaflet.css" />
|
||||
<script src="https://unpkg.com/leaflet@1.1.0/dist/leaflet.js"></script>
|
||||
<script type="text/javascript" src="/scripts/jquery-3.1.0.min.js"></script>
|
||||
<!--
|
||||
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
|
||||
-->
|
||||
|
||||
<script type="text/javascript" src="/scripts/js.cookie.js"></script>
|
||||
<script type="text/javascript" src="/scripts/go.js?v=5"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="gocontainer" id="gocontainer">
|
||||
<div class="goheader">
|
||||
<a href="/"><img src="/images/logo-grey.png" class="logo"/></a>
|
||||
<div class="coordinatescontainer">
|
||||
<div><span class="title">LAT</span><span class="coordinate latitude"></span></div>
|
||||
<div><span class="title">LON</span><span class="coordinate longitude"></span></div>
|
||||
</div>
|
||||
<div class="clear:both;"></div>
|
||||
</div>
|
||||
<div id="map"></div>
|
||||
<div class="gofooter">
|
||||
<div class="gobadges">
|
||||
<div class="badgecontainer google">
|
||||
<a href="https://play.google.com/store/apps/details?id=net.osmand.plus">
|
||||
<img alt="Get it on Google Play" src="https://play.google.com/intl/en_us/badges/images/generic/en-play-badge.png">
|
||||
</a>
|
||||
</div>
|
||||
<div class="badgecontainer apple">
|
||||
<a href="https://itunes.apple.com/us/app/id934850257"><img src="images/app-store-badge.png"></a>
|
||||
</div>
|
||||
<div class="badgecontainer amazon">
|
||||
<a href="http://www.amazon.com/gp/product/B00D0SEGMC/ref=mas_pm_OsmAnd-Maps-Navigation">
|
||||
<img alt="Get it on Amazon" src="images/amazon-apps-store.png">
|
||||
</a>
|
||||
</div>
|
||||
<div class="clear">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="overlay" style="display:none;"></div>
|
||||
<div class="popup" style="display:none;">
|
||||
<div class="logo"></div>
|
||||
<h1>Did you know</h1>
|
||||
<p>OsmAnd has an iOS application</p>
|
||||
<a class="button yes" href="javascript:void(0);">Yep, I've already got it</a>
|
||||
<a class="button no" href="javascript::void(0);">Nope, but I'd love to try it</a>
|
||||
<a class="button cancel" href="javascript:void(0);">Leave me alone</a>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
|
@ -11,6 +11,7 @@ import android.widget.Toast;
|
|||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
import net.osmand.plus.R;
|
||||
import net.osmand.plus.server.OsmAndHttpServer;
|
||||
|
||||
|
@ -70,6 +71,7 @@ public class ServerActivity extends AppCompatActivity {
|
|||
TrafficStats.setThreadStatsTag(THREAD_ID);
|
||||
try {
|
||||
server = new OsmAndHttpServer();
|
||||
server.setAndroidContext((OsmandApplication)this.getApplication());
|
||||
initialized = true;
|
||||
updateTextView("Server started at: http://" + OsmAndHttpServer.HOSTNAME
|
||||
+ ":" + OsmAndHttpServer.PORT);
|
||||
|
|
91
OsmAnd/src/net/osmand/plus/server/ApiRouter.java
Normal file
91
OsmAnd/src/net/osmand/plus/server/ApiRouter.java
Normal file
|
@ -0,0 +1,91 @@
|
|||
package net.osmand.plus.server;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
|
||||
import static fi.iki.elonen.NanoHTTPD.newFixedLengthResponse;
|
||||
|
||||
public class ApiRouter {
|
||||
private OsmandApplication androidContext;
|
||||
|
||||
public OsmandApplication getAndroidContext() {
|
||||
return androidContext;
|
||||
}
|
||||
|
||||
public void setAndroidContext(OsmandApplication androidContext) {
|
||||
this.androidContext = androidContext;
|
||||
}
|
||||
|
||||
public NanoHTTPD.Response route(NanoHTTPD.IHTTPSession session) {
|
||||
Log.d("SERVER", "URI: " + session.getUri());
|
||||
if (session.getUri().contains("/scripts/") ||
|
||||
session.getUri().contains("/images/") ||
|
||||
session.getUri().contains("/css/") ||
|
||||
session.getUri().contains("/favicon.ico")
|
||||
) {
|
||||
return getStatic(session.getUri());
|
||||
} else {
|
||||
return getGoHtml();
|
||||
}
|
||||
}
|
||||
|
||||
public NanoHTTPD.Response getStatic(String uri) {
|
||||
InputStream is = null;
|
||||
if (androidContext != null) {
|
||||
try {
|
||||
is = androidContext.getAssets().open("server" + uri);
|
||||
if (is.available() == 0){
|
||||
return ErrorResponses.response404;
|
||||
}
|
||||
return newFixedLengthResponse(
|
||||
NanoHTTPD.Response.Status.OK,
|
||||
"text/plain",
|
||||
is,
|
||||
is.available());
|
||||
} catch (IOException e) {
|
||||
return ErrorResponses.response500;
|
||||
}
|
||||
}
|
||||
return ErrorResponses.response500;
|
||||
}
|
||||
|
||||
public NanoHTTPD.Response getGoHtml() {
|
||||
String responseText = "";
|
||||
if (androidContext != null) {
|
||||
try {
|
||||
InputStream is = androidContext.getAssets().open("server/go.html");
|
||||
StringBuilder sb = new StringBuilder();
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(is,
|
||||
Charset.forName("UTF-8")));
|
||||
String str;
|
||||
while ((str = br.readLine()) != null) {
|
||||
sb.append(str);
|
||||
}
|
||||
br.close();
|
||||
responseText = sb.toString();
|
||||
} catch (IOException e) {
|
||||
return ErrorResponses.response500;
|
||||
}
|
||||
}
|
||||
return newFixedLengthResponse(responseText);
|
||||
}
|
||||
|
||||
static class ErrorResponses {
|
||||
static NanoHTTPD.Response response404 =
|
||||
newFixedLengthResponse(NanoHTTPD.Response.Status.NOT_FOUND,
|
||||
NanoHTTPD.MIME_PLAINTEXT, "404 Not Found");
|
||||
|
||||
static NanoHTTPD.Response response500 =
|
||||
newFixedLengthResponse(NanoHTTPD.Response.Status.INTERNAL_ERROR,
|
||||
NanoHTTPD.MIME_PLAINTEXT, "500 Internal Server Error");
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package net.osmand.plus.server;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
|
@ -9,21 +11,25 @@ public class OsmAndHttpServer extends NanoHTTPD {
|
|||
public static int PORT = 24990;
|
||||
public static String HOSTNAME = "0.0.0.0";
|
||||
|
||||
private ServerSessionHandler sessionHandler = new ServerSessionHandler();
|
||||
private OsmandApplication androidContext;
|
||||
|
||||
public OsmandApplication getAndroidContext() {
|
||||
return androidContext;
|
||||
}
|
||||
|
||||
public void setAndroidContext(OsmandApplication androidContext) {
|
||||
this.androidContext = androidContext;
|
||||
sessionHandler.setAndroidContext(androidContext);
|
||||
}
|
||||
|
||||
public OsmAndHttpServer() throws IOException {
|
||||
super(HOSTNAME,PORT);
|
||||
start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
|
||||
System.out.println("\nRunning! Point your browsers to http://localhost:8080/ \n");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Response serve(IHTTPSession session) {
|
||||
String msg = "<html><body><h1>Hello server</h1>\n";
|
||||
Map<String, String> parms = session.getParms();
|
||||
if (parms.get("username") == null) {
|
||||
msg += "<form action='?' method='get'>\n <p>Your name: <input type='text' name='username'></p>\n" + "</form>\n";
|
||||
} else {
|
||||
msg += "<p>Hello, " + parms.get("username") + "!</p>";
|
||||
}
|
||||
return newFixedLengthResponse(msg + "</body></html>\n");
|
||||
return sessionHandler.handle(session);
|
||||
}
|
||||
}
|
||||
|
|
24
OsmAnd/src/net/osmand/plus/server/ServerSessionHandler.java
Normal file
24
OsmAnd/src/net/osmand/plus/server/ServerSessionHandler.java
Normal file
|
@ -0,0 +1,24 @@
|
|||
package net.osmand.plus.server;
|
||||
|
||||
import net.osmand.plus.OsmandApplication;
|
||||
|
||||
import fi.iki.elonen.NanoHTTPD;
|
||||
|
||||
public class ServerSessionHandler {
|
||||
private OsmandApplication androidContext;
|
||||
|
||||
private ApiRouter router = new ApiRouter();
|
||||
|
||||
public OsmandApplication getAndroidContext() {
|
||||
return androidContext;
|
||||
}
|
||||
|
||||
public void setAndroidContext(OsmandApplication androidContext) {
|
||||
this.androidContext = androidContext;
|
||||
router.setAndroidContext(androidContext);
|
||||
}
|
||||
|
||||
public NanoHTTPD.Response handle(NanoHTTPD.IHTTPSession session) {
|
||||
return router.route(session);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue