Module: Iodine::Base::CLI
- Defined in:
- ext/iodine/iodine.c
Class Method Summary collapse
-
.parse ⇒ Object
Parses the CLI argnumnents, returning the Rack filename (if provided).
Class Method Details
.parse ⇒ Object
Parses the CLI argnumnents, returning the Rack filename (if provided).
Unknown arguments are ignored.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 |
# File 'ext/iodine/iodine.c', line 316
static VALUE iodine_cli_parse(VALUE self) {
(void)self;
VALUE ARGV = rb_get_argv();
VALUE ret = Qtrue;
VALUE defaults = iodine_default_args;
VALUE iodine_version = rb_const_get(IodineModule, rb_intern("VERSION"));
char desc[1024];
if (!defaults || !ARGV || TYPE(ARGV) != T_ARRAY || TYPE(defaults) != T_HASH ||
TYPE(iodine_version) != T_STRING || RSTRING_LEN(iodine_version) > 512) {
FIO_LOG_ERROR("CLI parsing initialization error "
"ARGV=%p, Array?(%d), defaults == %p (%d)",
(void *)ARGV, (int)(TYPE(ARGV) == T_ARRAY), (void *)defaults,
(int)(TYPE(defaults) == T_HASH));
return Qnil;
}
/* Copy the Ruby ARGV to a C valid ARGV */
int argc = (int)RARRAY_LEN(ARGV) + 1;
if (argc <= 1) {
FIO_LOG_DEBUG("CLI: No arguments to parse...\n");
return Qnil;
} else {
FIO_LOG_DEBUG("Iodine CLI parsing %d arguments", argc);
}
char **argv = calloc(argc, sizeof(*argv));
FIO_ASSERT_ALLOC(argv);
argv[0] = (char *)"iodine";
for (int i = 1; i < argc; ++i) {
VALUE tmp = rb_ary_entry(ARGV, (long)(i - 1));
if (TYPE(tmp) != T_STRING) {
FIO_LOG_ERROR("ARGV Array contains a non-String object.");
ret = Qnil;
goto finish;
}
fio_str_info_s s = IODINE_RSTRINFO(tmp);
argv[i] = malloc(s.len + 1);
FIO_ASSERT_ALLOC(argv[i]);
memcpy(argv[i], s.data, s.len);
argv[i][s.len] = 0;
}
/* Levarage the facil.io CLI library */
memcpy(desc, "Iodine's HTTP/WebSocket server version ", 39);
memcpy(desc + 39, StringValueCStr(iodine_version),
RSTRING_LEN(iodine_version));
memcpy(desc + 39 + RSTRING_LEN(iodine_version),
"\r\n\r\nUse:\r\n iodine <options> <filename>\r\n\r\n"
"Both <options> and <filename> are optional. i.e.,:\r\n"
" iodine -p 0 -b /tmp/my_unix_sock\r\n"
" iodine -p 8080 path/to/app/conf.ru\r\n"
" iodine -p 8080 -w 4 -t 16\r\n"
" iodine -w -1 -t 4 -r redis://usr:pass@localhost:6379/",
263);
desc[39 + 263 + RSTRING_LEN(iodine_version)] = 0;
fio_cli_start(
argc, (const char **)argv, 0, -1, desc,
FIO_CLI_PRINT_HEADER("Address Binding:"),
"-bind -b -address address to listen to. defaults to any available.",
FIO_CLI_INT("-port -p port number to listen to. defaults port 3000"),
FIO_CLI_PRINT("\t\t\x1B[4mNote\x1B[0m: to bind to a Unix socket, set "
"\x1B[1mport\x1B[0m to 0."),
FIO_CLI_PRINT_HEADER("Concurrency:"),
FIO_CLI_INT("-workers -w number of processes to use."),
FIO_CLI_INT("-threads -t number of threads per process."),
FIO_CLI_PRINT("Negative concurrency values "
"map to fractions of available CPU cores."),
FIO_CLI_PRINT_HEADER("HTTP Settings:"),
FIO_CLI_STRING("-public -www public folder, for static file service."),
FIO_CLI_BOOL("-log -v HTTP request logging."),
FIO_CLI_INT("-keep-alive -k -tout HTTP keep-alive timeout in seconds "
"(0..255). Default: 40s"),
FIO_CLI_INT("-ping websocket ping interval (0..255). Default: 40s"),
FIO_CLI_INT(
"-max-body -maxbd HTTP upload limit in Mega-Bytes. Default: 50Mb"),
FIO_CLI_INT("-max-header -maxhd header limit per HTTP request in Kb. "
"Default: 32Kb."),
FIO_CLI_PRINT_HEADER("WebSocket Settings:"),
FIO_CLI_INT("-max-msg -maxms incoming WebSocket message limit in Kb. "
"Default: 250Kb"),
FIO_CLI_PRINT_HEADER("SSL/TLS:"),
FIO_CLI_BOOL("-tls enable SSL/TLS using a self-signed certificate."),
FIO_CLI_STRING(
"-tls-cert -cert the SSL/TLS public certificate file name."),
FIO_CLI_STRING("-tls-key -key the SSL/TLS private key file name."),
FIO_CLI_STRING(
"-tls-pass -tls-password the password (if any) protecting the "
"private key file."),
FIO_CLI_PRINT("\t\t\x1B[1m-tls-password\x1B[0m is deprecated, use "
"\x1B[1m-tls-pass\x1B[0m"),
FIO_CLI_PRINT_HEADER("Connecting Iodine to Redis:"),
FIO_CLI_STRING(
"-redis -r an optional Redis URL server address. Default: none."),
FIO_CLI_INT(
"-redis-ping -rp websocket ping interval (0..255). Default: 300s"),
FIO_CLI_PRINT_HEADER("Misc:"),
FIO_CLI_BOOL(
"-warmup --preload warm up the application. CAREFUL! with workers."),
FIO_CLI_INT("-verbosity -V 0..5 server verbosity level. Default: 4"));
/* copy values from CLI library to iodine */
if (fio_cli_get("-V")) {
int level = fio_cli_get_i("-V");
if (level > 0 && level < 100)
FIO_LOG_LEVEL = level;
}
if (!fio_cli_get("-w") && getenv("WEB_CONCURRENCY")) {
fio_cli_set("-w", getenv("WEB_CONCURRENCY"));
}
if (!fio_cli_get("-w") && getenv("WORKERS")) {
fio_cli_set("-w", getenv("WORKERS"));
}
if (fio_cli_get("-w")) {
iodine_workers_set(IodineModule, INT2NUM(fio_cli_get_i("-w")));
}
if (!fio_cli_get("-t") && getenv("THREADS")) {
fio_cli_set("-t", getenv("THREADS"));
}
if (fio_cli_get("-t")) {
iodine_threads_set(IodineModule, INT2NUM(fio_cli_get_i("-t")));
}
if (fio_cli_get_bool("-v")) {
rb_hash_aset(defaults, log_sym, Qtrue);
}
if (fio_cli_get_bool("-warmup")) {
rb_hash_aset(defaults, ID2SYM(rb_intern("warmup_")), Qtrue);
}
// if (!fio_cli_get("-b") && getenv("ADDRESS")) {
// fio_cli_set("-b", getenv("ADDRESS"));
// }
if (fio_cli_get("-b")) {
if (fio_cli_get("-b")[0] == '/' ||
(fio_cli_get("-b")[0] == '.' && fio_cli_get("-b")[1] == '/')) {
if (fio_cli_get("-p") &&
(fio_cli_get("-p")[0] != '0' || fio_cli_get("-p")[1])) {
FIO_LOG_WARNING(
"Detected a Unix socket binding (-b) conflicting with port.\n"
" Port settings (-p %s) are ignored",
fio_cli_get("-p"));
}
fio_cli_set("-p", "0");
} else {
// if (!fio_cli_get("-p") && getenv("PORT")) {
// fio_cli_set("-p", getenv("PORT"));
// }
}
rb_hash_aset(defaults, address_sym, rb_str_new_cstr(fio_cli_get("-b")));
}
if (fio_cli_get("-p")) {
rb_hash_aset(defaults, port_sym, rb_str_new_cstr(fio_cli_get("-p")));
}
if (fio_cli_get("-www")) {
rb_hash_aset(defaults, public_sym, rb_str_new_cstr(fio_cli_get("-www")));
}
if (!fio_cli_get("-redis") && getenv("IODINE_REDIS_URL")) {
fio_cli_set("-redis", getenv("IODINE_REDIS_URL"));
}
if (fio_cli_get("-redis")) {
rb_hash_aset(defaults, ID2SYM(rb_intern("redis_")),
rb_str_new_cstr(fio_cli_get("-redis")));
}
if (fio_cli_get("-k")) {
rb_hash_aset(defaults, timeout_sym, INT2NUM(fio_cli_get_i("-k")));
}
if (fio_cli_get("-ping")) {
rb_hash_aset(defaults, ping_sym, INT2NUM(fio_cli_get_i("-ping")));
}
if (fio_cli_get("-redis-ping")) {
rb_hash_aset(defaults, ID2SYM(rb_intern("redis_ping_")),
INT2NUM(fio_cli_get_i("-redis-ping")));
}
if (fio_cli_get("-max-body")) {
rb_hash_aset(defaults, max_body_sym,
INT2NUM((fio_cli_get_i("-max-body") /* * 1024 * 1024 */)));
}
if (fio_cli_get("-maxms")) {
rb_hash_aset(defaults, max_msg_sym,
INT2NUM((fio_cli_get_i("-maxms") /* * 1024 */)));
}
if (fio_cli_get("-maxhd")) {
rb_hash_aset(defaults, max_headers_sym,
INT2NUM((fio_cli_get_i("-maxhd") /* * 1024 */)));
}
if (fio_cli_get_bool("-tls") || fio_cli_get("-key") || fio_cli_get("-cert")) {
VALUE rbtls = IodineCaller.call(IodineTLSClass, rb_intern2("new", 3));
if (rbtls == Qnil) {
FIO_LOG_FATAL("Iodine internal error, Ruby TLS object is nil.");
exit(-1);
}
fio_tls_s *tls = iodine_tls2c(rbtls);
if (!tls) {
FIO_LOG_FATAL("Iodine internal error, TLS object NULL.");
exit(-1);
}
if (fio_cli_get("-tls-key") && fio_cli_get("-tls-cert")) {
fio_tls_cert_add(tls, NULL, fio_cli_get("-tls-cert"),
fio_cli_get("-tls-key"), fio_cli_get("-tls-pass"));
} else {
if (!fio_cli_get_bool("-tls"))
FIO_LOG_ERROR("TLS support requires both key and certificate."
"\r\n\t\tfalling back on a self signed certificate.");
char name[1024];
fio_local_addr(name, 1024);
fio_tls_cert_add(tls, name, NULL, NULL, NULL);
}
rb_hash_aset(defaults, tls_sym, rbtls);
}
if (fio_cli_unnamed_count()) {
rb_hash_aset(defaults, ID2SYM(rb_intern("filename_")),
rb_str_new_cstr(fio_cli_unnamed(0)));
}
/* create `filename` String, cleanup and return */
fio_cli_end();
finish:
for (int i = 1; i < argc; ++i) {
free(argv[i]);
}
free(argv);
return ret;
}
|