Skip to content

Commit d0fd8e9

Browse files
committed
Fixed root, added AppState events to start and stop
1 parent fded512 commit d0fd8e9

File tree

4 files changed

+66
-50
lines changed

4 files changed

+66
-50
lines changed

android/src/main/java/com/futurepress/staticserver/FPStaticServerModule.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ public String getName() {
6969
@ReactMethod
7070
public void start(String _port, String root, Boolean localhost, Promise promise) {
7171

72+
if (server != null){
73+
promise.resolve(url);
74+
return;
75+
}
76+
7277
if (_port != null) {
7378
try {
7479
port = Integer.parseInt(_port);
@@ -77,34 +82,29 @@ public void start(String _port, String root, Boolean localhost, Promise promise)
7782
}
7883
}
7984

80-
if (root != null) {
81-
if(root.startsWith("/")) {
82-
www_root = new File(root);
83-
localPath = www_root.getAbsolutePath();
84-
} else {
85-
www_root = this.reactContext.getFilesDir();
86-
localPath = www_root.getAbsolutePath();
87-
}
85+
if(root != null && root.startsWith("/")) {
86+
www_root = new File(root);
87+
localPath = www_root.getAbsolutePath();
88+
} else {
89+
www_root = new File(this.reactContext.getFilesDir(), root);
90+
localPath = www_root.getAbsolutePath();
8891
}
8992

9093
if (localhost != null) {
9194
localhost_only = localhost;
9295
}
9396

94-
9597
try {
9698

9799
if(localhost_only) {
98-
byte[] ipAddr = new byte[]{127, 0, 0, 1};
99-
InetAddress localAddr = InetAddress.getByAddress(ipAddr);
100-
server = new WebServer(localAddr.toString(), port, www_root);
100+
server = new WebServer("localhost", port, www_root);
101101
} else {
102102
server = new WebServer(__getLocalIpAddress(), port, www_root);
103103
}
104104

105105

106106
if (localhost_only) {
107-
url = "http://127.0.0.1:" + port;
107+
url = "http://localhost:" + port;
108108
} else {
109109
url = "http://" + __getLocalIpAddress() + ":" + port;
110110
}
@@ -117,12 +117,10 @@ public void start(String _port, String root, Boolean localhost, Promise promise)
117117
String msg = e.getMessage();
118118

119119

120-
Log.w(LOGTAG, e);
121120

122121
// Server doesn't stop on refresh
123122
if (server != null && msg.equals("bind failed: EADDRINUSE (Address already in use)")){
124-
server.stop();
125-
start(_port, root, localhost, promise);
123+
promise.resolve(url);
126124
} else {
127125
promise.reject(null, msg);
128126
}
@@ -135,6 +133,7 @@ public void start(String _port, String root, Boolean localhost, Promise promise)
135133
@ReactMethod
136134
public void stop() {
137135
if (server != null) {
136+
Log.w(LOGTAG, "Stopped Server");
138137
server.stop();
139138
server = null;
140139
}
@@ -143,12 +142,12 @@ public void stop() {
143142
/* Shut down the server if app is destroyed or paused */
144143
@Override
145144
public void onHostResume() {
146-
start(null, null, null, null);
145+
//start(null, null, null, null);
147146
}
148147

149148
@Override
150149
public void onHostPause() {
151-
stop();
150+
//stop();
152151
}
153152

154153
@Override

index.js

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
2-
import { NativeModules } from 'react-native';
1+
import {
2+
NativeModules,
3+
AppState
4+
} from 'react-native';
35

46
const { FPStaticServer } = NativeModules;
57

@@ -47,26 +49,53 @@ class StaticServer {
4749
}
4850

4951
start() {
50-
if( this.started ){
51-
console.warn('StaticServer already running');
52+
if( this.running ){
53+
return console.warn('StaticServer already running');
5254
}
5355

54-
this.origin = LOCALHOST + this.port;
55-
5656
this.started = true;
57+
this.running = true;
58+
59+
AppState.addEventListener('change', this._handleAppStateChange.bind(this));
5760

5861
return FPStaticServer.start(this.port, this.root, this.localOnly);
5962
}
6063

6164
stop() {
62-
if( !this.started ){
63-
console.warn('StaticServer not running');
65+
if( !this.running ){
66+
return console.warn('StaticServer not running');
6467
}
6568

66-
this.started = false;
69+
this.running = false;
6770

6871
return FPStaticServer.stop();
6972
}
73+
74+
kill() {
75+
this.stop();
76+
this.started = false;
77+
AppState.addEventListener('change', this._handleAppStateChange.bind(this));
78+
}
79+
80+
_handleAppStateChange(appState) {
81+
if (!this.started) {
82+
return;
83+
}
84+
85+
if (appState === "active" && !this.running) {
86+
this.start();
87+
}
88+
89+
if (appState === "background" && this.running) {
90+
this.stop();
91+
}
92+
93+
if (appState === "inactive" && this.running) {
94+
this.stop();
95+
}
96+
}
97+
98+
7099
}
71100

72101
export default StaticServer;

ios/FPStaticServer.m

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@
2222

2323
@implementation FPStaticServer
2424

25-
//HTTPServer *httpServer;
26-
//NSString *_localPath = @"";
27-
//NSString *_url = @"";
28-
//
29-
//NSString *_www_root = @"";
30-
//int _port = 8888;
31-
//BOOL _localhost_only = false;
32-
3325
@synthesize bridge = _bridge;
3426

3527
RCT_EXPORT_MODULE();
@@ -105,12 +97,14 @@ - (NSDictionary *)getIPAddresses
10597

10698
NSString * root;
10799

108-
if( [optroot length] == 0 ){
100+
if( [optroot isEqualToString:@"DocumentDir"] ){
109101
root = [NSString stringWithFormat:@"%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] ];
102+
} else if( [optroot isEqualToString:@"BundleDir"] ){
103+
root = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] bundlePath] ];
110104
} else if([optroot hasPrefix:@"/"]) {
111105
root = optroot;
112-
} else if( [optroot isEqualToString:@"BUNDLE"] ){
113-
root = [NSString stringWithFormat:@"%@", [[NSBundle mainBundle] bundlePath] ];
106+
} else {
107+
root = [NSString stringWithFormat:@"%@/%@", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0], optroot ];
114108
}
115109

116110

@@ -135,16 +129,10 @@ - (NSDictionary *)getIPAddresses
135129

136130
if(self.localhost_only) [self.httpServer setInterface:IP_LOCALHOST];
137131

138-
// Serve files from our embedded Web folder
139-
const char * docroot = [self.www_root UTF8String];
140-
if(*docroot == '/') {
141-
self.localPath = self.www_root;
142-
} else {
143-
NSString* basePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"www"];
144-
self.localPath = [NSString stringWithFormat:@"%@/%@", basePath, self.www_root];
145-
}
146-
NSLog(@"Setting document root: %@", self.localPath);
147-
[self.httpServer setDocumentRoot:self.localPath];
132+
//self.localPath = self.www_root;
133+
134+
// NSLog(@"Setting document root: %@", self.www_root);
135+
[self.httpServer setDocumentRoot:self.www_root];
148136

149137

150138
NSError *error;
@@ -172,7 +160,7 @@ - (NSDictionary *)getIPAddresses
172160
[self.httpServer stop];
173161
self.httpServer = nil;
174162

175-
self.localPath = @"";
163+
self.www_root = @"";
176164
self.url = @"";
177165

178166
NSLog(@"httpd stopped");

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{
33
"name": "react-native-static-server",
4-
"version": "0.1.2",
4+
"version": "0.1.3",
55
"repository": "https://github.com/futurepress/react-native-static-server",
66
"description": "HTTP static file server for React Native",
77
"main": "index.js",

0 commit comments

Comments
 (0)