Skip to content

Commit

Permalink
Merge pull request #102 from guzba/ryan
Browse files Browse the repository at this point in the history
little things
  • Loading branch information
guzba authored Dec 26, 2023
2 parents 292514e + 625023b commit d01d5dd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
19 changes: 9 additions & 10 deletions examples/basic_notfound.nim → examples/basic_custom_404.nim
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
##
## How to handle not found requests. Same concept can be used for
## `methodNotAllowedHandler` and `errorHandler` (found in `mummy/routers`).
##

import mummy, mummy/routers

proc customNotFound(request: Request) =
## This example shows how to return a custom response for requests
## that do not have a matching route (return a 404).
## The same idea can be used for `methodNotAllowedHandler` (405) and
## `errorHandler` (500) responses.

proc custom404(request: Request) =
## This is a custom 404 handler
const body = "<h1>I'm not here</h1>"

var headers: httpheaders.HttpHeaders
var headers: HttpHeaders
headers["Content-Type"] = "text/html"

if request.httpMethod == "HEAD":
Expand All @@ -18,16 +18,15 @@ proc customNotFound(request: Request) =
else:
request.respond(404, headers, body)


proc indexHandler(request: Request) =
var headers: HttpHeaders
headers["Content-Type"] = "text/plain"
request.respond(200, headers, "Hello, World!")

var router: Router

# Custom not found handler
router.notFoundHandler = customNotFound
# Custom 404 handler
router.notFoundHandler = custom404

# Normal routes
router.get("/", indexHandler)
Expand Down
6 changes: 3 additions & 3 deletions examples/basic_database.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import mummy, mummy/routers, std/strutils, waterpark/sqlite
let db = newSqlitePool(10, "example.sqlite3")

# For example purposes, set up a dummy table
db.withConnnection conn:
db.withConnection conn:
conn.exec(sql"create table if not exists table1(id primary key, count int)")
conn.exec(sql"insert or replace into table1 values (0, 0)")

# A request to /get will return the count
proc getHandler(request: Request) =
var count: int
db.withConnnection conn:
db.withConnection conn:
count = parseInt(conn.getValue(sql"select count from table1 limit 1"))

var headers: HttpHeaders
Expand All @@ -25,7 +25,7 @@ proc getHandler(request: Request) =

# A request to /inc will increase the count by 1
proc incHandler(request: Request) =
db.withConnnection conn:
db.withConnection conn:
conn.exec(sql"update table1 set count = count + 1")

var headers: HttpHeaders
Expand Down
5 changes: 1 addition & 4 deletions src/mummy/routers.nim
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,12 @@ proc pathParts(uri: string): seq[string] =
# The URI path is assumed to end at the first ? & #
var
a = uri.find('?')
b = uri.find('&')
c = uri.find('#')
b = uri.find('#')
var len = uri.len
if a != -1:
len = min(len, a)
if b != -1:
len = min(len, b)
if c != -1:
len = min(len, c)

if len != uri.len:
result = uri[0 ..< len].split('/')
Expand Down

0 comments on commit d01d5dd

Please sign in to comment.