aboutsummaryrefslogtreecommitdiff
path: root/m2.c
blob: cb83d9154edd928bc14a1a41f39760c9d8af0596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#define _POSIX_C_SOURCE 200809L

#include <fcgiapp.h>
#include <sqlite3.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

static sqlite3 *db = NULL;
static sqlite3_stmt *stmt = NULL;

static void cleanup(void) {
    if (stmt) {
        sqlite3_finalize(stmt);
    }
    if (db) {
        sqlite3_close(db);
    }
}

int main(int argc, char **argv) {
    if (argc != 2) {
        fprintf(stderr, "Usage: %s /path/to/data.db\n", argv[0]);
        return 64;
    }
    atexit(cleanup);
    int r;
    if ((r = sqlite3_open_v2(argv[1], &db, SQLITE_OPEN_READONLY, NULL)) != SQLITE_OK) {
        fprintf(stderr, "Cannot open database: %s\n", sqlite3_errstr(r));
        return r;
    }
    if ((r = sqlite3_prepare(db, "SELECT title,title_zh,text,text_zh FROM v_random_quote", -1, &stmt, NULL)) != SQLITE_OK) {
        fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errstr(r));
        return r;
    }
    FCGX_Stream *in, *out, *err;
    FCGX_ParamArray envp;
    while(FCGX_Accept(&in, &out, &err, &envp) >= 0) {
        const char *url = FCGX_GetParam("REQUEST_URI", envp);
        switch (r = sqlite3_step(stmt)) {
            case SQLITE_DONE:
                FCGX_FPrintF(out,
                        "Status: 500 Internal Server Error\r\n"
                        "Content-type: text/plain\r\n"
                        "\r\n"
                        "No data is available.\n");
                break;
            case SQLITE_BUSY:
            case SQLITE_MISUSE:
            case SQLITE_ERROR:
                fprintf(stderr, "Cannot query: %d\n", r);
                FCGX_FPrintF(out,
                        "Status: 500 Internal Server Error\r\n"
                        "Content-type: text/plain\r\n"
                        "\r\n"
                        "%s\n",
                        sqlite3_errstr(r));
                break;
            case SQLITE_ROW:
                char *title = (char *) sqlite3_column_text(stmt, 0);
                char *title_zh = (char *) sqlite3_column_text(stmt, 1);
                char *text = (char *) sqlite3_column_text(stmt, 2);
                char *text_zh = (char *) sqlite3_column_text(stmt, 3);
                if (!strncmp(url, "/hitokoto.css", 13)) {
                    FCGX_FPrintF(out,
                            "Content-Type: text/css\r\n"
                            "\r\n"
                            "#hitokoto::before { content: \"%s\" }\n"
                            "#hitokoto::after { content: \"%s\" }\n"
                            "#hitokoto_zh::before { content: \"%s\" }\n"
                            "#hitokoto_zh::after { content: \"%s\" }\n",
                            text, title,
                            text_zh ? text_zh : "", title_zh ? title_zh : "");
                } else {
                    FCGX_FPrintF(out,
                            "Content-type: application/json\r\n"
                            "\r\n"
                            "{\"title\":\"%s\",\"title_zh\":\"%s\",\"text\":\"%s\",\"text_zh\":\"%s\"}\n",
                            title,
                            title_zh ? title_zh : "",
                            text,
                            text_zh ? text_zh : "");
                }
                break;
        }
        if ((r = sqlite3_reset(stmt))) {
            fprintf(stderr, "sqlite3_reset: %d\n", r);
        }
    }
}