Class: MonkeysPaw::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/monkeyspaw/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, **options) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
# File 'lib/monkeyspaw/server.rb', line 8

def initialize(app, **options)
  @app = app
  @options = options
  @rack_app = build_rack_app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/monkeyspaw/server.rb', line 6

def app
  @app
end

Instance Method Details

#generate_error_page(message) ⇒ Object



57
58
59
# File 'lib/monkeyspaw/server.rb', line 57

def generate_error_page(message)
  "<html><body><h1>Error</h1><p>#{message}</p></body></html>"
end

#generate_page(path, prompt_file) ⇒ Object



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
# File 'lib/monkeyspaw/server.rb', line 30

def generate_page(path, prompt_file)
  if app.config.caching_enabled
    # Try to get from cache first
    cached_content = app.cache_manager.get_cached_page(path, prompt_file)
    return cached_content if cached_content
  end

  # Cache miss or caching disabled, generate the page
  # Reload layout and style prompts to pick up changes
  app.prompt_manager.load_default_components
  layout_prompt = app.prompt_manager.layout_prompt
  style_prompt = app.prompt_manager.style_prompt

  html_content = HtmlFromDescriptionGenerator.new(
    description: File.read(prompt_file),
    layout_prompt: layout_prompt,
    style_prompt: style_prompt
  ).generate

  # Store in cache if enabled
  if app.config.caching_enabled
    app.cache_manager.cache_page(path, prompt_file, html_content)
  end

  html_content
end

#shutdownObject



25
26
27
28
# File 'lib/monkeyspaw/server.rb', line 25

def shutdown
  puts "Enough! You throw the paw away. Hopefully there were no unintended consequences. Mua ha ha ha ha!"
  exit
end

#startObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/monkeyspaw/server.rb', line 14

def start
  host = @options[:host] || app.config.host || 'localhost'
  port = @options[:port] || app.config.port || 1337

  puts "You pick up the monkey's paw. Be careful what you wish for on http://#{host}:#{port}"

  setup_signal_handlers

  Rack::Handler::Puma.run(@rack_app, Host: host, Port: port, Silent: true, Threads: "0:16", workers: 0)
end