Module: Roda::RodaPlugins::BridgetownServer

Defined in:
lib/roda/plugins/bridgetown_server.rb

Defined Under Namespace

Modules: RequestMethods Classes: SiteContext

Class Method Summary collapse

Class Method Details

.load_dependencies(app) ⇒ Object

rubocop:disable Metrics



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/roda/plugins/bridgetown_server.rb', line 8

def self.load_dependencies(app) # rubocop:disable Metrics
  unless Bridgetown::Current.preloaded_configuration
    raise "You must supply a preloaded configuration before loading the Bridgetown Roda " \
          "plugin"
  end

  app.plugin :initializers
  app.plugin :method_override
  app.plugin :all_verbs
  app.plugin :hooks
  app.plugin :common_logger, Bridgetown::Rack::Logger.new($stdout), method: :info
  app.plugin :json
  app.plugin :json_parser
  app.plugin :indifferent_params
  app.plugin :cookies
  app.plugin :streaming
  app.plugin :public, root: Bridgetown::Current.preloaded_configuration.destination
  app.plugin :not_found do
    output_folder = Bridgetown::Current.preloaded_configuration.destination
    File.read(File.join(output_folder, "404.html"))
  rescue Errno::ENOENT
    "404 Not Found"
  end
  app.plugin :exception_page
  app.plugin :error_handler do |e|
    Bridgetown::Errors.print_build_error(
      e, logger: Bridgetown::LogAdapter.new(self.class.opts[:common_logger]), server: true
    )
    next exception_page(e) if ENV.fetch("RACK_ENV", nil) == "development"

    output_folder = Bridgetown::Current.preloaded_configuration.destination
    File.read(File.join(output_folder, "500.html"))
  rescue Errno::ENOENT
    "500 Internal Server Error"
  end

  ExceptionPage.class_eval do # rubocop:disable Metrics/BlockLength
    def self.css
      <<~CSS
        html * { padding:0; margin:0; }
        body * { padding:10px 20px; }
        body * * { padding:0; }
        body { font-family: -apple-system, sans-serif; font-size: 90%; }
        body>div { border-bottom:1px solid #ddd; }
        code { font-family: ui-monospace, monospace; }
        h1 { font-weight: bold; margin-block-end: .8em; }
        h2 { margin-block-end:.8em; }
        h2 span { font-size:80%; color:#f7f7db; font-weight:normal; }
        h3 { margin:1em 0 .5em 0; }
        h4 { margin:0 0 .5em 0; font-weight: normal; }
        table {
            border:1px solid #ccc; border-collapse: collapse; background:white; }
        tbody td, tbody th { vertical-align:top; padding:2px 3px; }
        thead th {
            padding:1px 6px 1px 3px; background:#fefefe; text-align:left;
            font-weight:normal; font-size:11px; border:1px solid #ddd; }
        tbody th { text-align:right; opacity: 0.7; padding-right:.5em; }
        table.vars { margin:5px 0 2px 40px; }
        table.vars td, table.req td { font-family: ui-monospace, monospace; }
        table td.code { width:100%;}
        table td.code div { overflow:hidden; }
        table.source th { color:#666; }
        table.source td {
            font-family: ui-monospace, monospace; white-space:pre; border-bottom:1px solid #eee; }
        ul.traceback { list-style-type:none; }
        ul.traceback li.frame { margin-bottom:1em; }
        div.context { margin: 10px 0; }
        div.context ol {
            padding-left:30px; margin:0 10px; list-style-position: inside; }
        div.context ol li {
            font-family: ui-monospace, monospace; white-space:pre; color:#666; cursor:pointer; }
        div.context ol.context-line li { color:black; background-color:#f7f7db; }
        div.context ol.context-line li span { float: right; }
        div.commands { margin-left: 40px; }
        div.commands a { color:black; text-decoration:none; }
        #summary { background: #1D453C; color: white; }
        #summary h2 { font-weight: normal; color: white; }
        #summary ul#quicklinks { list-style-type: none; margin-bottom: 2em; }
        #summary ul#quicklinks li { float: left; padding: 0 1em; }
        #summary ul#quicklinks>li+li { border-left: 1px #666 solid; }
        #summary a { color: #f47c3c; }
        #explanation { background:#eee; }
        #traceback { background: white; }
        #requestinfo { background:#f6f6f6; padding-left:120px; }
        #summary table { border:none; background:transparent; }
        #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; }
        #requestinfo h3 { margin-bottom:-1em; }
        .error { background: #ffc; }
        .specific { color:#cc3300; font-weight:bold; }
      CSS
    end
  end

  app.before do
    if self.class.opts[:bridgetown_site]
      # The site had previously been initialized via the bridgetown_ssr plugin
      Bridgetown::Current.sites[self.class.opts[:bridgetown_site].label] =
        self.class.opts[:bridgetown_site]
      @context ||= SiteContext.new({ site: self.class.opts[:bridgetown_site] })
    end
    Bridgetown::Current.preloaded_configuration ||=
      self.class.opts[:bridgetown_preloaded_config]

    request.root do
      output_folder = Bridgetown::Current.preloaded_configuration.destination
      File.read(File.join(output_folder, "index.html"))
    rescue StandardError
      response.status = 500
      "<p>ERROR: cannot find <code>index.html</code> in the output folder.</p>"
    end
  end
end