Class: Up::Bun::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(app:, host: 'localhost', port: 3000, scheme: 'http', ca_file: nil, cert_file: nil, key_file: nil) ⇒ Server

Returns a new instance of Server.



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/up/bun/server.rb', line 8

def initialize(app:, host: 'localhost', port: 3000, scheme: 'http', ca_file: nil, cert_file: nil, key_file: nil)
  @app = app
  @scheme    = scheme || 'http'
  raise "unsupported scheme #{@scheme}" unless %w[http https].include?(@scheme)
  @host      = host || 'localhost'
  @port      = port&.to_i || 3000
  @config    = { handler: self.class.name, engine: "bun/#{`process.version`}", port: port, scheme: scheme, host: host }.freeze
  @ca_file   = ca_file
  @cert_file = cert_file
  @key_file  = key_file
  @server = nil
end

Instance Method Details

#listenObject



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
# File 'lib/up/bun/server.rb', line 49

def listen
  raise "already running" if @server
  %x{
    const oubr = Opal.Up.Bun.RackEnv;
    const oubs = Opal.Up.Bun.Server;

    var server_options = {
      port: #@port,
      hostname: #@host,
      development: false,
      fetch(req) {
        const rack_res = #@app.$call(oubr.$new(req, #@config));
        const hdr = new Headers();
        oubs.handle_headers(rack_res[1]);
        var body = '';
        body = oubs.handle_response(rack_res[2], body);
        return new Response(body, {status: rack_res[0], statusText: 'OK', headers: hdr});
      }
    };
    if (#@scheme === 'https') {
      server_options.tls = {
        key: Bun.file(#@key_file),
        cert: Bun.file(#@cert_file),
        ca: Bun.file(#@ca_file)
      };
    }

    #@server = Bun.serve(server_options);
    console.log(`Server is running on ${#@scheme}://${#@host}:${#@port}`);
  }
end

#stopObject



81
82
83
84
85
86
# File 'lib/up/bun/server.rb', line 81

def stop
  if Up::CLI::stoppable?
    `#@server.stop()`
    @server = nil
  end
end