JSON support, thanks jansson

This commit is contained in:
roberto@sirius
2011-04-14 09:32:17 +02:00
parent 1db64b72ae
commit 81fd7757c1
6 changed files with 167 additions and 1 deletions
+1
View File
@@ -2,6 +2,7 @@
xml = true
ini = true
yaml = true
json = auto
snmp = true
sctp = false
spooler = true
+100
View File
@@ -0,0 +1,100 @@
#ifdef UWSGI_JSON
#include "uwsgi.h"
extern struct uwsgi_server uwsgi;
#include <jansson.h>
/*
I really love the jansson library...
*/
void uwsgi_json_config(char *file, char *magic_table[]) {
int len = 0;
char *json_data;
const char *key;
json_t *root;
json_error_t error;
json_t *config;
json_t *config_value, *config_array_item;
void *config_iter;
char *object_asked = "uwsgi";
char *colon;
int i;
colon = uwsgi_get_last_char(file, ':');
if (colon) {
colon[0] = 0;
if (colon[1] != 0) {
object_asked = colon+1;
}
}
uwsgi_log("[uWSGI] getting JSON configuration from %s\n", file);
json_data = uwsgi_open_and_read(file, &len, 1, magic_table);
root = json_loads(json_data, 0, &error);
if (!root) {
uwsgi_log("error parsing JSON data: line %d %s\n", error.line, error.text);
exit(1);
}
config = json_object_get(root, object_asked);
if (!json_is_object(config)) {
uwsgi_log("you must define a object named %s in your JSON data\n", object_asked);
exit(1);
}
config_iter = json_object_iter(config);
while(config_iter) {
key = json_object_iter_key(config_iter);
config_value = json_object_iter_value(config_iter);
if (json_is_string(config_value)) {
add_exported_option((char *)key, (char *) json_string_value(config_value), 0);
}
else if (json_is_true(config_value)) {
add_exported_option((char *)key, (char *) "1", 0);
}
else if (json_is_false(config_value) || json_is_null(config_value)) {
add_exported_option((char *)key, (char *) "0", 0);
}
else if (json_is_integer(config_value)) {
add_exported_option((char *)key, uwsgi_num2str(json_integer_value(config_value)), 0);
}
else if (json_is_array(config_value)) {
for(i=0;i<(int)json_array_size(config_value);i++) {
config_array_item = json_array_get(config_value, i);
if (json_is_string(config_array_item)) {
add_exported_option((char *)key, (char *) json_string_value(config_array_item), 0);
}
else if (json_is_true(config_array_item)) {
add_exported_option((char *)key, (char *) "1", 0);
}
else if (json_is_false(config_array_item) || json_is_null(config_array_item)) {
add_exported_option((char *)key, (char *) "0", 0);
}
else if (json_is_integer(config_array_item)) {
add_exported_option((char *)key, uwsgi_num2str(json_integer_value(config_array_item)), 0);
}
}
}
config_iter = json_object_iter_next(config, config_iter);
}
}
#endif
+30
View File
@@ -68,6 +68,7 @@ static struct option long_base_options[] = {
{"reload-mercy", required_argument, 0, LONG_ARGS_RELOAD_MERCY},
{"exit-on-reload", no_argument, &uwsgi.exit_on_reload, 1},
{"help", no_argument, 0, 'h'},
{"usage", no_argument, 0, 'h'},
{"reaper", no_argument, 0, 'r'},
{"max-requests", required_argument, 0, 'R'},
{"socket-timeout", required_argument, 0, 'z'},
@@ -97,6 +98,9 @@ static struct option long_base_options[] = {
{"yaml", required_argument, 0, 'y'},
{"yml", required_argument, 0, 'y'},
#endif
#ifdef UWSGI_JSON
{"json", required_argument, 0, 'j'},
#endif
#ifdef UWSGI_LDAP
{"ldap", required_argument, 0, LONG_ARGS_LDAP},
{"ldap-schema", no_argument, 0, LONG_ARGS_LDAP_SCHEMA},
@@ -661,6 +665,11 @@ int main(int argc, char *argv[], char *envp[])
else if (!strcmp(lazy+strlen(lazy)-4, ".yml")) {
uwsgi.yaml = lazy;
}
#endif
#ifdef UWSGI_JSON
else if (!strcmp(lazy+strlen(lazy)-3, ".js")) {
uwsgi.json = lazy;
}
#endif
// manage magic mountpoint
else if ( (lazy[0] == '/' || strchr(lazy, '|')) && strchr(lazy,'=')) {
@@ -748,6 +757,22 @@ int main(int argc, char *argv[], char *envp[])
uwsgi_yaml_config(uwsgi.yaml, magic_table);
}
#endif
#ifdef UWSGI_JSON
if (uwsgi.json != NULL) {
magic_table['o'] = uwsgi.json;
if (uwsgi.json[0] == '/') {
magic_table['p'] = uwsgi.json;
}
else {
magic_table['p'] = uwsgi_concat3(uwsgi.cwd,"/",uwsgi.json);
}
magic_table['s'] = uwsgi_get_last_char(magic_table['p'], '/')+1;
magic_table['d'] = uwsgi_concat2n(magic_table['p'], magic_table['s']-magic_table['p'], "", 0);
if (uwsgi_get_last_char(uwsgi.json, '.')) magic_table['e'] = uwsgi_get_last_char(uwsgi.json, '.')+1;
if (uwsgi_get_last_char(magic_table['s'], '.')) magic_table['n'] = uwsgi_concat2n(magic_table['s'], uwsgi_get_last_char(magic_table['s'], '.')-magic_table['s'], "", 0) ;
uwsgi_json_config(uwsgi.json, magic_table);
}
#endif
#ifdef UWSGI_LDAP
if (uwsgi.ldap != NULL) {
uwsgi_ldap_config();
@@ -2582,6 +2607,11 @@ end:
uwsgi.yaml = optarg;
return 1;
#endif
#ifdef UWSGI_JSON
case 'j':
uwsgi.json = optarg;
return 1;
#endif
#ifdef UWSGI_INI
case LONG_ARGS_INI:
uwsgi.ini = optarg;
+8
View File
@@ -967,6 +967,10 @@ struct uwsgi_server {
char *yaml;
#endif
#ifdef UWSGI_JSON
char *json;
#endif
#ifdef UWSGI_INI
char *ini;
#endif
@@ -1425,6 +1429,10 @@ void uwsgi_ini_config(char *, char*[]);
void uwsgi_yaml_config(char *, char*[]);
#endif
#ifdef UWSGI_JSON
void uwsgi_json_config(char *, char*[]);
#endif
#ifdef UWSGI_LDAP
void uwsgi_ldap_schema_dump(void);
+17 -1
View File
@@ -25,7 +25,7 @@ if not GCC:
def spcall(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,stderr=open('/dev/null','w'))
if p.wait() == 0:
if sys.version_info[0] > 2:
@@ -392,6 +392,22 @@ class uConf(object):
self.cflags.append("-DUWSGI_YAML")
self.gcc_list.append('yaml')
if self.get('json'):
if self.get('json') == 'auto':
jsonconf = spcall("pkg-config --cflags jansson")
if jsonconf:
self.cflags.append(jsonconf)
self.gcc_list.append('json')
self.libs.append(spcall("pkg-config --libs jansson"))
elif os.path.exists('/usr/include/jansson.h') or os.path.exists('/usr/local/include/jansson.h'):
self.cflags.append("-DUWSGI_JSON")
self.gcc_list.append('json')
self.libs.append('-ljansson')
else:
self.cflags.append("-DUWSGI_JSON")
self.gcc_list.append('json')
self.libs.append('-ljansson')
if self.get('ldap'):
if self.get('ldap') == 'auto':
if os.path.exists('/usr/include/ldap.h'):
+11
View File
@@ -0,0 +1,11 @@
{
"uwsgi": {
"http": ":8080",
"workers": 8,
"module": "werkzeug.testapp:test_app",
"master": true,
"socket": [ "/tmp/uwsgi.sock", "127.0.0.1:3031" ],
"pythonpath": [ "/foo", "/bar" ],
"show-config": true
}
}