api: WIP CORS supoport
This commit is contained in:
parent
0a8f701c3c
commit
cf60793395
@ -34,6 +34,10 @@ proc loadConfig*(args: Table[string, docopt.Value] = initTable[string, docopt.Va
|
|||||||
warn "Cannot read configuration file \"" & filePath & "\":\n\t" &
|
warn "Cannot read configuration file \"" & filePath & "\":\n\t" &
|
||||||
getCurrentExceptionMsg()
|
getCurrentExceptionMsg()
|
||||||
|
|
||||||
|
let knownOriginsArray =
|
||||||
|
if json.hasKey("knownOrigins"): json["knownOrigins"]
|
||||||
|
else: newJArray()
|
||||||
|
|
||||||
let cfg = CombinedConfig(docopt: args, json: json)
|
let cfg = CombinedConfig(docopt: args, json: json)
|
||||||
|
|
||||||
result = PMApiConfig(
|
result = PMApiConfig(
|
||||||
@ -41,7 +45,8 @@ proc loadConfig*(args: Table[string, docopt.Value] = initTable[string, docopt.Va
|
|||||||
dbConnString: cfg.getVal("db-conn-string"),
|
dbConnString: cfg.getVal("db-conn-string"),
|
||||||
debug: "true".startsWith(cfg.getVal("debug", "false").toLower()),
|
debug: "true".startsWith(cfg.getVal("debug", "false").toLower()),
|
||||||
port: parseInt(cfg.getVal("port", "8080")),
|
port: parseInt(cfg.getVal("port", "8080")),
|
||||||
pwdCost: cast[int8](parseInt(cfg.getVal("pwd-cost", "11"))))
|
pwdCost: cast[int8](parseInt(cfg.getVal("pwd-cost", "11"))),
|
||||||
|
knownOrigins: toSeq(knownOriginsArray).mapIt(it.getStr))
|
||||||
|
|
||||||
proc initContext(args: Table[string, docopt.Value]): PMApiContext =
|
proc initContext(args: Table[string, docopt.Value]): PMApiContext =
|
||||||
|
|
||||||
|
@ -32,10 +32,21 @@ template halt(code: HttpCode,
|
|||||||
result.matched = true
|
result.matched = true
|
||||||
break allRoutes
|
break allRoutes
|
||||||
|
|
||||||
template jsonResp(code: HttpCode, details: string = "", headers: RawHeaders = @{:} ) =
|
template jsonResp(code: HttpCode, details: string = "", headersToSend: RawHeaders = @{:} ) =
|
||||||
|
|
||||||
|
let reqOrigin = $(request.headers["Origin"])
|
||||||
|
let corsHeaders =
|
||||||
|
if ctx.cfg.knownOrigins.contains(reqOrigin):
|
||||||
|
@{
|
||||||
|
"Access-Control-Allow-Origin": reqOrigin,
|
||||||
|
"Access-Control-Allow-Credentials": "true",
|
||||||
|
"Access-Control-Allow-Methods": $(request.reqMeth)
|
||||||
|
}
|
||||||
|
else: @{:}
|
||||||
|
|
||||||
halt(
|
halt(
|
||||||
code,
|
code,
|
||||||
headers & @{
|
headersToSend & corsHeaders & @{
|
||||||
"Content-Type": JSON,
|
"Content-Type": JSON,
|
||||||
"Cache-Control": "no-cache"
|
"Cache-Control": "no-cache"
|
||||||
},
|
},
|
||||||
|
@ -9,6 +9,7 @@ type
|
|||||||
debug*: bool
|
debug*: bool
|
||||||
port*: int
|
port*: int
|
||||||
pwdCost*: int8
|
pwdCost*: int8
|
||||||
|
knownOrigins*: seq[string]
|
||||||
|
|
||||||
PMApiContext* = object
|
PMApiContext* = object
|
||||||
cfg*: PMApiConfig
|
cfg*: PMApiConfig
|
||||||
@ -23,7 +24,8 @@ proc `%`*(cfg: PMApiConfig): JsonNode =
|
|||||||
"dbConnString": cfg.dbConnString,
|
"dbConnString": cfg.dbConnString,
|
||||||
"debug": cfg.debug,
|
"debug": cfg.debug,
|
||||||
"port": cfg.port,
|
"port": cfg.port,
|
||||||
"pwdCost": cfg.pwdCost }
|
"pwdCost": cfg.pwdCost,
|
||||||
|
"knownOrigins": cfg.knownOrigins }
|
||||||
|
|
||||||
template raiseEx*(errorType: type, reason: string): void =
|
template raiseEx*(errorType: type, reason: string): void =
|
||||||
raise newException(errorType, reason)
|
raise newException(errorType, reason)
|
||||||
|
@ -1 +0,0 @@
|
|||||||
{"Modules":[{"Key":"","Source":"","Dir":"."},{"Key":"dev_env","Source":"./deployed_env","Dir":"deployed_env"},{"Key":"prod_env","Source":"./deployed_env","Dir":"deployed_env"}]}
|
|
@ -1,3 +0,0 @@
|
|||||||
{
|
|
||||||
"aws": "7b0461a9e1bb61eb2b58ee46d67d51a2f59d4b0fe6137025c82b4f5060477d2f"
|
|
||||||
}
|
|
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user