Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
Move different parts of Crankypants into their own router classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
hmans committed May 6, 2018
1 parent d4b108d commit a091f77
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 53 deletions.
7 changes: 5 additions & 2 deletions 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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/crankypants/web/helpers.cr
@@ -1,3 +1,6 @@
require "kilt"
require "kilt/slang"

require "./assets"

module Crankypants
Expand Down
@@ -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
Expand Down Expand Up @@ -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
17 changes: 17 additions & 0 deletions 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
21 changes: 21 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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

0 comments on commit a091f77

Please sign in to comment.