Class: Fresco::App

Inherits:
Object
  • Object
show all
Defined in:
lib/fresco/runtime/runtime.rb

Overview

Boot class for the user’s application. Generated apps subclass this in config/app.rb (e.g. ‘class App::Base < Fresco::App`) and call `App::Base.new.run` from app.rb. Subclasses can override defaults by reassigning @port / @workers in their own initialize.

‘run` dispatches between two modes:

[serve] [-p PORT] [-w N]   → HTTP listener (run_server). `serve` is
                             the default when no subcommand is given,
                             so `./build/app` and `./build/app -p 3030`
                             both boot the server.
METHOD PATH [BODY]         → one-shot CLI dispatch (kept for smoke tests)

ARGV is referenced directly rather than passed through as a method parameter — Spinel infers ‘ARGV` as its dedicated `sp_Argv` type only at global-read sites; routing it through a Ruby parameter collapses the type to `mrb_int` and the indexing/length lowering fails. Keep all ARGV access inside these methods.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApp

Returns a new instance of App.



454
455
456
457
# File 'lib/fresco/runtime/runtime.rb', line 454

def initialize
  @port    = 3030
  @workers = 1
end

Instance Attribute Details

#portObject

Returns the value of attribute port.



452
453
454
# File 'lib/fresco/runtime/runtime.rb', line 452

def port
  @port
end

#workersObject

Returns the value of attribute workers.



452
453
454
# File 'lib/fresco/runtime/runtime.rb', line 452

def workers
  @workers
end

Class Method Details

.runObject

Class-level convenience so app.rb is ‘App::Base.run` rather than `App::Base.new.run`. The instance carries config state; the class method exists purely to spare the caller the `.new`.



462
463
464
# File 'lib/fresco/runtime/runtime.rb', line 462

def self.run
  new.run
end

Instance Method Details

#parse_serve_argsObject



486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
# File 'lib/fresco/runtime/runtime.rb', line 486

def parse_serve_args
  i = ARGV[0] == "serve" ? 1 : 0
  while i < ARGV.length
    a = ARGV[i]
    if a == "-p"
      @port = ARGV[i + 1].to_i
      i += 2
    elsif a == "-w"
      @workers = ARGV[i + 1].to_i
      i += 2
    else
      i += 1
    end
  end
end

#runObject



466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/fresco/runtime/runtime.rb', line 466

def run
  # Eager-open the DB connection at startup so a misconfigured DSN
  # fails fast on the binary's first command rather than the first
  # request. Spinel-shape note: Fresco::Db.boot! is defined by the
  # generated db_adapter.rb when an adapter is configured; the
  # `respond_to?` guard keeps no-DB apps working.
  Fresco::Db.boot! if Fresco::Db.respond_to?(:boot!)

  if ARGV[0] == "db:migrate"
    Fresco::DbMigrations.migrate!
  elsif ARGV[0] == "db:rollback"
    Fresco::DbMigrations.rollback!
  elsif ARGV.length == 0 || ARGV[0] == "serve" || ARGV[0].start_with?("-")
    parse_serve_args
    run_server(port: @port, workers: @workers)
  else
    run_cli
  end
end

#run_cliObject



502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/fresco/runtime/runtime.rb', line 502

def run_cli
  method = ARGV[0]
  path   = ARGV[1]
  body   = ARGV[2] || ""

  req = Request.new(method, path, body)
  res = dispatch_request(method, path, req)

  puts res.status
  puts
  print res.body
end