Skip to content

Commit e1c8e84

Browse files
committed
[futurepress#13][futurepress#72] mod_alias support + deprecates webdav option of Server constructor() + adds "extraConfig" option
1 parent a4ee464 commit e1c8e84

File tree

8 files changed

+92
-35
lines changed

8 files changed

+92
-35
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,10 @@ set(PLUGIN_STATIC
129129
PLUGIN_INIT(mod_staticfile)\n
130130
)
131131

132+
if(WITH_MOD_ALIAS)
133+
set(PLUGIN_STATIC "${PLUGIN_STATIC}PLUGIN_INIT(mod_alias)\n")
134+
endif()
135+
132136
if(WITH_MOD_WEBDAV)
133137
set(PLUGIN_STATIC "${PLUGIN_STATIC}PLUGIN_INIT(mod_webdav)\n")
134138
endif()

README.md

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and [old][Old Architecture] RN architectures.
3939
[OLD-README.md]: https://github.com/birdofpreyru/react-native-static-server/blob/master/OLD-README.md
4040
[getDeviceType()]: https://www.npmjs.com/package/react-native-device-info#getDeviceType
4141
[MainBundlePath]: https://www.npmjs.com/package/@dr.pogodin/react-native-fs#mainbundlepath
42+
[mod_alias]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_alias
4243
[mod_webdav]: https://redmine.lighttpd.net/projects/lighttpd/wiki/Mod_webdav
4344
[react-native-device-info]: https://www.npmjs.com/package/react-native-device-info
4445
[react-native-fs]: https://www.npmjs.com/package/react-native-fs
@@ -50,6 +51,7 @@ and [old][Old Architecture] RN architectures.
5051

5152
- [Getting Started](#getting-started)
5253
- [Bundling-in Server Assets Into an App Statically](#bundling-in-server-assets-into-an-app-statically)
54+
- [Enabling Alias module]
5355
- [Enabling WebDAV module]
5456
- [API Reference](#api-reference)
5557
- [Project History and Roadmap](#project-history-and-roadmap)
@@ -340,6 +342,40 @@ outside platform-specific sub-folders.
340342
</Target>
341343
```
342344
345+
### Enabling Alias Module
346+
[Enabling Alias module]: #enabling-alias-module
347+
348+
[Lighttpd]'s optional module [mod_alias] is used to specify a special document
349+
root for a given url-subset. To enable it:
350+
351+
1. **Android**: Edit `android/gradle.properties` file of your app, adding
352+
this flag:
353+
```gradle
354+
ReactNativeStaticServer_alias = true
355+
```
356+
357+
**iOS**: Use environment variable `RN_STATIC_SERVER_ALIAS=1` when
358+
installing or updating the pods (_i.e._ when doing `pod install` or
359+
`pod update`).
360+
361+
**macOS (Catalyst)**: The same as for iOS.
362+
363+
**Windows**: Does not require a special setup &mdash; the pre-compiled DLL
364+
for [mod_alias] is always packed with the library, and loaded if opted
365+
by [Server]'s [constructor()].
366+
367+
2. Use `extraConfig` option of [Server] [constructor()] to load and configure
368+
[mod_alias], for example:
369+
```ts
370+
extraConfig: `
371+
server.modules += ("mod_alias")
372+
alias.url = ("/sample/url" => "/special/root/path")
373+
`,
374+
```
375+
With this configuration incoming request `GET /sample/url/file` will be
376+
routed to the `/special/root/path/file`, rather than the `FILE_DIR/sample/url/file`,
377+
where `FILE_DIR` is the value you provided to the [Server]'s `fileDir` option.
378+
343379
### Enabling WebDAV Module
344380
[Enabling WebDAV module]: #enabling-webdav-module
345381
@@ -348,15 +384,11 @@ HTTP extensions that provides a framework allowing to create, change, and move
348384
documents on a server &mdash; essentially an easy way to enable `POST`, `PUT`,
349385
_etc._ functionality for selected routes.
350386
351-
**BEWARE:** _As of now, I only need it for implementing a local testing of
352-
network functionality inside [@dr.pogodin/react-native-fs] library, thus for
353-
development needs only, and I have not put efforts to build it with all features
354-
(support of props and locks), and have not looked much into what configuration
355-
is needed for its safe and flexible production use._
387+
**BEWARE:** _As of now, props and locks are not supported._
356388
357389
To enable [mod_webdav] in the library you need (1) configure your host RN app
358390
to build Lighttpd with [mod_webdav] included; (2) opt-in to use it for selected
359-
routes when you create [Server] instance inside your TypeScript code.
391+
routes when you create [Server] instance, using `extraConfig` option.
360392
361393
1. **Android**: Edit `android/gradle.properties` file of your app, adding
362394
this flag in there:
@@ -374,8 +406,16 @@ routes when you create [Server] instance inside your TypeScript code.
374406
for [WebDAV] module is always packed with the library, and loaded if opted
375407
for by [Server]'s [constructor()].
376408
377-
2. Use `webdav` option of [Server]'s [constructor()] to enable [WebDAV] for
378-
selected routes of the created server instance.
409+
2. Use `extraConfig` option of [Server]'s [constructor()] to load [mod_webdav]
410+
and use it for selected routes of the created server instance, for example:
411+
```ts
412+
extraConfig: `
413+
server.modules += ("mod_webdav")
414+
$HTTP["url"] =~ "^/dav/($|/)" {
415+
webdav.activate = "enable"
416+
}
417+
`,
418+
```
379419
380420
## API Reference
381421
- [Server] &mdash; Represents a server instance.
@@ -452,6 +492,12 @@ within `options` argument:
452492
**BEWARE:** If you opt for file logging with this option, it is up to you
453493
to control and purge the [ERROR_LOG_FILE] as needed.
454494
495+
- `extraConfig` &mdash; **string** &mdash; Optional. If given, it should be
496+
a valid piece of
497+
[Lighttpd configuration](https://redmine.lighttpd.net/projects/lighttpd/wiki/Docs_Configuration),
498+
and it will be appended to the base Lighttpd config generated by this
499+
library according to the other server options.
500+
455501
- `hostname` &mdash; **string** &mdash; Optional. Sets the address for server
456502
to bind to.
457503
- By default, when `nonLocal` option is **false**, `hostname` is set equal
@@ -502,20 +548,9 @@ within `options` argument:
502548
`details` values set equal "_App entered background_",
503549
and "_App entered foreground_" strings.
504550
505-
- `webdav` &mdash; **string[]** &mdash; Optional. Enables [WebDAV] for specified
506-
server routes. To use this option, first read [Enabling WebDAV module]. Each
507-
string inside provided `webdav` array is expected to be a Perl-style regular
508-
expression for the route(s) on which [WebDAV] should be enabled. For example,
509-
to enable [WebDAV] for `/dav` directory and everything below it, you should
510-
give `webdav = ["^/dav($|/)"]`, which will add the following configuration
511-
to the internal [Lighttpd] config of the server:
512-
```perl
513-
$HTTP["url"] =~ "^/dav($|/)" {
514-
webdav.activate = "enable"
515-
}
516-
```
517-
As of now, we don't yet support more advanced features and configurations for
518-
[mod_webdav].
551+
- **DEPRECATED**: `webdav` &mdash; **string[]** &mdash; It still works, but it
552+
will be removed in future versions. Instead of it use `extraConfig` option to
553+
enable and configure [WebDAV] as necessary (see [Enabling WebDAV module]).
519554
520555
#### .addStateListener()
521556
[.addStateListener()]: #addstatelistener

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ android {
4343
// TODO: I guess, there should be a more elegant way to append
4444
// arguments here based on flags set in project Gradle properties,
4545
// but for now this will do.
46+
(project.properties["ReactNativeStaticServer_alias"] ? "-DWITH_MOD_ALIAS=ON" : "-DWITH_MOD_ALIAS=OFF"),
4647
(project.properties["ReactNativeStaticServer_webdav"] ? "-DWITH_MOD_WEBDAV=ON" : "-DWITH_MOD_WEBDAV=OFF")
4748
targets "lighttpd"
4849
}

example/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ to prepare and run the example:
2929
- `npm run android`
3030
- On **iOS**:
3131
- ```sh
32-
cd ios && RCT_NEW_ARCH_ENABLED=1 pod install
32+
cd ios && RCT_NEW_ARCH_ENABLED=1 RN_STATIC_SERVER_ALIAS=1 pod install
3333
```
3434
Here `RCT_NEW_ARCH_ENABLED=1` is optional, omit it to build for the old RN
3535
architecture.

example/android/gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,5 @@ newArchEnabled=true
4343
# If set to false, you will be using JSC instead.
4444
hermesEnabled=true
4545

46+
ReactNativeStaticServer_alias = true
4647
ReactNativeStaticServer_webdav = true

example/src/App.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ export default function App() {
7676
// opt-in for building the library with WebDAV support enabled
7777
// (see README for details).
7878
// webdav: ['^/dav($|/)'],
79+
80+
extraConfig: `
81+
server.modules += ("mod_alias")
82+
alias.url = (
83+
"/some/path" => "${fileDir}"
84+
)
85+
`,
7986
});
8087
const serverId = server.id;
8188

@@ -198,6 +205,11 @@ export default function App() {
198205
source={origin ? { uri: origin } : { html: '' }}
199206
/>
200207
</View>
208+
<View style={styles.webview}>
209+
<WebView
210+
source={origin ? { uri: `${origin}/some/path` } : { html: '' }}
211+
/>
212+
</View>
201213
</SafeAreaView>
202214
);
203215
}

src/config.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ export type ErrorLogOptions = {
4848
*/
4949
export type StandardConfigOptions = {
5050
errorLog?: ErrorLogOptions;
51+
extraConfig: string;
5152
fileDir: string;
5253
hostname: string;
5354
port: number;
54-
webdav?: string[];
55+
webdav?: string[]; // DEPRECATED
5556
};
5657

5758
/**
@@ -91,19 +92,15 @@ function errorLogConfig(errorLogOptions?: ErrorLogOptions): string {
9192
*/
9293
function standardConfig({
9394
errorLog,
95+
extraConfig,
9496
fileDir,
9597
hostname,
9698
port,
97-
webdav,
99+
webdav, // DEPRECATED
98100
}: StandardConfigOptions) {
99-
const modules: string[] = [];
100-
if (webdav) modules.push('mod_webdav');
101-
const modulesString: string = modules.length
102-
? `server.modules += ( "${modules.join('", "')}")`
103-
: '';
104-
105101
let webdavConfig = '';
106102
if (webdav) {
103+
webdavConfig += 'server.modules += ("mod_webdav")';
107104
for (let i = 0; i < webdav.length; ++i) {
108105
webdavConfig += `$HTTP["url"] =~ "${webdav[i]}" { webdav.activate = "enable" }`;
109106
}
@@ -116,8 +113,8 @@ function standardConfig({
116113
${errorLogConfig(errorLog)}
117114
index-file.names += ("index.xhtml", "index.html", "index.htm", "default.htm", "index.php")
118115
119-
${modulesString}
120-
${webdavConfig}`;
116+
${webdavConfig}
117+
${extraConfig}`;
121118
}
122119

123120
/**

src/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class StaticServer {
8383
_appStateSub?: NativeEventSubscription;
8484
_configPath?: string;
8585
_errorLog?: ErrorLogOptions;
86+
_extraConfig: string;
8687
_fileDir: string;
8788
_hostname = '';
8889

@@ -161,15 +162,18 @@ class StaticServer {
161162
*/
162163
constructor({
163164
errorLog = false,
165+
extraConfig = '',
164166
fileDir,
165167
hostname,
166168

167169
/* DEPRECATED */ nonLocal = false,
168170

169171
port = 0,
170172
stopInBackground = false,
171-
webdav,
173+
174+
/* DEPRECATED */ webdav,
172175
}: {
176+
extraConfig?: string;
173177
errorLog?: boolean | ErrorLogOptions;
174178
fileDir: string;
175179
hostname?: string;
@@ -179,10 +183,12 @@ class StaticServer {
179183
port?: number;
180184
stopInBackground?: boolean;
181185

182-
webdav?: string[];
186+
/* DEPRECATED */ webdav?: string[];
183187
}) {
184188
if (errorLog) this._errorLog = errorLog === true ? {} : errorLog;
185189

190+
this._extraConfig = extraConfig;
191+
186192
this._nonLocal = nonLocal;
187193
this._hostname = hostname || (nonLocal ? '' : LOOPBACK_ADDRESS);
188194

@@ -288,6 +294,7 @@ class StaticServer {
288294
await this._removeConfigFile();
289295
this._configPath = await newStandardConfigFile({
290296
errorLog: this._errorLog,
297+
extraConfig: this._extraConfig,
291298
fileDir: this._fileDir,
292299
hostname: this._hostname,
293300
port: this._port,

0 commit comments

Comments
 (0)