Module: Agoo::Server
- Defined in:
- ext/agoo/rserver.c
Class Method Summary collapse
-
.add_mime(suffix, type) ⇒ Object
call-seq: add_mime(suffix, type).
-
.domain(host, path) ⇒ Object
call-seq: domain(host, path).
-
.handle(method, pattern, handler) ⇒ Object
call-seq: handle(method, pattern, handler).
-
.handle_not_found(handler) ⇒ Object
call-seq: not_found_handle(handler).
-
.header_rule(path, mime, key, value) ⇒ Object
call-seq: header_rule(path, mime, key, value).
-
.init(*args) ⇒ Object
call-seq: init(port, root, options).
-
.path_group(path, dirs) ⇒ Object
call-seq: path_group(path, dirs).
-
.rack_early_hints(on) ⇒ Object
call-seq: rack_early_hints(on).
-
.shutdown ⇒ Object
call-seq: shutdown().
-
.start ⇒ Object
call-seq: start().
-
.use(*args) ⇒ Object
call-seq: use(middleware, *args).
Class Method Details
.add_mime(suffix, type) ⇒ Object
call-seq: add_mime(suffix, type)
Adds a mime type by associating a type string with a suffix. This is used for static files.
1123 1124 1125 1126 1127 1128 1129 1130 1131 |
# File 'ext/agoo/rserver.c', line 1123
static VALUE
add_mime(VALUE self, VALUE suffix, VALUE type) {
struct _agooErr err = AGOO_ERR_INIT;
if (AGOO_ERR_OK != mime_set(&err, StringValuePtr(suffix), StringValuePtr(type))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
return Qnil;
}
|
.domain(host, path) ⇒ Object
call-seq: domain(host, path)
Sets up a sub-domain. The first argument, host should be either a String or a Regexp that includes variable replacement elements. The path argument should also be a string. If the host argument is a Regex then the $(n) sequence will be replaced by the matching variable in the Regex result. The path is the root of the sub-domain.
1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 |
# File 'ext/agoo/rserver.c', line 1208
static VALUE
domain(VALUE self, VALUE host, VALUE path) {
struct _agooErr err = AGOO_ERR_INIT;
switch(rb_type(host)) {
case RUBY_T_STRING:
rb_check_type(path, T_STRING);
if (AGOO_ERR_OK != agoo_domain_add(&err, rb_string_value_ptr((VALUE*)&host), rb_string_value_ptr((VALUE*)&path))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
break;
case RUBY_T_REGEXP: {
volatile VALUE v = rb_funcall(host, rb_intern("inspect"), 0);
char rx[1024];
if (sizeof(rx) <= (size_t)RSTRING_LEN(v)) {
rb_raise(rb_eArgError, "host Regex limited to %ld characters", sizeof(rx));
}
strcpy(rx, rb_string_value_ptr((VALUE*)&v) + 1);
rx[RSTRING_LEN(v) - 2] = '\0';
if (AGOO_ERR_OK != agoo_domain_add_regex(&err, rx, rb_string_value_ptr((VALUE*)&path))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
break;
}
default:
rb_raise(rb_eArgError, "host must be a String or Regex");
break;
}
return Qnil;
}
|
.handle(method, pattern, handler) ⇒ Object
call-seq: handle(method, pattern, handler)
Registers a handler for the HTTP method and path pattern specified. The path pattern follows glob like rules in that a single * matches a single token bounded by the ‘/` character and a double ** matches all remaining. The handler must resolve to an object than responds to “on_request” for the basic handler, “call” for a Rack handler, or for a WAB handler (see github.com/ohler55/wabur), “create”, “read”, “update”, and “delete”. The name of a class will resolve to the class itself.
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 |
# File 'ext/agoo/rserver.c', line 1002
static VALUE
handle(VALUE self, VALUE method, VALUE pattern, VALUE handler) {
agooHook hook;
agooMethod meth = AGOO_ALL;
const char *pat;
ID static_id = rb_intern("static?");
rb_check_type(pattern, T_STRING);
pat = StringValuePtr(pattern);
if (connect_sym == method) {
meth = AGOO_CONNECT;
} else if (delete_sym == method) {
meth = AGOO_DELETE;
} else if (get_sym == method) {
meth = AGOO_GET;
} else if (head_sym == method) {
meth = AGOO_HEAD;
} else if (options_sym == method) {
meth = AGOO_OPTIONS;
} else if (post_sym == method) {
meth = AGOO_POST;
} else if (put_sym == method) {
meth = AGOO_PUT;
} else if (patch_sym == method) {
meth = AGOO_PATCH;
} else if (Qnil == method) {
meth = AGOO_ALL;
} else {
rb_raise(rb_eArgError, "invalid method");
}
if (T_STRING == rb_type(handler)) {
handler = resolve_classpath(StringValuePtr(handler), RSTRING_LEN(handler));
}
if (rb_respond_to(handler, static_id)) {
if (Qtrue == rb_funcall(handler, static_id, 0)) {
VALUE res = rb_funcall(handler, call_id, 1, Qnil);
VALUE bv;
rb_check_type(res, T_ARRAY);
if (3 != RARRAY_LEN(res)) {
rb_raise(rb_eArgError, "a rack call() response must be an array of 3 members.");
}
bv = rb_ary_entry(res, 2);
if (T_ARRAY == rb_type(bv)) {
int i;
int bcnt = (int)RARRAY_LEN(bv);
agooText t = agoo_text_allocate(1024);
struct _agooErr err = AGOO_ERR_INIT;
VALUE v;
if (NULL == t) {
rb_raise(rb_eArgError, "failed to allocate response.");
}
for (i = 0; i < bcnt; i++) {
v = rb_ary_entry(bv, i);
t = agoo_text_append(t, StringValuePtr(v), (int)RSTRING_LEN(v));
}
if (NULL == t) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for a response.");
}
if (NULL == agoo_page_immutable(&err, pat, t->text, (int)t->len)) {
rb_raise(rb_eArgError, "%s", err.msg);
}
agoo_text_release(t);
return Qnil;
}
}
}
if (NULL != the_rserver.uses) {
RUse u;
for (u = the_rserver.uses; NULL != u; u = u->next) {
u->argv[0] = handler;
handler = rb_funcall2(u->clas, rb_intern("new"), u->argc, u->argv);
}
}
if (NULL == (hook = rhook_create(meth, pat, handler, &agoo_server.eval_queue))) {
rb_raise(rb_eStandardError, "out of memory.");
} else {
agooHook h;
agooHook prev = NULL;
for (h = agoo_server.hooks; NULL != h; h = h->next) {
prev = h;
}
if (NULL != prev) {
prev->next = hook;
} else {
agoo_server.hooks = hook;
}
rb_gc_register_address((VALUE*)&hook->handler);
}
return Qnil;
}
|
.handle_not_found(handler) ⇒ Object
call-seq: not_found_handle(handler)
Registers a handler to be called when no other hook is found and no static file is found.
1106 1107 1108 1109 1110 1111 1112 1113 1114 |
# File 'ext/agoo/rserver.c', line 1106
static VALUE
handle_not_found(VALUE self, VALUE handler) {
if (NULL == (agoo_server.hook404 = rhook_create(AGOO_GET, "/", handler, &agoo_server.eval_queue))) {
rb_raise(rb_eStandardError, "out of memory.");
}
rb_gc_register_address((VALUE*)&agoo_server.hook404->handler);
return Qnil;
}
|
.header_rule(path, mime, key, value) ⇒ Object
call-seq: header_rule(path, mime, key, value)
Add a header rule. A header rule will add the key and value to the headers of any static asset that matches the path and mime type specified. The path pattern follows glob like rules in that a single * matches a single token bounded by the ‘/` character and a double ** matches all remaining. The mime can also be a * which matches all types. The mime argument will be compared to the mine type as well as the file extension so ’applicaiton/json’, a mime type can be used as can ‘json’ as a file extension. All rules that match add the header key and value to the header of a static asset.
Note that the server must be initialized before calling this method.
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 |
# File 'ext/agoo/rserver.c', line 1183
static VALUE
header_rule(VALUE self, VALUE path, VALUE mime, VALUE key, VALUE value) {
struct _agooErr err = AGOO_ERR_INIT;
rb_check_type(path, T_STRING);
rb_check_type(mime, T_STRING);
rb_check_type(key, T_STRING);
rb_check_type(value, T_STRING);
if (AGOO_ERR_OK != agoo_header_rule(&err, StringValuePtr(path), StringValuePtr(mime), StringValuePtr(key), StringValuePtr(value))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
return Qnil;
}
|
.init(*args) ⇒ Object
call-seq: init(port, root, options)
Configures the server that will listen on the designated port and using the root as the root of the static resources. This must be called before using other server methods. Logging is feature based and not level based and the options reflect that approach. If bind option is to be used instead of the port then set the port to zero.
-
options [Hash] server options
-
:pedantic [true|false] if true response header and status codes are checked and an exception raised if they violate the rack spec at github.com/rack/rack/blob/master/SPEC, tools.ietf.org/html/rfc3875#section-4.1.18, or tools.ietf.org/html/rfc7230.
-
:thread_count [Integer] number of ruby worker threads. Defaults to one. If zero then the start function will not return but instead will proess using the thread that called start. Usually the default is best unless the workers are making IO calls.
-
:worker_count [Integer] number of workers to fork. Defaults to one which is not to fork.
-
:poll_timeout [Float] timeout seconds when polling. Default is 0.1. Lower gives faster response times but uses more CPU.
-
:connection_timeout [Float] timeout seconds for connections. Default is 30.
-
:bind [String|Array] a binding or array of binds. Examples are: “http ://127.0.0.1:6464”, “unix:///tmp/agoo.socket”, “http ://[::1]:6464, or to not restrict the address ”http ://:6464“.
-
:graphql [String] path to GraphQL endpoint if support for GraphQL is desired.
-
:max_push_pending [Integer] maximum number or outstanding push messages, less than 1000.
-
:ssl_cert [String] filepath to the SSL certificate file.
-
:ssl_key [String] filepath to the SSL private key file.
-
:hide_schema [true|false] if true the graphql/schema path is handled.
-
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 |
# File 'ext/agoo/rserver.c', line 324
static VALUE
rserver_init(int argc, VALUE *argv, VALUE self) {
struct _agooErr err = AGOO_ERR_INIT;
int port;
const char *root = NULL;
VALUE options = Qnil;
if (argc < 2 || 3 < argc) {
rb_raise(rb_eArgError, "Wrong number of arguments to Agoo::Server.configure.");
}
port = FIX2INT(argv[0]);
if (Qnil != argv[1]) {
rb_check_type(argv[1], T_STRING);
root = StringValuePtr(argv[1]);
if ('\0' == *root) {
root = NULL;
}
}
if (3 <= argc) {
options = argv[2];
}
if (AGOO_ERR_OK != agoo_server_setup(&err)) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
agoo_server.ctx_nil_value = (void*)Qnil;
agoo_server.env_nil_value = (void*)Qnil;
if (AGOO_ERR_OK != configure(&err, port, root, options)) {
rb_raise(rb_eArgError, "%s", err.msg);
}
agoo_server.inited = true;
return Qnil;
}
|
.path_group(path, dirs) ⇒ Object
call-seq: path_group(path, dirs)
Sets up a path group where the path defines a group of directories to search for a file. For example a path of ‘/assets’ could be mapped to a set of [ ‘home/user/images’, ‘/home/user/app/assets/images’ ].
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 |
# File 'ext/agoo/rserver.c', line 1141
static VALUE
path_group(VALUE self, VALUE path, VALUE dirs) {
struct _agooErr err = AGOO_ERR_INIT;
agooGroup g;
rb_check_type(path, T_STRING);
rb_check_type(dirs, T_ARRAY);
if (NULL != (g = agoo_group_create(StringValuePtr(path)))) {
int i;
int dcnt = (int)RARRAY_LEN(dirs);
VALUE entry;
for (i = dcnt - 1; 0 <= i; i--) {
entry = rb_ary_entry(dirs, i);
if (T_STRING != rb_type(entry)) {
entry = rb_funcall(entry, rb_intern("to_s"), 0);
}
if (NULL == agoo_group_add(&err, g, StringValuePtr(entry))) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
}
}
return Qnil;
}
|
.rack_early_hints(on) ⇒ Object
call-seq: rack_early_hints(on)
Turns on or off the inclusion of a early_hints object in the rack call env Hash. If the argument is nil then the current value is returned.
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 |
# File 'ext/agoo/rserver.c', line 1247
static VALUE
rack_early_hints(VALUE self, VALUE on) {
if (Qtrue == on) {
agoo_server.rack_early_hints = true;
} else if (Qfalse == on) {
agoo_server.rack_early_hints = false;
} else if (Qnil == on) {
on = agoo_server.rack_early_hints ? Qtrue : Qfalse;
} else {
rb_raise(rb_eArgError, "rack_early_hints can only be set to true or false");
}
return on;
}
|
.shutdown ⇒ Object
call-seq: shutdown()
Shutdown the server. Logs and queues are flushed before shutting down.
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 |
# File 'ext/agoo/rserver.c', line 946
VALUE
rserver_shutdown(VALUE self) {
if (agoo_server.inited) {
agoo_server_shutdown("Agoo", stop_runners);
if (1 < the_rserver.worker_cnt && getpid() == *the_rserver.worker_pids) {
int i;
int status;
int exit_cnt = 1;
int j;
for (i = 1; i < the_rserver.worker_cnt; i++) {
kill(the_rserver.worker_pids[i], SIGKILL);
}
for (j = 0; j < 20; j++) {
for (i = 1; i < the_rserver.worker_cnt; i++) {
if (0 == the_rserver.worker_pids[i]) {
continue;
}
if (0 < waitpid(the_rserver.worker_pids[i], &status, WNOHANG)) {
if (WIFEXITED(status)) {
//printf("exited, status=%d for %d\n", agoo_server.worker_pids[i], WEXITSTATUS(status));
the_rserver.worker_pids[i] = 0;
exit_cnt++;
} else if (WIFSIGNALED(status)) {
//printf("*** killed by signal %d for %d\n", agoo_server.worker_pids[i], WTERMSIG(status));
the_rserver.worker_pids[i] = 0;
exit_cnt++;
}
}
}
if (the_rserver.worker_cnt <= exit_cnt) {
break;
}
dsleep(0.2);
}
if (exit_cnt < the_rserver.worker_cnt) {
printf("*-*-* Some workers did not exit.\n");
}
}
}
return Qnil;
}
|
.start ⇒ Object
call-seq: start()
Start the server.
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 |
# File 'ext/agoo/rserver.c', line 822
static VALUE
rserver_start(VALUE self) {
VALUE *vp;
int i;
int pid;
double giveup;
struct _agooErr err = AGOO_ERR_INIT;
VALUE agoo = rb_const_get_at(rb_cObject, rb_intern("Agoo"));
VALUE v = rb_const_get_at(agoo, rb_intern("VERSION"));
ID after = rb_intern("after");
*the_rserver.worker_pids = getpid();
// If workers then set the loop_max based on the expected number of
// threads per worker.
if (1 < the_rserver.worker_cnt) {
agoo_server.loop_max /= the_rserver.worker_cnt;
if (agoo_server.loop_max < 1) {
agoo_server.loop_max = 1;
}
}
if (AGOO_ERR_OK != setup_listen(&err)) {
rb_raise(rb_eIOError, "%s", err.msg);
}
if (1 < the_rserver.worker_cnt && the_rserver.forker != Qnil) {
ID before = rb_intern("before");
if (rb_respond_to(the_rserver.forker, before)) {
rb_funcall(the_rserver.forker, before, 0);
}
}
for (i = 1; i < the_rserver.worker_cnt; i++) {
VALUE rpid = rb_funcall(rb_cObject, rb_intern("fork"), 0);
if (Qnil == rpid) {
pid = 0;
} else {
pid = NUM2INT(rpid);
}
if (0 > pid) { // error, use single process
agoo_log_cat(&agoo_error_cat, "Failed to fork. %s.", strerror(errno));
break;
} else if (0 == pid) {
if (AGOO_ERR_OK != agoo_log_start(&err, true)) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
break;
} else {
the_rserver.worker_pids[i] = pid;
}
}
if (1 < the_rserver.worker_cnt && the_rserver.forker != Qnil && rb_respond_to(the_rserver.forker, after)) {
rb_funcall(the_rserver.forker, after, 0);
}
if (AGOO_ERR_OK != agoo_server_start(&err, "Agoo", StringValuePtr(v))) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
if (0 >= agoo_server.thread_cnt) {
agooReq req;
while (agoo_server.active) {
if (NULL != (req = (agooReq)agoo_queue_pop(&agoo_server.eval_queue, poll_timeout))) {
handle_protected(req, false);
agoo_req_destroy(req);
} else {
rb_thread_schedule();
}
if (agoo_stop) {
agoo_shutdown();
break;
}
}
} else {
if (NULL == (the_rserver.eval_threads = (VALUE*)AGOO_MALLOC(sizeof(VALUE) * (agoo_server.thread_cnt + 1)))) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for the thread pool.");
}
for (i = agoo_server.thread_cnt, vp = the_rserver.eval_threads; 0 < i; i--, vp++) {
*vp = rb_thread_create(wrap_process_loop, NULL);
}
*vp = Qnil;
giveup = dtime() + 1.0;
while (dtime() < giveup) {
// The processing threads will not start until this thread
// releases ownership so do that and then see if the threads have
// been started yet.
rb_thread_schedule();
if (agoo_server.loop_cnt + 1 + agoo_server.thread_cnt <= (long)atomic_load(&agoo_server.running)) {
break;
}
dsleep(0.05);
}
}
rb_set_end_proc(stop_server, Qnil);
return Qnil;
}
|
.use(*args) ⇒ Object
call-seq: use(middleware, *args)
The use function must be called before the handle functions. Any invocations of use apply only to handlers called after the call to use.
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 |
# File 'ext/agoo/rserver.c', line 1268
static VALUE
use(int argc, VALUE *argv, VALUE self) {
VALUE mc;
RUse u;
if (argc < 1) { // at least the middleware class must be provided.
rb_raise(rb_eArgError, "no middleware class provided");
}
mc = argv[0];
if (T_CLASS != rb_type(mc)) {
rb_raise(rb_eArgError, "the first argument to use must be a class");
}
if (NULL == (u = (RUse)AGOO_MALLOC(sizeof(struct _rUse)))) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for a middleware use.");
}
u->clas = mc;
u->argc = argc;
if (NULL == (u->argv = (VALUE*)AGOO_MALLOC(sizeof(VALUE) * u->argc))) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for a middleware use.");
}
memcpy(u->argv, argv, argc * sizeof(VALUE));
u->next = the_rserver.uses;
the_rserver.uses = u;
return Qnil;
}
|