Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit ed8e3cb

Browse files
authored
Merge pull request #216 from skatetdieu/master
Define Cross Domain Allow Headers To Be Set For The API
2 parents 315d5e9 + f42e12e commit ed8e3cb

File tree

4 files changed

+72
-4
lines changed

4 files changed

+72
-4
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ Edit the default configuration of the server by adding options to your **laravel
9191
| `sslCertChainPath` | `''` | The path to your server's ssl certificate chain |
9292
| `sslPassphrase` | `''` | The pass phrase to use for the certificate (if applicable) |
9393
| `socketio` | `{}` | Options to pass to the socket.io instance ([available options](https://github.com/socketio/engine.io#methods-1)) |
94+
| `apiOriginAllow` | `{}` | Configuration to allow API be accessed over CORS. [Example](#cross-domain-access-to-api)|
9495

9596
### Running with SSL
9697

@@ -186,6 +187,24 @@ List of users on a channel.
186187
GET /apps/:APP_ID/channels/:CHANNEL_NAME/users
187188
```
188189

190+
## Cross Domain Access To API
191+
Cross domain access can be specified in laravel-echo-server.json file by changing `allowCors` in `apiOriginAllow` to `true`. You can then set the CORS origin domain you want to allow, the http allow methods as a comma separated string (only GET and POST works as of right now) and the allow headers to allow to the API to receive.
192+
193+
Example below:
194+
195+
``` json
196+
{
197+
"apiOriginAllow":{
198+
"allowCors" : true,
199+
"allowOrigin" : "http://127.0.0.1",
200+
"allowMethods" : "GET, POST",
201+
"allowHeaders" : "Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id"
202+
}
203+
}
204+
205+
```
206+
This allows you to ping the API via AJAX calls from your app that could be running on the same domain but different port or it could be entirely different domain.
207+
189208
## Database
190209

191210
To persist presence channel data, there is support for use of Redis or SQLite as a key/value store. The key being the channel name, and the value being the list of presence channel members.

src/api/http-api.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,23 @@ export class HttpApi {
99
* @param {any} io
1010
* @param {any} channel
1111
* @param {any} express
12+
* @param {any} options object apiOriginAllow
1213
*/
13-
constructor(private io, private channel, private express) { }
14+
constructor(private io, private channel, private express, private options) { }
1415

1516
/**
1617
* Initialize the API.
1718
*/
1819
init(): void {
20+
if(this.options.allowCors){
21+
this.express.use( (req, res, next) => {
22+
res.header('Access-Control-Allow-Origin', this.options.allowOrigin);
23+
res.header('Access-Control-Allow-Methods', this.options.allowMethods);
24+
res.header('Access-Control-Allow-Headers', this.options.allowHeaders);
25+
next();
26+
});
27+
}
28+
1929
this.express.get(
2030
'/apps/:appId/status',
2131
(req, res) => this.getStatus(req, res)

src/cli/cli.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class Cli {
3232
init(yargs) {
3333
this.setupConfig().then((options) => {
3434
options = Object.assign({}, this.defaultOptions, options);
35-
35+
3636
if (options.addClient) {
3737
let client = {
3838
appId: this.createAppId(),
@@ -44,6 +44,13 @@ export class Cli {
4444
console.log('key: ' + colors.magenta(client.key));
4545
}
4646

47+
if(options.corsAllow){
48+
options.apiOriginAllow.allowCors = true;
49+
options.apiOriginAllow.allowOrigin = options.allowOrigin;
50+
options.apiOriginAllow.allowMethods = options.allowMethods;
51+
options.apiOriginAllow.allowHeaders = options.allowHeaders;
52+
}
53+
4754
this.saveConfig(options).then(() => {
4855
console.log('Configuration file saved. Run ' + colors.magenta.bold('laravel-echo-server start') + ' to run server.');
4956

@@ -101,6 +108,32 @@ export class Cli {
101108
default: false,
102109
message: 'Do you want to generate a client ID/Key for HTTP API?',
103110
type: 'confirm'
111+
}, {
112+
name: 'corsAllow',
113+
default: false,
114+
message: 'Do you want to setup cross domain access to API? Useful for AJAX request to API on same domain different port.',
115+
type: 'confirm'
116+
},{
117+
name: 'allowOrigin',
118+
default: 'http://localhost:80',
119+
message: 'Enter the domain you want CORS access to:',
120+
when: function(options){
121+
return options.corsAllow == true;
122+
}
123+
},{
124+
name: 'allowMethods',
125+
default: 'GET, POST',
126+
message: 'Enter the CORS HTTP methods you want to allow:',
127+
when: function(options){
128+
return options.corsAllow == true;
129+
}
130+
},{
131+
name: 'allowHeaders',
132+
default: 'Origin, Content-Type, X-Auth-Token, X-Requested-With, Accept, Authorization, X-CSRF-TOKEN, X-Socket-Id',
133+
message: 'Enter the CORS headers you want to allow:',
134+
when: function(options){
135+
return options.corsAllow == true;
136+
}
104137
}
105138
]);
106139
}

src/echo-server.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ export class EchoServer {
3434
sslCertPath: '',
3535
sslKeyPath: '',
3636
sslCertChainPath: '',
37-
sslPassphrase: ''
37+
sslPassphrase: '',
38+
apiOriginAllow:{
39+
allowCors : false,
40+
allowOrigin : '',
41+
allowMethods : '',
42+
allowHeaders : ''
43+
}
3844
};
3945

4046
/**
@@ -115,7 +121,7 @@ export class EchoServer {
115121
this.channel = new Channel(io, this.options);
116122
this.redisSub = new RedisSubscriber(this.options);
117123
this.httpSub = new HttpSubscriber(this.server.express, this.options);
118-
this.httpApi = new HttpApi(io, this.channel, this.server.express);
124+
this.httpApi = new HttpApi(io, this.channel, this.server.express, this.options.apiOriginAllow);
119125
this.httpApi.init();
120126

121127
this.onConnect();

0 commit comments

Comments
 (0)