Skip to content

Commit

Permalink
bump
Browse files Browse the repository at this point in the history
  • Loading branch information
ringabout committed Jul 2, 2020
1 parent f0cb7ed commit 973c8db
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 143 deletions.
16 changes: 3 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,10 @@ app.run()

#### DSL for routes

Routes are equivalent to the example above:
You can read docs in https://nim-lang.github.io/Nim/with.html and construct your own DSL.

``` nim
app.route:
get post "/" home
get post "/home" home
get "/redirect" doRedirect
get "/login" login
post "/login" login debugRequestMiddleware()
get "/hello/{name}" helloName
```
Pseudocode:

Full examples in: [tests/test_readme/example3.nim](tests/test_readme/example3.nim).

or
```nim
import std/with
Expand All @@ -153,6 +142,7 @@ with app:
get "/login", login
post("/login", login, middlewares = debugRequestMiddleware())
get "/hello/{name}", helloName
addRoute [HttpGet, HttpPost], helloRoute
```

Run **app.nim**. Now the server is running at localhost:8080.
Expand Down
2 changes: 1 addition & 1 deletion prologue.nimble
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Package

version = "0.2.4"
version = "0.2.6"
author = "flywind"
description = "Full-Stack Web Framework."
license = "Apache-2.0"
Expand Down
2 changes: 0 additions & 2 deletions src/prologue.nim
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import prologue/core/application
import prologue/dsl/route_dsl
export route_dsl
export application


Expand Down
46 changes: 29 additions & 17 deletions src/prologue/core/application.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ from ./route import pattern, initPath, initRePath, newPathHandler, newRouter,
newReRouter, DuplicatedRouteError, DuplicatedReversedRouteError, UrlPattern,
add, `[]`, `[]=`, hasKey
from ./form import parseFormParams
from ./nativesettings import newSettings, newCtxSettings, getOrDefault, Settings
from ./nativesettings import newSettings, newCtxSettings,
getOrDefault, Settings, LocalSettings,
newLocalSettings
from ./httpexception import HttpError, AbortError

import ./dispatch
Expand Down Expand Up @@ -74,9 +76,15 @@ proc registerErrorHandler*(app: Prologue, code: openArray[HttpCode],
for idx in code:
app.registerErrorHandler(idx, handler)


proc newSettings*(settings: Settings, localSettings: LocalSettings): Settings {.inline.} =
result = newSettings(localSettings.data, settings.port, settings.debug, settings.reusePort,
settings.staticDirs, settings.appName)


proc addRoute*(app: Prologue, route: Regex, handler: HandlerAsync,
httpMethod = HttpGet, middlewares: seq[HandlerAsync] = @[],
settings: Settings = nil) {.inline.} =
settings: LocalSettings = nil) {.inline.} =
## add single handler route
## don't check whether regex routes are duplicated
# for group in route.namedGroups.keys:
Expand All @@ -88,7 +96,7 @@ proc addRoute*(app: Prologue, route: Regex, handler: HandlerAsync,

proc addRoute*(app: Prologue, route: Regex, handler: HandlerAsync,
httpMethod: seq[HttpMethod], middlewares: seq[HandlerAsync] = @[],
settings: Settings = nil) {.inline.} =
settings: LocalSettings = nil) {.inline.} =
for m in httpMethod:
app.addRoute(route, handler, m, middlewares, settings)

Expand All @@ -101,7 +109,7 @@ proc addReversedRoute(app: Prologue, name, route: string) {.inline.} =

proc addRoute*(app: Prologue, route: string, handler: HandlerAsync,
httpMethod = HttpGet, name = "", middlewares: seq[HandlerAsync] = @[],
settings: Settings = nil) {.inline.} =
settings: LocalSettings = nil) {.inline.} =
## add single handler route
## check whether routes are duplicated
let path = initPath(route = route, httpMethod = httpMethod)
Expand All @@ -111,63 +119,67 @@ proc addRoute*(app: Prologue, route: string, handler: HandlerAsync,

if app.gScope.router.hasKey(path):
raise newException(DuplicatedRouteError, fmt"Route {route} is duplicated!")
app.gScope.router[path] = newPathHandler(handler, middlewares, settings)
if settings == nil:
app.gScope.router[path] = newPathHandler(handler, middlewares, nil)
else:
app.gScope.router[path] = newPathHandler(handler, middlewares,
newSettings(app.gScope.settings, settings))
app.addReversedRoute(name, route)

proc addRoute*(app: Prologue, route: string, handler: HandlerAsync,
httpMethod: seq[HttpMethod], name = "",
middlewares: seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
## add single handler route with multi http method
## check whether routes are duplicated
app.addReversedRoute(name, route)
for m in httpMethod:
app.addRoute(route, handler, m, "", middlewares, settings)

proc addRoute*(app: Prologue, patterns: seq[UrlPattern],
baseRoute = "", settings: Settings = nil) {.inline.} =
baseRoute = "", settings: LocalSettings = nil) {.inline.} =
## add multi handler route
for pattern in patterns:
app.addRoute(baseRoute & pattern.route, pattern.matcher, pattern.httpMethod,
pattern.name, pattern.middlewares, settings)

proc head*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpHead, name, middlewares, settings)

proc get*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpGet, name, middlewares, settings)

proc post*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpPost, name, middlewares, settings)

proc put*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpPut, name, middlewares, settings)

proc delete*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpDelete, name, middlewares, settings)

proc trace*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpTrace, name, middlewares, settings)

proc options*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpOptions, name, middlewares, settings)

proc connect*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpConnect, name, middlewares, settings)

proc patch*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, HttpPatch, name, middlewares, settings)

proc all*(app: Prologue, route: string, handler: HandlerAsync, name = "",
middlewares: sink seq[HandlerAsync] = @[], settings: Settings = nil) {.inline.} =
middlewares: sink seq[HandlerAsync] = @[], settings: LocalSettings = nil) {.inline.} =
app.addRoute(route, handler, @[HttpGet, HttpPost, HttpPut, HttpDelete,
HttpTrace, HttpOptions, HttpConnect, HttpPatch], name, middlewares, settings)

Expand Down
2 changes: 1 addition & 1 deletion src/prologue/core/constants.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const
PrologueVersion* = "0.2.4"
PrologueVersion* = "0.2.6"
ProloguePrefix* = "PROLOGUE_"
2 changes: 1 addition & 1 deletion src/prologue/core/middlewaresbase.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ proc switch*(ctx: Context) {.async.} =
lastHandler = handler.handler
middlewares = handler.middlewares
ctx.localSettings = handler.settings
ctx.middlewares = ctx.middlewares & middlewares & lastHandler
ctx.middlewares.add middlewares & lastHandler
ctx.first = false
let next = ctx.middlewares[ctx.size - 1]
await next(ctx)
Expand Down
18 changes: 18 additions & 0 deletions src/prologue/core/nativesettings.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type
mimeDB*: MimeDB
config*: TableRef[string, StringTableRef]

LocalSettings* = ref object
data*: JsonNode


proc hasKey*(settings: Settings, key: string): bool {.inline.} =
settings.data.hasKey(key)
Expand All @@ -31,6 +34,13 @@ proc getOrDefault*(settings: Settings, key: string): JsonNode {.inline.} =
proc newCtxSettings*(): CtxSettings {.inline.} =
CtxSettings(mimeDB: newMimetypes(), config: newTable[string, StringTableRef]())

proc newLocalSettings*(data: JsonNode): LocalSettings {.inline.} =
result = LocalSettings(data: data)

proc newLocalSettings*(configPath: string): LocalSettings {.inline.} =
var data = parseFile(configPath)
result = LocalSettings(data: data)

proc newSettings*(port = Port(8080), debug = true, reusePort = true,
staticDirs: openArray[string] = ["static"], secretKey = randomString(8),
appName = ""): Settings {.inline.} =
Expand All @@ -41,6 +51,14 @@ proc newSettings*(port = Port(8080), debug = true, reusePort = true,
staticDirs: @staticDirs, appName: appName,
data: %* {"secretKey": secretKey})


proc newSettings*(data: JsonNode, port = Port(8080), debug = true, reusePort = true,
staticDirs: openArray[string] = ["static"],
appName = ""): Settings {.inline.} =
result = Settings(port: port, debug: debug, reusePort: reusePort,
staticDirs: @staticDirs, appName: appName,
data: data)

proc newSettings*(configPath: string, port = Port(8080), debug = true, reusePort = true,
staticDirs: openArray[string] = ["static"],
appName = ""): Settings {.inline.} =
Expand Down
67 changes: 0 additions & 67 deletions src/prologue/dsl/route_dsl.nim

This file was deleted.

34 changes: 0 additions & 34 deletions tests/test_readme/example3.nim

This file was deleted.

7 changes: 0 additions & 7 deletions tests/test_readme/readme.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ block:
let
e1 = "tests/test_readme/example1.nim"
e2 = "tests/test_readme/example2.nim"
e3 = "tests/test_readme/example3.nim"
execCommand = "nim c --gc:arc --d:release --hints:off"

# example1 can compile
Expand All @@ -18,9 +17,3 @@ block:
block:
let (outp, errC) = execCmdEx(fmt"{execCommand} {e2}")
doAssert errC == 0, outp

# example3 can compile
block:
let (outp, errC) = execCmdEx(fmt"{execCommand} {e3}")
doAssert errC == 0, outp

0 comments on commit 973c8db

Please sign in to comment.