Skip to content

Commit

Permalink
add -d:disableColors
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Sep 3, 2024
1 parent 125b303 commit 2c9a375
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 31 deletions.
10 changes: 8 additions & 2 deletions src/happyx/core/constants.nim
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const
enableHistoryApi* = defined(historyApi) or defined(hpxHistoryApi) or defined(happyxHistoryApi)
enableDebug* = defined(debug) or defined(happyxDebug) or defined(hpxDebug) or exportJvm or exportPython or defined(napibuild)
enableApiDoc* = not defined(disableApiDoc)
enableColors* = not defined(disableColors) or not defined(happyxDisableColors) or not defined(hpxDisableColors)
numThreads* {. intdefine .} = 0
sessionIdLength* {.intdefine.} = 32
appName* {.strdefine.} = "HappyX Application"
Expand Down Expand Up @@ -105,7 +106,7 @@ const

when cryptoMethod notin availableCryptoMethods or (enableDebug and not defined(js)):
import strformat
when not defined(js) and enableDebug:
when not defined(js) and enableDebug and enableColors:
import terminal


Expand All @@ -126,8 +127,13 @@ when int(enableHttpx) + int(enableMicro) + int(enableHttpBeast) > 1:


when enableDebug:
when not defined(js):
when not defined(js) and enableColors:
styledEcho fgYellow, fmt"Enable auto translate: {enableAutoTranslate}"
styledEcho fgYellow, fmt"Enable httpbeast: {enableHttpBeast}"
styledEcho fgYellow, fmt"Enable httpx: {enableHttpx}"
styledEcho fgYellow, fmt"Enable MicroAsyncHttpServer: {enableMicro}"
elif not enableColors:
echo fmt"Enable auto translate: {enableAutoTranslate}"
echo fmt"Enable httpbeast: {enableHttpBeast}"
echo fmt"Enable httpx: {enableHttpx}"
echo fmt"Enable MicroAsyncHttpServer: {enableMicro}"
89 changes: 63 additions & 26 deletions src/happyx/ssr/server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,26 @@ import
checksums/md5,
# HappyX
./[cors, types, utils, websockets],
./liveviews/[utils, liveviews],
../spa/[tag, renderer],
../core/[exceptions, constants],
../private/[macro_utils],
../routing/[routing, mounting, decorators],
../spa/tag,
../routing/[routing, mounting],
../sugar/sgr


when enableLiveViews:
import
../spa/renderer,
./liveviews/[utils, liveviews]

export
liveviews


when enableDefaultDecorators:
import ../routing/decorators


export
strutils,
strtabs,
Expand All @@ -89,8 +102,7 @@ export
utils,
json,
os,
types,
liveviews
types


when enableApiDoc:
Expand Down Expand Up @@ -152,22 +164,36 @@ proc newServer*(address: string = "127.0.0.1", port: int = 5000): Server =
port: port,
components: newTable[string, BaseComponent](),
logger:
if loggerCreated:
newConsoleLogger(lvlNone, fgColored("[$date at $time]:$levelname ", fgYellow))
when enableColors:
if loggerCreated:
newConsoleLogger(lvlNone, fgColored("[$date at $time]:$levelname ", fgYellow))
else:
loggerCreated = true
newConsoleLogger(lvlInfo, fgColored("[$date at $time]:$levelname ", fgYellow))
else:
loggerCreated = true
newConsoleLogger(lvlInfo, fgColored("[$date at $time]:$levelname ", fgYellow))
if loggerCreated:
newConsoleLogger(lvlNone, "[$date at $time]:$levelname ")
else:
loggerCreated = true
newConsoleLogger(lvlInfo, "[$date at $time]:$levelname ")
)
else:
result = Server(
address: address,
port: port,
logger:
if loggerCreated:
newConsoleLogger(lvlNone, fgColored("[$date at $time]:$levelname ", fgYellow))
when enableColors:
if loggerCreated:
newConsoleLogger(lvlNone, fgColored("[$date at $time]:$levelname ", fgYellow))
else:
loggerCreated = true
newConsoleLogger(lvlInfo, fgColored("[$date at $time]:$levelname ", fgYellow))
else:
loggerCreated = true
newConsoleLogger(lvlInfo, fgColored("[$date at $time]:$levelname ", fgYellow))
if loggerCreated:
newConsoleLogger(lvlNone, "[$date at $time]:$levelname ")
else:
loggerCreated = true
newConsoleLogger(lvlInfo, "[$date at $time]:$levelname ")
)
when enableHttpx or enableHttpBeast:
result.instance = initSettings(Port(port), bindAddr=address, numThreads = numThreads)
Expand Down Expand Up @@ -610,7 +636,8 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
finalize = newStmtList()
setup = newStmtList()

body.handleLiveViews()
when enableLiveViews:
body.handleLiveViews()

when enableHttpx or enableHttpBeast:
var path = newCall("decodeUrl", newNimNode(nnkBracketExpr).add(
Expand Down Expand Up @@ -730,8 +757,9 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
# "/...": statement list
elif statement[1].kind == nnkStmtList and statement[0].kind == nnkStrLit:
detectReturnStmt(statement[1])
for route in nextRouteDecorators:
decorators[route.name](@["GET"], $statement[0], statement[1], route.args)
when enableDefaultDecorators:
for route in nextRouteDecorators:
decorators[route.name](@["GET"], $statement[0], statement[1], route.args)
let exported = exportRouteArgs(pathIdent, statement[0], statement[1])
if exported.len > 0: # /my/path/with{custom:int}/{param:path}
methodTable.mgetOrPut("GET", newNimNode(nnkIfStmt)).add(exported)
Expand All @@ -746,8 +774,9 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
var httpMethods: seq[string] = @[]
for i in statement[0]:
httpMethods.add($i)
for route in nextRouteDecorators:
decorators[route.name](httpMethods, $statement[0], statement[1], route.args)
when enableDefaultDecorators:
for route in nextRouteDecorators:
decorators[route.name](httpMethods, $statement[0], statement[1], route.args)
let exported = exportRouteArgs(pathIdent, statement[1], statement[2])
var methods = newNimNode(nnkBracket)
for i in statement[0]:
Expand Down Expand Up @@ -779,8 +808,9 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
let
name = ($statement[0]).toUpper()
slash = newLit"/"
for route in nextRouteDecorators:
decorators[route.name](@[$statement[0]], $statement[1], statement[2], route.args)
when enableDefaultDecorators:
for route in nextRouteDecorators:
decorators[route.name](@[$statement[0]], $statement[1], statement[2], route.args)
if name == "STATICDIR":
# Just path
var
Expand Down Expand Up @@ -1142,10 +1172,13 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
elseStmtList.add(
newCall(
"warn",
newCall(
"fgColored",
newCall("fmt", newLit"{urlPath} is not found."), ident"fgYellow"
)
when enableColors:
newCall(
"fgColored",
newCall("fmt", newLit"{urlPath} is not found."), ident"fgYellow"
)
else:
newCall("fmt", newLit"{urlPath} is not found.")
)
)
elseStmtList.add(
Expand Down Expand Up @@ -1253,8 +1286,12 @@ macro routes*(server: Server, body: untyped = newStmtList()): untyped =
immutableVars.add(newIdentDefs(ident"reqMethod", newEmptyNode(), reqMethod))
if stmtList.isIdentUsed(ident"headers"):
immutableVars.add(newIdentDefs(ident"headers", newEmptyNode(), headers))
if stmtList.isIdentUsed(ident"startSession") or stmtList.isIdentUsed(ident"hostname") or liveViewsCache.len > 0:
immutableVars.add(newIdentDefs(ident"hostname", newEmptyNode(), hostname))
when enableLiveViews:
if stmtList.isIdentUsed(ident"startSession") or stmtList.isIdentUsed(ident"hostname") or liveViewsCache.len > 0:
immutableVars.add(newIdentDefs(ident"hostname", newEmptyNode(), hostname))
else:
if stmtList.isIdentUsed(ident"startSession") or stmtList.isIdentUsed(ident"hostname"):
immutableVars.add(newIdentDefs(ident"hostname", newEmptyNode(), hostname))
when enableDebugSsrMacro:
echo result.toStrLit

Expand Down
7 changes: 4 additions & 3 deletions src/happyx/ssr/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import
std/json,
std/httpcore,
std/options,
std/strtabs
std/strtabs,
../core/constants

when not defined(js):
when not defined(js) and enableColors:
import std/terminal
export terminal

Expand Down Expand Up @@ -48,7 +49,7 @@ proc toHttpHeaders*(json: JsonNode): HttpHeaders =
result[k] = v.getStr


when not defined(js):
when not defined(js) and enableColors:
func fgColored*(text: string, clr: ForegroundColor): string {.inline.} =
## This function takes in a string of text and a ForegroundColor enum
## value and returns the same text with the specified color applied.
Expand Down

0 comments on commit 2c9a375

Please sign in to comment.