diff --git a/__pycache__/bookdb.cpython-39.pyc b/__pycache__/bookdb.cpython-39.pyc
new file mode 100644
index 0000000..3dc7183
Binary files /dev/null and b/__pycache__/bookdb.cpython-39.pyc differ
diff --git a/bookapp.py b/bookapp.py
index d2284c6..8d89c23 100644
--- a/bookapp.py
+++ b/bookapp.py
@@ -1,4 +1,5 @@
import re
+import traceback
from bookdb import BookDB
@@ -6,18 +7,75 @@
def book(book_id):
- return "
a book with id %s
" % book_id
+ #return "A book with id {}
".format(book_id)
+ page = """
+{title}
+
+ | Author | {author} |
+ | Publisher | {publisher} |
+ | ISBN | {isbn} |
+
+Back to the list
+ """
+
+ book = DB.title_info(book_id)
+ if book is None:
+ raise NameError
+ return page.format(**book)
def books():
- return "a list of books
"
+ #return "A list of books
"
+ all_books = DB.titles()
+ body = ['My Bookshelf
', '']
+
+ item_template = '- {title}
'
+ for book in all_books:
+ body.append(item_template.format(**book))
+ body.append('
')
+
+ return '\n'.join(body)
def application(environ, start_response):
- status = "200 OK"
+ #status = "200 OK"
headers = [('Content-type', 'text/html')]
- start_response(status, headers)
- return ["No Progress Yet
".encode('utf8')]
+ try:
+ path = environ.get('PATH_INFO', None)
+ if path is None:
+ raise NameError
+ func, args = resolve_path(path)
+ body = func(*args)
+ status = "200 ok"
+ except NameError:
+ status = "404 Not Found"
+ body = " Not Found
"
+ except Exception:
+ status = "500 Internal Server Error"
+ body = "Internal Server Error
"
+ print(traceback.format_exc())
+ finally:
+ headers.append(('Content-length', str(len(body))))
+ start_response(status, headers)
+ return [body.encode('utf8')]
+
+
+def resolve_path(path):
+ funcs = {
+ "": books,
+ 'book': book,
+ }
+ path = path.strip('/').split('/')
+
+ func_name = path[0]
+ args = path[1:]
+
+ try:
+ func = funcs[func_name]
+ except KeyError:
+ raise NameError
+
+ return func, args
if __name__ == '__main__':
diff --git a/wsgi_1.py b/wsgi_1.py
index 85498d1..59ac5ed 100644
--- a/wsgi_1.py
+++ b/wsgi_1.py
@@ -14,6 +14,8 @@