Code cleanup in server.nim
This commit is contained in:
parent
9d00d638db
commit
e547ecd607
@ -13,7 +13,8 @@ type
|
|||||||
user*: UserRef
|
user*: UserRef
|
||||||
issuedAt*, expires*: Time
|
issuedAt*, expires*: Time
|
||||||
|
|
||||||
const ISO_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
|
#const ISO_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss"
|
||||||
|
const JSON = "application/json"
|
||||||
|
|
||||||
proc makeJsonResp(status: HttpCode, details: string = ""): string =
|
proc makeJsonResp(status: HttpCode, details: string = ""): string =
|
||||||
result = $(%* {
|
result = $(%* {
|
||||||
@ -106,21 +107,20 @@ proc makeAuthToken*(cfg: StrawBossConfig, uname, pwd: string): string =
|
|||||||
if not validatePwd(user, pwd): raiseEx "invalid username or password"
|
if not validatePwd(user, pwd): raiseEx "invalid username or password"
|
||||||
result = toJWT(cfg, newSession(user))
|
result = toJWT(cfg, newSession(user))
|
||||||
|
|
||||||
|
|
||||||
template withSession(body: untyped): untyped =
|
template withSession(body: untyped): untyped =
|
||||||
var session {.inject.}: Session
|
var session {.inject.}: Session
|
||||||
var authed = false
|
var authed = false
|
||||||
|
|
||||||
try:
|
try:
|
||||||
session = extractSession(givenCfg, request)
|
session = extractSession(cfg, request)
|
||||||
authed = true
|
authed = true
|
||||||
except:
|
except:
|
||||||
debug "Auth failed: " & getCurrentExceptionMsg()
|
debug "Auth failed: " & getCurrentExceptionMsg()
|
||||||
resp(Http401, makeJsonResp(Http401), "application/json")
|
resp(Http401, makeJsonResp(Http401), JSON)
|
||||||
|
|
||||||
if authed: body
|
if authed: body
|
||||||
|
|
||||||
proc start*(givenCfg: StrawBossConfig): void =
|
proc start*(cfg: StrawBossConfig): void =
|
||||||
|
|
||||||
let stopFuture = newFuture[void]()
|
let stopFuture = newFuture[void]()
|
||||||
var workers: seq[Worker] = @[]
|
var workers: seq[Worker] = @[]
|
||||||
@ -131,24 +131,22 @@ proc start*(givenCfg: StrawBossConfig): void =
|
|||||||
|
|
||||||
routes:
|
routes:
|
||||||
|
|
||||||
get "/ping":
|
get "/ping": resp($(%*"pong"), JSON)
|
||||||
resp($(%*"pong"), "application/json")
|
|
||||||
|
|
||||||
get "/auth-token":
|
get "/auth-token":
|
||||||
try:
|
try:
|
||||||
let authToken = makeAuthToken(givenCfg, @"username", @"password")
|
let authToken = makeAuthToken(cfg, @"username", @"password")
|
||||||
resp("\"" & $authToken & "\"", "application/json")
|
resp("\"" & $authToken & "\"", JSON)
|
||||||
except: resp(Http401, makeJsonResp(Http401, getCurrentExceptionMsg()))
|
except: resp(Http401, makeJsonResp(Http401, getCurrentExceptionMsg()), JSON)
|
||||||
|
|
||||||
get "/verify-auth": withSession:
|
get "/verify-auth": withSession:
|
||||||
resp(Http200, $(%*{
|
resp(Http200, $(%*{ "username": session.user.name }), JSON)
|
||||||
"username": session.user.name
|
|
||||||
}), "application/json")
|
|
||||||
|
|
||||||
get "/projects": withSession:
|
get "/projects": withSession:
|
||||||
resp($(%(givenCfg.projects)), "application/json")
|
resp($(%(givenCfg.projects)), "application/json")
|
||||||
|
|
||||||
post "/project/@projectName/@stepName/run/@buildRef?":
|
post "/project/@projectName/@stepName/run/@buildRef?":
|
||||||
|
# Kick off a run
|
||||||
workers.add(spawnWorker(RunRequest(
|
workers.add(spawnWorker(RunRequest(
|
||||||
projectName: @"projectName",
|
projectName: @"projectName",
|
||||||
stepName: @"stepName",
|
stepName: @"stepName",
|
||||||
@ -156,9 +154,9 @@ proc start*(givenCfg: StrawBossConfig): void =
|
|||||||
forceRebuild: false))) # TODO support this with optional query params
|
forceRebuild: false))) # TODO support this with optional query params
|
||||||
|
|
||||||
post "/service/debug/stop":
|
post "/service/debug/stop":
|
||||||
if not givenCfg.debug: resp(Http404, makeJsonResp(Http404), "application/json")
|
if not cfg.debug: resp(Http404, makeJsonResp(Http404), JSON)
|
||||||
else:
|
else:
|
||||||
callSoon(proc(): void = complete(stopFuture))
|
callSoon(proc(): void = complete(stopFuture))
|
||||||
resp($(%*"shutting down"), "application/json")
|
resp($(%*"shutting down"), JSON)
|
||||||
|
|
||||||
waitFor(stopFuture)
|
waitFor(stopFuture)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user