diff --git a/src/crankypants/web.cr b/src/crankypants/web.cr index 4f6ff10..2d5297f 100644 --- a/src/crankypants/web.cr +++ b/src/crankypants/web.cr @@ -1,7 +1,7 @@ require "http/server" require "colorize" require "./version" -require "./web/router" +require "./web/routers/*" module Crankypants module Web @@ -35,7 +35,10 @@ module Crankypants HTTP::ErrorHandler.new, HTTP::LogHandler.new, HTTP::StaticFileHandler.new("./public/", directory_listing: false), - Crappy::Handler(Router).new, + Crappy::Handler(Routers::Blog).new, + Crappy::Handler(Routers::Assets).new, + Crappy::Handler(Routers::Api).new, + Crappy::Handler(Routers::App).new, ]).listen end end diff --git a/src/crankypants/web/helpers.cr b/src/crankypants/web/helpers.cr index f989140..f243208 100644 --- a/src/crankypants/web/helpers.cr +++ b/src/crankypants/web/helpers.cr @@ -1,3 +1,6 @@ +require "kilt" +require "kilt/slang" + require "./assets" module Crankypants diff --git a/src/crankypants/web/router.cr b/src/crankypants/web/routers/api.cr similarity index 54% rename from src/crankypants/web/router.cr rename to src/crankypants/web/routers/api.cr index e430698..35abbc8 100644 --- a/src/crankypants/web/router.cr +++ b/src/crankypants/web/routers/api.cr @@ -1,54 +1,18 @@ -# Third party libraries -require "kilt" -require "kilt/slang" - -# Crappy require "../../../crappy" require "../../../crappy/authentication" -# Our own stuff -require "../formatter" -require "../models/*" -require "../data" -require "./views/*" -require "./helpers" -require "./input_mappings" +require "../input_mappings" +require "../helpers" +require "../../models/*" +require "../../data" +require "../views/*" -module Crankypants::Web - class Router < Crappy::Router +module Crankypants::Web::Routers + class Api < Crappy::Router include Helpers include Crappy::Authentication def call - serve_static_assets || serve_blog || serve_api || serve_app - end - - def serve_static_assets - {% if flag?(:release) %} - within "assets" do - within :version do - get :filename do |params| - serve_static_asset "assets/#{params["filename"]}" - end - end - end - {% end %} - end - - def serve_blog - get do - render html: PostView.index(Data.load_posts) - end - - within "posts" do - get :id do |params| - post_id = params["id"].not_nil!.to_i - render html: PostView.show(Data.load_post(post_id)) - end - end - end - - def serve_api within "api" do protect_with ENV["CRANKY_LOGIN"], ENV["CRANKY_PASSWORD"] do within "posts" do @@ -93,13 +57,5 @@ module Crankypants::Web end end end - - def serve_app - within "app" do - protect_with ENV["CRANKY_LOGIN"], ENV["CRANKY_PASSWORD"] do - render text: Kilt.render("src/crankypants/web/views/app.slang"), content_type: "text/html" - end - end - end end end diff --git a/src/crankypants/web/routers/app.cr b/src/crankypants/web/routers/app.cr new file mode 100644 index 0000000..a8f23e0 --- /dev/null +++ b/src/crankypants/web/routers/app.cr @@ -0,0 +1,17 @@ +require "../../../crappy" +require "../../../crappy/authentication" + +module Crankypants::Web::Routers + class App < Crappy::Router + include Helpers + include Crappy::Authentication + + def call + within "app" do + protect_with ENV["CRANKY_LOGIN"], ENV["CRANKY_PASSWORD"] do + render text: Kilt.render("src/crankypants/web/views/app.slang"), content_type: "text/html" + end + end + end + end +end diff --git a/src/crankypants/web/routers/assets.cr b/src/crankypants/web/routers/assets.cr new file mode 100644 index 0000000..a9018e0 --- /dev/null +++ b/src/crankypants/web/routers/assets.cr @@ -0,0 +1,21 @@ +require "../../../crappy" +require "../../../crappy/authentication" + +module Crankypants::Web::Routers + class Assets < Crappy::Router + include Helpers + include Crappy::Authentication + + def call + {% if flag?(:release) %} + within "assets" do + within :version do + get :filename do |params| + serve_static_asset "assets/#{params["filename"]}" + end + end + end + {% end %} + end + end +end diff --git a/src/crankypants/web/routers/blog.cr b/src/crankypants/web/routers/blog.cr new file mode 100644 index 0000000..90c5974 --- /dev/null +++ b/src/crankypants/web/routers/blog.cr @@ -0,0 +1,22 @@ +require "../../../crappy" +require "../../../crappy/authentication" + +module Crankypants::Web::Routers + class Blog < Crappy::Router + include Helpers + include Crappy::Authentication + + def call + get do + render html: PostView.index(Data.load_posts) + end + + within "posts" do + get :id do |params| + post_id = params["id"].not_nil!.to_i + render html: PostView.show(Data.load_post(post_id)) + end + end + end + end +end