Skip to content

Commit 19bbf12

Browse files
committed
refactor: more cleanup and moving stuff around
1 parent 6c50082 commit 19bbf12

File tree

13 files changed

+262
-254
lines changed

13 files changed

+262
-254
lines changed

lightbug.🔥

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from lightbug_http import *
2+
from lightbug_http.middleware.helpers import Success
23

34
struct HelloWorld(HTTPHandler):
45
fn handle(self, context: Context) -> HTTPResponse:

lightbug_http/middleware.mojo

Lines changed: 0 additions & 254 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from lightbug_http.middleware.middleware import Context, Middleware, MiddlewareChain
2+
3+
from lightbug_http.middleware.basicauth import BasicAuthMiddleware
4+
from lightbug_http.middleware.compression import CompressionMiddleware
5+
from lightbug_http.middleware.cors import CorsMiddleware
6+
from lightbug_http.middleware.error import ErrorMiddleware
7+
from lightbug_http.middleware.logger import LoggerMiddleware
8+
from lightbug_http.middleware.notfound import NotFoundMiddleware
9+
from lightbug_http.middleware.router import RouterMiddleware, HTTPHandler
10+
from lightbug_http.middleware.static import StaticMiddleware
11+
12+
# from lightbug_http.middleware.csrf import CsrfMiddleware
13+
# from lightbug_http.middleware.session import SessionMiddleware
14+
# from lightbug_http.middleware.websocket import WebSocketMiddleware
15+
# from lightbug_http.middleware.cache import CacheMiddleware
16+
# from lightbug_http.middleware.cookies import CookiesMiddleware
17+
# from lightbug_http.middleware.session import SessionMiddleware
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from lightbug_http.middleware.helpers import Unauthorized
2+
3+
## BasicAuth middleware requires basic authentication to access the route.
4+
@value
5+
struct BasicAuthMiddleware(Middleware):
6+
var next: Middleware
7+
var username: String
8+
var password: String
9+
10+
fn set_next(self, next: Middleware):
11+
self.next = next
12+
13+
fn __init__(self, username: String, password: String):
14+
self.username = username
15+
self.password = password
16+
17+
fn call(self, context: Context) -> HTTPResponse:
18+
var request = context.request
19+
var auth = request.headers["Authorization"]
20+
if auth == f"Basic {username}:{password}":
21+
context.params["username"] = username
22+
return next.call(context)
23+
else:
24+
return Unauthorized("Requires Basic Authentication")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## Compression middleware compresses the response body.
2+
@value
3+
struct CompressionMiddleware(Middleware):
4+
var next: Middleware
5+
6+
fn set_next(self, next: Middleware):
7+
self.next = next
8+
9+
fn call(self, context: Context) -> HTTPResponse:
10+
var response = self.next.call(context)
11+
response.body = self.compress(response.body)
12+
return response
13+
14+
fn compress(self, body: Bytes) -> Bytes:
15+
#TODO: implement compression
16+
return body
17+

lightbug_http/middleware/cors.mojo

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from lightbug_http.middleware.helpers import Unauthorized
2+
3+
## CORS middleware adds the necessary headers to allow cross-origin requests.
4+
@value
5+
struct CorsMiddleware(Middleware):
6+
var next: Middleware
7+
var allow_origin: String
8+
9+
fn set_next(self, next: Middleware):
10+
self.next = next
11+
12+
fn __init__(self, allow_origin: String):
13+
self.allow_origin = allow_origin
14+
15+
fn call(self, context: Context) -> HTTPResponse:
16+
if context.request.header.method() == "OPTIONS":
17+
var response = next.call(context)
18+
response.headers["Access-Control-Allow-Origin"] = allow_origin
19+
response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS"
20+
response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization"
21+
return response
22+
23+
if context.request.origin == allow_origin:
24+
return self.next.call(context)
25+
else:
26+
return Unauthorized("CORS not allowed")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from lightbug_http.middleware.helpers import InternalServerError
2+
3+
## Error handler will catch any exceptions thrown by the other
4+
## middleware and return a 500 response.
5+
## It should be the first middleware in the chain.
6+
@value
7+
struct ErrorMiddleware(Middleware):
8+
var next: Middleware
9+
10+
fn set_next(self, next: Middleware):
11+
self.next = next
12+
13+
fn call(inout self, context: Context) -> HTTPResponse:
14+
try:
15+
return self.next.call(context)
16+
except e:
17+
return InternalServerError(e)
18+

0 commit comments

Comments
 (0)