mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 13:45:10 +00:00
[ADD/WIP] Routerwerk!
This commit is contained in:
parent
25ed114d0f
commit
47c98cbbe3
8 changed files with 280 additions and 1 deletions
2
Makefile
2
Makefile
|
|
@ -16,7 +16,7 @@ CYTO_LIB=/usr/local/lib # Where's Cytoplasm's library is
|
|||
|
||||
SOURCE=src
|
||||
OBJECT=build
|
||||
INCLUDES=include
|
||||
INCLUDES=src/include
|
||||
CC=cc
|
||||
CFLAGS=-I$(INCLUDES) -I$(CYTO_INC) -DNAME="\"$(NAME)\"" -DVERSION="\"$(VERSION)\"" -g -ggdb
|
||||
LDFLAGS=-L $(CYTO_LIB) -lCytoplasm -Wl,--export-dynamic
|
||||
|
|
|
|||
52
src/HttParsee.c
Normal file
52
src/HttParsee.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <Parsee.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Json.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
#include <Routes.h>
|
||||
|
||||
void
|
||||
ParseeRequest(HttpServerContext *ctx, void *argp)
|
||||
{
|
||||
ParseeData *data = argp;
|
||||
char *path = HttpRequestPath(ctx);
|
||||
HashMap *response = NULL;
|
||||
Stream *stream = HttpServerStream(ctx);
|
||||
int encodedSize;
|
||||
char *encodedStr;
|
||||
ParseeHttpArg arg;
|
||||
|
||||
/* Basic headers */
|
||||
HttpResponseStatus(ctx, HTTP_OK);
|
||||
HttpResponseHeader(ctx, "Server", NAME "/v" VERSION);
|
||||
HttpResponseHeader(ctx, "Connection", "close");
|
||||
|
||||
arg.data = data;
|
||||
arg.ctx = ctx ;
|
||||
|
||||
arg.stream = stream;
|
||||
|
||||
if (!HttpRouterRoute(data->router, path, &arg, (void **) &response))
|
||||
{
|
||||
HttpResponseStatus(ctx, HTTP_NOT_FOUND);
|
||||
JsonFree(response);
|
||||
response = NULL;
|
||||
/* TODO: Set a thing */
|
||||
}
|
||||
|
||||
/* Whatever, we routed a thing. */
|
||||
if (response)
|
||||
{
|
||||
encodedSize = JsonEncode(response, NULL, JSON_DEFAULT);
|
||||
encodedStr = StrInt(encodedSize);
|
||||
HttpResponseHeader(ctx, "Content-Length", encodedStr);
|
||||
HttpResponseHeader(ctx, "Content-Type", "application/json");
|
||||
Free(encodedStr);
|
||||
|
||||
HttpSendHeaders(ctx);
|
||||
JsonEncode(response, stream, JSON_DEFAULT);
|
||||
return;
|
||||
}
|
||||
}
|
||||
28
src/Main.c
28
src/Main.c
|
|
@ -1,9 +1,37 @@
|
|||
#include <Cytoplasm/HttpServer.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <Parsee.h>
|
||||
|
||||
int
|
||||
Main(void)
|
||||
{
|
||||
HttpServer *server = NULL;
|
||||
HttpServerConfig conf;
|
||||
ParseeData *data;
|
||||
const ParseeConfig *parseeConf;
|
||||
|
||||
Log(LOG_INFO, "%s - v%s", NAME, VERSION);
|
||||
ParseeConfigInit();
|
||||
ParseeExportConfigYAML(StreamStdout());
|
||||
|
||||
parseeConf = ParseeConfigGet();
|
||||
|
||||
memset(&conf, 0, sizeof(conf));
|
||||
conf.port = parseeConf->port;
|
||||
conf.threads = 4;
|
||||
conf.maxConnections = 32;
|
||||
conf.handlerArgs = ParseeInitData();
|
||||
conf.handler = ParseeRequest;
|
||||
|
||||
/* TODO: The rest of Parsee. */
|
||||
server = HttpServerCreate(&conf);
|
||||
HttpServerStart(server);
|
||||
HttpServerJoin(server);
|
||||
|
||||
HttpServerFree(server);
|
||||
ParseeConfigFree();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
69
src/ParseeConfig.c
Normal file
69
src/ParseeConfig.c
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
#include <Parsee.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
|
||||
static ParseeConfig *config = NULL;
|
||||
|
||||
void
|
||||
ParseeConfigInit(void)
|
||||
{
|
||||
if (config)
|
||||
{
|
||||
return;
|
||||
}
|
||||
config = Malloc(sizeof(*config));
|
||||
config->as_token = StrRandom(32);
|
||||
config->hs_token = StrRandom(32);
|
||||
config->sender_localpart = StrDuplicate("_parsee_bridge");
|
||||
config->namespace_base = StrDuplicate("_parsee_j");
|
||||
config->listen_as = StrDuplicate("localhost");
|
||||
config->port = 7642; /* proposed by Saint */
|
||||
}
|
||||
|
||||
void
|
||||
ParseeExportConfigYAML(Stream *stream)
|
||||
{
|
||||
if (!stream || !config)
|
||||
{
|
||||
return;
|
||||
}
|
||||
StreamPrintf(stream, "# Autogenerated YAML AS entry for %s\n", NAME);
|
||||
StreamPrintf(stream, "# This file should be *moved* to your\n");
|
||||
StreamPrintf(stream, "# Matrix homeserver's config directory\n");
|
||||
StreamPrintf(stream, "# (as for Conduit, y'all really need a better system)\n");
|
||||
StreamPrintf(stream, "\n");
|
||||
StreamPrintf(stream, "id: \"Parsee XMPP\"\n");
|
||||
StreamPrintf(stream, "url: \"http://%s:%d/\"\n", config->listen_as, config->port);
|
||||
StreamPrintf(stream, "as_token: \"%s\"\n", config->as_token);
|
||||
StreamPrintf(stream, "hs_token: \"%s\"\n", config->hs_token);
|
||||
StreamPrintf(stream, "sender_localpart: \"%s\"\n", config->sender_localpart);
|
||||
StreamPrintf(stream, "\n");
|
||||
StreamPrintf(stream, "namespaces: \n");
|
||||
StreamPrintf(stream, " users:\n");
|
||||
StreamPrintf(stream, " - exclusive: true\n");
|
||||
StreamPrintf(stream, " regex: \"@%s_.*\n", config->namespace_base);
|
||||
StreamPrintf(stream, " aliases:\n");
|
||||
StreamPrintf(stream, " - exclusive: true\n");
|
||||
StreamPrintf(stream, " regex: \"#%s_.*\n", config->namespace_base);
|
||||
}
|
||||
|
||||
void
|
||||
ParseeConfigFree(void)
|
||||
{
|
||||
if (!config)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Free(config->as_token);
|
||||
Free(config->hs_token);
|
||||
Free(config->sender_localpart);
|
||||
Free(config->namespace_base);
|
||||
Free(config->listen_as);
|
||||
Free(config);
|
||||
}
|
||||
const ParseeConfig *
|
||||
ParseeConfigGet(void)
|
||||
{
|
||||
return (const ParseeConfig *) config;
|
||||
}
|
||||
35
src/ParseeData.c
Normal file
35
src/ParseeData.c
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <Parsee.h>
|
||||
|
||||
#include <Cytoplasm/Memory.h>
|
||||
|
||||
#include <Routes.h>
|
||||
|
||||
ParseeData *
|
||||
ParseeInitData(void)
|
||||
{
|
||||
ParseeData *data;
|
||||
if (!ParseeConfigGet())
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
data = Malloc(sizeof(*data));
|
||||
data->config = ParseeConfigGet();
|
||||
data->router = HttpRouterCreate();
|
||||
|
||||
#define X_ROUTE(path, func) HttpRouterAdd(data->router, path, func);
|
||||
ROUTES
|
||||
#undef X_ROUTE
|
||||
return data;
|
||||
}
|
||||
void
|
||||
ParseeFreeData(ParseeData *data)
|
||||
{
|
||||
if (!data)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
HttpRouterFree(data->router);
|
||||
Free(data);
|
||||
}
|
||||
30
src/Routes/Root.c
Normal file
30
src/Routes/Root.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#include <Routes.h>
|
||||
|
||||
RouteHead(RouteRoot, arr, argp)
|
||||
{
|
||||
ParseeHttpArg *args = argp;
|
||||
|
||||
HttpResponseHeader(args->ctx, "Content-Type", "text/html");
|
||||
HttpSendHeaders(args->ctx);
|
||||
StreamPrintf(args->stream, "<html lang=\"en\">");
|
||||
StreamPrintf(args->stream, " <head>");
|
||||
StreamPrintf(args->stream, " <title>Parsee Lander</title>");
|
||||
StreamPrintf(args->stream, " </head>");
|
||||
StreamPrintf(args->stream, " <body>");
|
||||
StreamPrintf(args->stream, " <center><h1>");
|
||||
StreamPrintf(args->stream, " Your Parsee is running!");
|
||||
StreamPrintf(args->stream, " </h1></center>");
|
||||
StreamPrintf(args->stream, " <hr/>");
|
||||
StreamPrintf(args->stream, " <p>");
|
||||
StreamPrintf(args->stream, " Kinda jealous of that, to be ");
|
||||
StreamPrintf(args->stream, " fair.");
|
||||
StreamPrintf(args->stream, " </p>");
|
||||
StreamPrintf(args->stream, " <p>");
|
||||
StreamPrintf(args->stream, " Your homeserver now can interact");
|
||||
StreamPrintf(args->stream, " with the bridge, with the ");
|
||||
StreamPrintf(args->stream, " generated YAML file.");
|
||||
StreamPrintf(args->stream, " </p>");
|
||||
StreamPrintf(args->stream, " </body>");
|
||||
StreamPrintf(args->stream, "</html>");
|
||||
return NULL;
|
||||
}
|
||||
45
src/include/Parsee.h
Normal file
45
src/include/Parsee.h
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#ifndef PARSEE_PARSEE_H
|
||||
#define PARSEE_PARSEE_H
|
||||
|
||||
#include <Cytoplasm/HttpServer.h>
|
||||
#include <Cytoplasm/HttpRouter.h>
|
||||
#include <Cytoplasm/Stream.h>
|
||||
|
||||
typedef struct ParseeConfig {
|
||||
char *as_token, *hs_token;
|
||||
/* id here is "Parsee XMPP". */
|
||||
char *sender_localpart;
|
||||
char *namespace_base;
|
||||
|
||||
char *listen_as;
|
||||
int port;
|
||||
} ParseeConfig;
|
||||
|
||||
typedef struct ParseeData {
|
||||
const ParseeConfig *config;
|
||||
HttpRouter *router;
|
||||
} ParseeData;
|
||||
|
||||
/* Initialises a Parsee config from scratch, and writes to it
|
||||
* as JSON in the CWD. */
|
||||
extern void ParseeConfigInit(void);
|
||||
|
||||
/* Loads a Parsee config from a JSON filepath. */
|
||||
extern void ParseeConfigLoad(char *);
|
||||
|
||||
/* Retrieves the Parsee config if loaded. */
|
||||
extern const ParseeConfig * ParseeConfigGet(void);
|
||||
|
||||
/* Exports the Parsee config as a YAML document. */
|
||||
extern void ParseeExportConfigYAML(Stream *);
|
||||
|
||||
/* Destroys the Parsee configuration */
|
||||
extern void ParseeConfigFree(void);
|
||||
|
||||
/* Creates and destroys a data structure, stored on the heap. */
|
||||
extern ParseeData * ParseeInitData(void);
|
||||
extern void ParseeFreeData(ParseeData *);
|
||||
|
||||
/* HTTP server handler for Parsee, takes in a config. */
|
||||
extern void ParseeRequest(HttpServerContext *, void *);
|
||||
#endif
|
||||
20
src/include/Routes.h
Normal file
20
src/include/Routes.h
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#ifndef PARSEE_ROUTES_H
|
||||
#define PARSEE_ROUTES_H
|
||||
|
||||
#include <Parsee.h>
|
||||
typedef struct ParseeHttpArg {
|
||||
ParseeData *data;
|
||||
HttpServerContext *ctx;
|
||||
Stream *stream;
|
||||
} ParseeHttpArg;
|
||||
|
||||
/* A list of all routes. */
|
||||
#define ROUTES X_ROUTE("/", RouteRoot)
|
||||
|
||||
#define X_ROUTE(path, name) extern void * name(Array *, void *);
|
||||
ROUTES
|
||||
#undef X_ROUTE
|
||||
|
||||
#define RouteHead(name, pathargs, argp) void * \
|
||||
name(Array * pathargs, void *argp)
|
||||
#endif
|
||||
Loading…
Add table
Add a link
Reference in a new issue