From 0cf17ab5ace502e9a4fc5adf3cba4667e5c2a58a Mon Sep 17 00:00:00 2001 From: Alpha Chen Date: Sat, 17 Jun 2023 20:30:48 -0700 Subject: [PATCH] mu --- config.ru | 15 ++- lib/app.rb | 232 +++++++++++++++++++-------------------------- lib/models.rb | 2 +- lib/views.rb | 43 +++++++++ views/hello.phlex | 3 - views/layout.phlex | 5 - 6 files changed, 151 insertions(+), 149 deletions(-) create mode 100644 lib/views.rb delete mode 100644 views/hello.phlex delete mode 100644 views/layout.phlex diff --git a/config.ru b/config.ru index 6d5a898..c4a4858 100644 --- a/config.ru +++ b/config.ru @@ -5,9 +5,14 @@ if dev logger = Logger.new($stdout) end - require "rack/unreloader" -Unreloader = Rack::Unreloader.new(subclasses: %w[Roda Sequel::Model], logger: logger, reload: dev, autoload: dev){App} -require_relative "lib/models" -Unreloader.require("lib/app.rb"){'App'} -run(dev ? Unreloader : App.freeze.app) +Unreloader = Rack::Unreloader.new( + subclasses: %w[Roda Sequel::Model Phlex::HTML], + logger: logger, + reload: dev, + autoload: dev, +) { RankKing::App } +# require_relative("lib/models") +Unreloader.require("lib/views.rb") +Unreloader.require("lib/app.rb") { "RankKing::App" } +run(dev ? Unreloader : RankKing::App.freeze.app) diff --git a/lib/app.rb b/lib/app.rb index 3c9efa2..c376d90 100644 --- a/lib/app.rb +++ b/lib/app.rb @@ -1,159 +1,121 @@ # frozen_string_literal: true require_relative "models" +require_relative "views" require "roda" -class App < Roda - opts[:check_dynamic_arity] = false - opts[:check_arity] = :warn - - plugin :default_headers, - "Content-Type" => "text/html", - # "Strict-Transport-Security" => "max-age=16070400;", # Uncomment if only allowing https:// access - "X-Frame-Options" => "deny", - "X-Content-Type-Options" => "nosniff", - "X-XSS-Protection" => "1; mode=block" - - plugin :content_security_policy do |csp| - csp.default_src :none - # csp.style_src :self, "https://cdn.jsdelivr.net" - csp.form_action :self - csp.script_src :self - csp.connect_src :self - csp.base_uri :none - csp.frame_ancestors :none - end - - # css_opts = {cache: false, style: :compressed} - # :nocov: - # if ENV["RACK_ENV"] == 'development' - # css_opts.merge!(source_map_embed: true, source_map_contents: true, source_map_file: ".") - # end - plugin :render_coverage if defined?(SimpleCov) - # :nocov: - - plugin :route_csrf - plugin :flash - # plugin :assets, css: "app.scss", css_opts: css_opts, timestamp_paths: true - # plugin :render, escape: true, layout: "./layout", :template_opts=>{chain_appends: !defined?(SimpleCov), freeze: true, skip_compiled_encoding_detection: true} - plugin :render, engine: :phlex - # plugin :public - plugin :Integer_matcher_max - plugin :typecast_params_sized_integers, sizes: [64], default_size: 64 - plugin :hash_branch_view_subdir - - logger = if ENV["RACK_ENV"] == "test" - Class.new{def write(_) end}.new - else - $stderr - end - plugin :common_logger, logger - - plugin :not_found do - @page_title = "File Not Found" - view(content: "") - end - - if ENV["RACK_ENV"] == "development" - plugin :exception_page - class RodaRequest - def assets - exception_page_assets - super - end +module RankKing + class App < Roda + opts[:check_dynamic_arity] = false + opts[:check_arity] = :warn + + plugin :default_headers, + "Content-Type" => "text/html", + # "Strict-Transport-Security" => "max-age=16070400;", # Uncomment if only allowing https:// access + "X-Frame-Options" => "deny", + "X-Content-Type-Options" => "nosniff", + "X-XSS-Protection" => "1; mode=block" + + plugin :content_security_policy do |csp| + csp.default_src :none + # csp.style_src :self, "https://cdn.jsdelivr.net" + csp.form_action :self + csp.script_src :self + csp.connect_src :self + csp.base_uri :none + csp.frame_ancestors :none end - else - def self.freeze - Sequel::Model.freeze_descendents - DB.freeze - super - end - end - plugin :error_handler do |e| - case e - when Roda::RodaPlugins::RouteCsrf::InvalidToken - @page_title = "Invalid Security Token" - response.status = 400 - view(content: "

An invalid security token was submitted with this request, and this request could not be processed.

") + # css_opts = {cache: false, style: :compressed} + # :nocov: + # if ENV["RACK_ENV"] == 'development' + # css_opts.merge!(source_map_embed: true, source_map_contents: true, source_map_file: ".") + # end + # plugin :render_coverage if defined?(SimpleCov) + # :nocov: + + plugin :route_csrf + plugin :flash + # plugin :assets, css: "app.scss", css_opts: css_opts, timestamp_paths: true + # plugin :render, escape: true, layout: "./layout", :template_opts=>{chain_appends: !defined?(SimpleCov), freeze: true, skip_compiled_encoding_detection: true} + # plugin :public + plugin :Integer_matcher_max + plugin :typecast_params_sized_integers, sizes: [64], default_size: 64 + # plugin :hash_branch_view_subdir + plugin :custom_block_results + + logger = if ENV["RACK_ENV"] == "test" + Class.new{def write(_) end}.new else - $stderr.print "#{e.class}: #{e.message}\n" - $stderr.puts e.backtrace - next exception_page(e, assets: true) if ENV["RACK_ENV"] == "development" - @page_title = "Internal Server Error" - view(content: "") + $stderr end - end + plugin :common_logger, logger - plugin :sessions, - key: "_App.session", - #cookie_options: {secure: ENV['RACK_ENV'] != 'test'}, # Uncomment if only allowing https:// access - secret: ENV.send((ENV["RACK_ENV"] == "development" ? :[] : :delete), "APP_SESSION_SECRET") - - # if Unreloader.autoload? - # plugin :autoload_hash_branches - # autoload_hash_branch_dir("./routes") - # end - # Unreloader.autoload("routes", delete_hook: proc{|f| hash_branch(File.basename(f).delete_suffix(".rb"))}){} - - route do |r| - # r.public - # r.assets - check_csrf! + plugin :not_found do + @page_title = "File Not Found" + view(content: "") + end - r.root do - @page_title = "Foo" + if ENV["RACK_ENV"] == "development" + plugin :exception_page + class RodaRequest + def assets + exception_page_assets + super + end + end + else + def self.freeze + Sequel::Model.freeze_descendents + DB.freeze + super + end + end - # render("hello", locals: {name: "Alice"}) - view("hello", locals: {name: "Bob"}) + plugin :error_handler do |e| + case e + when Roda::RodaPlugins::RouteCsrf::InvalidToken + @page_title = "Invalid Security Token" + response.status = 400 + view(content: "

An invalid security token was submitted with this request, and this request could not be processed.

") + else + $stderr.print "#{e.class}: #{e.message}\n" + $stderr.puts e.backtrace + next exception_page(e, assets: true) if ENV["RACK_ENV"] == "development" + # @page_title = "Internal Server Error" + # view(content: "") + end end - end -end -require "tilt/template" -require "phlex" + plugin :sessions, + key: "_App.session", + #cookie_options: {secure: ENV['RACK_ENV'] != 'test'}, # Uncomment if only allowing https:// access + secret: ENV.send((ENV["RACK_ENV"] == "development" ? :[] : :delete), "APP_SESSION_SECRET") -module Tilt - class PhlexTemplate < Template - def prepare - end + # if Unreloader.autoload? + # plugin :autoload_hash_branches + # autoload_hash_branch_dir("./routes") + # end + # Unreloader.autoload("routes", delete_hook: proc{|f| hash_branch(File.basename(f).delete_suffix(".rb"))}){} - # I have no idea how this works - it's just copied pretty much blindly from here: - # - # https://github.com/phlex-ruby/phlex-rails/blob/main/lib/phlex/rails/layout.rb#L18 - def evaluate(scope, locals, &block) - klass = Class.new(Phlex::HTML) - klass.class_eval(data, __FILE__, __LINE__) + # def render(view, layout=Views::Layout.new) + # component.call + # end - component = klass.new + route do |r| + # r.public + # r.assets + check_csrf! - scope.instance_variables - .reject { _2.start_with?("@_") } - .each do |k| - component.instance_variable_set(k, scope.instance_variable_get(k)) - end - locals.each do |k,v| - component.instance_variable_set("@#{k}", v) + r.root do + Views::Layout.new(Views::Hello.new(name: "Bob")).call end - component.call do |yielded| - output = yield - component.unsafe_raw(output) + r.on "pools" do + r.is "new" do + Views::Layout.new(Views::NewPool.new).call + end end end - - # Not sure if this is better...? - # def precompiled_preamble(*) = "klass = Class.new(Phlex::HTML) do" - # def precompiled_template(*) = data - # def precompiled_postamble(locals) = <<~RUBY - # end - # component = klass.new(**locals) - # component.call do |yielded| - # output = yield - # component.unsafe_raw(output) - # end - # RUBY end end - -Tilt.register(Tilt::PhlexTemplate, "phlex") diff --git a/lib/models.rb b/lib/models.rb index 32eefb3..aba51f5 100644 --- a/lib/models.rb +++ b/lib/models.rb @@ -16,7 +16,7 @@ unless defined?(Unreloader) Unreloader = Rack::Unreloader.new(reload: false, autoload: !ENV["NO_AUTOLOAD"]) end -Unreloader.autoload("models"){|f| Sequel::Model.send(:camelize, File.basename(f).sub(/\.rb\z/, ''))} +# Unreloader.autoload("models"){|f| Sequel::Model.send(:camelize, File.basename(f).sub(/\.rb\z/, ''))} if ENV["RACK_ENV"] == "development" || ENV["RACK_ENV"] == "test" require "logger" diff --git a/lib/views.rb b/lib/views.rb new file mode 100644 index 0000000..af7d82b --- /dev/null +++ b/lib/views.rb @@ -0,0 +1,43 @@ +require "phlex" + +module RankKing + module Views + + class Layout < Phlex::HTML + def initialize(view) + @view = view + end + + def template + doctype + html(lang: "en") do + head do + meta(charset: "utf-8") + meta(name: "viewport", content: "width=device-width, initial-scale=1") + title { "Rank King" } + end + + body do + header do + h1 { "Rank King" } + end + + nav + + main do + render @view + end + + footer + end + end + end + end + + class NewPool < Phlex::HTML + def template + plain "ohai" + end + end + end +end diff --git a/views/hello.phlex b/views/hello.phlex deleted file mode 100644 index 5c7a363..0000000 --- a/views/hello.phlex +++ /dev/null @@ -1,3 +0,0 @@ -def template - h1 { "👋 Hello #@name!" } -end diff --git a/views/layout.phlex b/views/layout.phlex deleted file mode 100644 index 1fc8541..0000000 --- a/views/layout.phlex +++ /dev/null @@ -1,5 +0,0 @@ -def template - article do - yield - end -end