Commit 1fcad6e3 authored by Давид Ли's avatar Давид Ли

lesson 38

parent c9d1f438
class BaseController:
def __init__(self, request, response):
self.request = request
self.response = response
from .base import BaseController
class PagesController(BaseController):
def home(self):
self.response.add_header('Content-Type', 'text/html')
self.response.set_body('<h1>This is homepage</h1>')
def about(self):
self.response.add_header('Content-Type', 'text/html')
self.response.set_body('<h1>This is about</h1>')
from .base import BaseController
class PostsController(BaseController):
def get_list(self):
posts_bd = [
{'id': 1, 'title': 'This is 1st post'},
{'id': 2, 'title': 'This is 2st post'},
{'id': 3, 'title': 'This is 3st post'},
]
body = '<h1>This is our posts</h1>'
for post in posts_bd:
body += f'<h3>{post["id"]} - {post["title"]}'
self.response.add_header('Content-Type', 'text/html')
self.response.set_body(body)
from response import Response
from request import Request
def not_found(request, response):
response.set_status(Response.HTTP_NOT_FOUND)
response.add_header('Content-Type', 'text/html')
response.set_body('<h1>404 Not Found</h1>')
def internal_server_error(request, response):
response.set_status(Response.HTTP_INTERNAL_SERVER_ERROR)
response.add_header('Content-Type', 'text/html')
response.set_body('<h1>500 INTERNAL_SERVER_ERROR</h1>')
from request import Request
from response import Response
import errors
from static_responder import StaticResponder
import socketserver
from controllers.pages import PagesController
from controllers.posts import PostsController
from errors import not_found
from errors import not_found
from router import Router
#REST RESTfull
#CRUDL - Create, Read, Update, Delete, List
#/posts
#GET - /posts
#GET - /posts/{id}
#POST - /posts
#PUT\PATH - /posts/{id}
#DELETE - /posts/{id}
router = Router()
router.get('/', PagesController, 'home')
router.get('/about', PagesController, 'about')
router.get('/posts', PostsController, 'get_list')
class Handler(socketserver.StreamRequestHandler):
# self.request - client socket
......@@ -11,17 +35,12 @@ class Handler(socketserver.StreamRequestHandler):
def handle(self):
request = Request(self.rfile)
response = Response(self.wfile)
static_responder = StaticResponder(request, response)
response.add_header('Connection', 'close')
# static_responder = StaticResponder(request, response)
print(request)
if static_responder.file:
static_responder.prepare_response()
else:
response.add_header('Content-Type', 'text/html')
response.add_header('Connection', 'close')
response.set_body('<h1>Hello World!</h1>')
router.run(request, response)
response.send()
......
......@@ -23,6 +23,9 @@ class Response:
self.body = None
self.file_body = None
def set_status(self, new_status: int) -> None:
self.status = new_status
def add_header(self, name, value):
self.headers[name] = value
......
# router = Router()
#
# router.get('/', PagesController, 'home')
#
# router.post('/posts', PostController, 'create')
#
# router.run()
from errors import not_found, internal_server_error
class Router:
def __init__(self):
self.routes = {
'get': [],
'post': []
}
def add(self, http_method: str, uri: str, ctrl, ctrl_method):
self.routes[http_method].append({
'uri': uri,
'ctrl': ctrl,
'ctrl_method': ctrl_method
})
def get(self, *args):
self.add('get', *args)
def post(self, *args):
self.add('post', *args)
def run(self, request, response):
http_method = request.method.lower()
method_routes = self.routes[http_method]
route = None
for r in method_routes:
if r['uri'] == request.uri:
route = r
break
if not route:
return not_found(request, response)
try:
ctrl = route['ctrl'](request, response)
getattr(ctrl, route['ctrl_method'])()
except BaseException:
return internal_server_error(request, response)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment