cli: add ability to bind to custom addr / port

This commit is contained in:
0x1eef 2024-02-16 01:21:12 -03:00
parent 3ab0106d11
commit d72b9597f0
3 changed files with 19 additions and 9 deletions

View file

@ -3,6 +3,16 @@
class Twenty::Command::Up < Twenty::Command class Twenty::Command::Up < Twenty::Command
set_banner usage: "twenty up [OPTIONS]", set_banner usage: "twenty up [OPTIONS]",
description: "Start the twenty web server" description: "Start the twenty web server"
set_option "-b ADDR",
"--bind ADDR",
"Bind to ADDR (default: 127.0.0.1)",
default: "127.0.0.1"
set_option "-p PORT",
"--port PORT",
"Listen on PORT (default: 2020)",
default: 2020,
as: Integer
include CommonOptionMixin include CommonOptionMixin
prepend Twenty::Command::MigrationMixin prepend Twenty::Command::MigrationMixin
prepend Twenty::Command::SQLiteMixin prepend Twenty::Command::SQLiteMixin
@ -15,7 +25,7 @@ class Twenty::Command::Up < Twenty::Command
private private
def run_command(options) def run_command(options)
server = Twenty::Servlet.server server = Twenty::Servlet.server(options)
trap(:SIGINT) { server.shutdown } trap(:SIGINT) { server.shutdown }
server.start server.start
end end

View file

@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
gem.summary = "twenty: CLI component" gem.summary = "twenty: CLI component"
gem.description = gem.summary gem.description = gem.summary
gem.executables = ["twenty", "20"] gem.executables = ["twenty", "20"]
gem.add_runtime_dependency "cmd.rb", "~> 0.2" gem.add_runtime_dependency "cmd.rb", "~> 0.4"
gem.add_development_dependency "test-unit", "~> 3.5.7" gem.add_development_dependency "test-unit", "~> 3.5.7"
gem.add_development_dependency "yard", "~> 0.9" gem.add_development_dependency "yard", "~> 0.9"
gem.add_development_dependency "redcarpet", "~> 3.5" gem.add_development_dependency "redcarpet", "~> 3.5"

View file

@ -2,14 +2,14 @@
module Twenty::Servlet::ServerMixin module Twenty::Servlet::ServerMixin
## ##
# @param [Hash] options # @param [Hash] cli_options
# Server options that take precedence over # CLI options merged into
# {ServerMixin#server_options ServerMixin#server_options}. # {ServerMixin#server_options ServerMixin#server_options}.
# #
# @return [WEBrick::HTTPServer] # @return [WEBrick::HTTPServer]
# Returns an instance of WEBrick::HTTPServer. # Returns an instance of WEBrick::HTTPServer.
def server(options = {}) def server(cli_options = {})
server = WEBrick::HTTPServer.new server_options.merge(options) server = WEBrick::HTTPServer.new server_options(cli_options)
server.mount "/graphql", Twenty::Servlet::GraphQL server.mount "/graphql", Twenty::Servlet::GraphQL
server server
end end
@ -17,11 +17,11 @@ module Twenty::Servlet::ServerMixin
## ##
# @return [Hash<Symbol, String>] # @return [Hash<Symbol, String>]
# The default server options given to WEBrick::HTTPServer.new. # The default server options given to WEBrick::HTTPServer.new.
def server_options def server_options(cli_options)
{ {
DocumentRoot: Twenty.build, DocumentRoot: Twenty.build,
BindAddress: "127.0.0.1", BindAddress: cli_options.bind,
Port: 2020 Port: cli_options.port
} }
end end
end end