|
| 1 | +from lightbug_http.http import HTTPRequest, HTTPResponse |
| 2 | + |
1 | 3 | struct Context: |
2 | 4 | var request: Request |
3 | 5 | var params: Dict[String, AnyType] |
4 | 6 |
|
5 | | - func __init__(request: Request): |
| 7 | + fn __init__(self, request: Request): |
6 | 8 | self.request = request |
7 | 9 | self.params = Dict[String, AnyType]() |
8 | 10 |
|
9 | 11 | trait Middleware: |
10 | 12 | var next: Middleware |
11 | 13 |
|
12 | | - func call(context: Context) -> Response: |
| 14 | + fn call(self, context: Context) -> Response: |
13 | 15 | ... |
14 | 16 |
|
15 | 17 | struct ErrorMiddleware(Middleware): |
16 | | - func call(context: Context) -> Response: |
17 | | - do: |
| 18 | + fn call(self, context: Context) -> Response: |
| 19 | + try: |
18 | 20 | return next.call(context: context) |
19 | 21 | catch e: Exception: |
20 | 22 | return InternalServerError() |
21 | 23 |
|
22 | 24 | struct LoggerMiddleware(Middleware): |
23 | | - func call(context: Context) -> Response: |
| 25 | + fn call(self, context: Context) -> Response: |
24 | 26 | print("Request: \(context.request)") |
25 | 27 | return next.call(context: context) |
26 | 28 |
|
27 | | -struct RouterMiddleware(Middleware): |
28 | | - var routes: Dict[String, Middleware] |
29 | | - |
30 | | - func __init__(): |
31 | | - self.routes = Dict[String, Middleware]() |
32 | | - |
33 | | - func add(route: String, middleware: Middleware): |
34 | | - routes[route] = middleware |
35 | | - |
36 | | - func call(context: Context) -> Response: |
37 | | - # TODO: create a more advanced router |
38 | | - |
39 | | - var route = context.request.path |
40 | | - if middleware = routes[route]: |
41 | | - return middleware.call(context: context) |
42 | | - else: |
43 | | - return NotFound() |
44 | | - |
45 | 29 | struct StaticMiddleware(Middleware): |
46 | 30 | var path: String |
47 | 31 |
|
48 | | - funct __init__(path: String): |
| 32 | + fnt __init__(self, path: String): |
49 | 33 | self.path = path |
50 | 34 |
|
51 | | - func call(context: Context) -> Response: |
52 | | - var file = File(path: path + context.request.path) |
| 35 | + fn call(self, context: Context) -> Response: |
| 36 | + if context.request.path == "/": |
| 37 | + var file = File(path: path + "index.html") |
| 38 | + else: |
| 39 | + var file = File(path: path + context.request.path) |
| 40 | + |
53 | 41 | if file.exists: |
54 | | - return FileResponse(file: file) |
| 42 | + var html: String |
| 43 | + with open(file, "r") as f: |
| 44 | + html = f.read() |
| 45 | + return OK(html.as_bytes(), "text/html") |
55 | 46 | else: |
56 | 47 | return next.call(context: context) |
57 | 48 |
|
58 | 49 | struct CorsMiddleware(Middleware): |
59 | | - func call(context: Context) -> Response: |
60 | | - var response = next.call(context: context) |
61 | | - response.headers["Access-Control-Allow-Origin"] = "*" |
62 | | - return response |
| 50 | + var allow_origin: String |
| 51 | + |
| 52 | + fn __init__(self, allow_origin: String): |
| 53 | + self.allow_origin = allow_origin |
| 54 | + |
| 55 | + fn call(self, context: Context) -> Response: |
| 56 | + if context.request.method == "OPTIONS": |
| 57 | + var response = next.call(context: context) |
| 58 | + response.headers["Access-Control-Allow-Origin"] = allow_origin |
| 59 | + response.headers["Access-Control-Allow-Methods"] = "GET, POST, PUT, DELETE, OPTIONS" |
| 60 | + response.headers["Access-Control-Allow-Headers"] = "Content-Type, Authorization" |
| 61 | + return response |
| 62 | + |
| 63 | + if context.request.origin == allow_origin: |
| 64 | + return next.call(context: context) |
| 65 | + else: |
| 66 | + return Unauthorized() |
63 | 67 |
|
64 | 68 | struct CompressionMiddleware(Middleware): |
65 | | - func call(context: Context) -> Response: |
| 69 | + fn call(self, context: Context) -> Response: |
66 | 70 | var response = next.call(context: context) |
67 | 71 | response.body = compress(response.body) |
68 | 72 | return response |
69 | 73 |
|
70 | | -struct SessionMiddleware(Middleware): |
71 | | - var session: Session |
| 74 | + fn compress(self, body: Bytes) -> Bytes: |
| 75 | + #TODO: implement compression |
| 76 | + return body |
72 | 77 |
|
73 | | - func call(context: Context) -> Response: |
74 | | - var request = context.request |
75 | | - var response = context.response |
76 | | - var session = session.load(request) |
77 | | - context.params["session"] = session |
78 | | - response = next.call(context: context) |
79 | | - session.save(response) |
80 | | - return response |
| 78 | + |
| 79 | +struct RouterMiddleware(Middleware): |
| 80 | + var routes: Dict[String, Middleware] |
| 81 | + |
| 82 | + fn __init__(self): |
| 83 | + self.routes = Dict[String, Middleware]() |
| 84 | + |
| 85 | + fn add(self, method: String, route: String, middleware: Middleware): |
| 86 | + routes[method + ":" + route] = middleware |
| 87 | + |
| 88 | + fn call(self, context: Context) -> Response: |
| 89 | + # TODO: create a more advanced router |
| 90 | + var method = context.request.method |
| 91 | + var route = context.request.path |
| 92 | + if middleware = routes[method + ":" + route]: |
| 93 | + return middleware.call(context: context) |
| 94 | + else: |
| 95 | + return next.call(context: context) |
81 | 96 |
|
82 | 97 | struct BasicAuthMiddleware(Middleware): |
83 | 98 | var username: String |
84 | 99 | var password: String |
85 | 100 |
|
86 | | - func __init__(username: String, password: String): |
| 101 | + fn __init__(self, username: String, password: String): |
87 | 102 | self.username = username |
88 | 103 | self.password = password |
89 | 104 |
|
90 | | - func call(context: Context) -> Response: |
| 105 | + fn call(self, context: Context) -> Response: |
91 | 106 | var request = context.request |
92 | 107 | var auth = request.headers["Authorization"] |
93 | 108 | if auth == "Basic \(username):\(password)": |
| 109 | + context.params["username"] = username |
94 | 110 | return next.call(context: context) |
95 | 111 | else: |
96 | 112 | return Unauthorized() |
97 | 113 |
|
98 | | -struct MiddlewareChain: |
| 114 | +# always add at the end of the middleware chain |
| 115 | +struct NotFoundMiddleware(Middleware): |
| 116 | + fn call(self, context: Context) -> Response: |
| 117 | + return NotFound() |
| 118 | + |
| 119 | +struct MiddlewareChain(HttpService): |
99 | 120 | var middlewares: Array[Middleware] |
100 | 121 |
|
101 | | - func __init__(): |
| 122 | + fn __init__(self): |
102 | 123 | self.middlewares = Array[Middleware]() |
103 | 124 |
|
104 | | - func add(middleware: Middleware): |
| 125 | + fn add(self, middleware: Middleware): |
105 | 126 | if middlewares.count == 0: |
106 | 127 | middlewares.append(middleware) |
107 | 128 | else: |
108 | 129 | var last = middlewares[middlewares.count - 1] |
109 | 130 | last.next = middleware |
110 | 131 | middlewares.append(middleware) |
111 | 132 |
|
112 | | - func execute(request: Request) -> Response: |
| 133 | + fn func(self, request: Request) -> Response: |
| 134 | + self.add(NotFoundMiddleware()) |
113 | 135 | var context = Context(request: request, response: response) |
114 | | - if middlewares.count > 0: |
115 | | - return middlewares[0].call(context: context) |
116 | | - else: |
117 | | - return NotFound() |
| 136 | + return middlewares[0].call(context: context) |
118 | 137 |
|
119 | 138 | fn OK(body: Bytes) -> HTTPResponse: |
120 | 139 | return OK(body, String("text/plain")) |
|
0 commit comments