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