Skip to content

Commit

Permalink
added CORS support, fixes #204
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathias authored and mdzio committed May 26, 2020
1 parent 33d4ff7 commit 1bf4b57
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions ccu-historian/src-webapp/query/jsonrpc.gy
Expand Up @@ -32,6 +32,18 @@ import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer
@Field
private final static log = Logger.getLogger('mdz.ccuhistorian.webapp.JsonRpc')

// add CORS headers
response.setHeader('Access-Control-Allow-Origin', webServer.config.corsOrigin);
if (webServer.config.corsOrigin!='*') {
response.setHeader('Access-Control-Allow-Credentials', 'true');
}
response.setHeader('Access-Control-Allow-Methods', 'GET,POST,PUT,OPTIONS');
response.setHeader('Access-Control-Allow-Headers', '*');
if (request.method=="OPTIONS") {
response.status=response.SC_ACCEPTED
return
}

def rpcResponse=[:]
rpcResponse.id=null
try {
Expand Down
Expand Up @@ -39,6 +39,7 @@ class WebServerConfig {
Map<String, TrendDesign> trendDesigns=[:].withDefault { new TrendDesign(identifier: (String)it, displayName: (String)it) }
Map<String, Link> menuLinks=new TreeMap().withDefault { new Link() }
String[] apiKeys=[]
String corsOrigin="*"

void logDebug() {
log.fine "webServer.port=$port"
Expand All @@ -48,5 +49,6 @@ class WebServerConfig {
log.fine "webServer.trendDesigns=[${trendDesigns.collect { it.key }.join(', ')}]"
log.fine "webServer.apiKeys=[${apiKeys.collect { "'$it'" }.join(', ')}]"
log.fine "webServer.menuLinks=[${menuLinks.values().collect { it.text }.join(', ')}]"
log.fine "webServer.corsOrigin='$corsOrigin'"
}
}
1 change: 1 addition & 0 deletions dist-generic/src/ccu-historian-sample.config
Expand Up @@ -35,6 +35,7 @@
// webServer.trendDesigns ... (s.a. https://github.com/mdzio/ccu-historian/wiki#anpassung-der-trend-darstellung)
// webServer.apiKeys=[]
// webServer.menuLinks ... (s.a. https://github.com/mdzio/ccu-historian/wiki#eigene-verweise-auf-web-seiten)
// webServer.corsOrigin='*'
// historian.metaCycle=3600000 // 1 Stunde
// historian.bufferCount=5000
// historian.bufferTime=0
Expand Down

0 comments on commit 1bf4b57

Please sign in to comment.