Move router building function into a more sensible location.

This commit is contained in:
Jordan Bancino 2023-04-14 21:37:00 +00:00
parent 83971dfaff
commit b21d018daa
6 changed files with 81 additions and 54 deletions

View file

@ -48,6 +48,7 @@ Milestone: v0.3.0
[x] Support regex matching [x] Support regex matching
[x] Replace current routing system [x] Replace current routing system
[ ] Add route for requestToken endpoints [ ] Add route for requestToken endpoints
[x] Move TelodendriaBuildRouter() to Routes
[ ] Token permissions [ ] Token permissions
[ ] Move configuration to database [ ] Move configuration to database

View file

@ -39,6 +39,7 @@
#include <HashMap.h> #include <HashMap.h>
#include <Json.h> #include <Json.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <Routes.h>
#include <Matrix.h> #include <Matrix.h>
#include <Db.h> #include <Db.h>
#include <Cron.h> #include <Cron.h>
@ -179,7 +180,7 @@ main(int argc, char **argv)
} }
Log(LOG_NOTICE, "Building routing tree..."); Log(LOG_NOTICE, "Building routing tree...");
matrixArgs.router = TelodendriaBuildRouter(); matrixArgs.router = RouterBuild();
if (!matrixArgs.router) if (!matrixArgs.router)
{ {
Log(LOG_ERR, "Unable to build routing tree."); Log(LOG_ERR, "Unable to build routing tree.");

74
src/Routes.c Normal file
View file

@ -0,0 +1,74 @@
/*
* Copyright (C) 2022-2023 Jordan Bancino <@jordan:bancino.net>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <Routes.h>
HttpRouter *
RouterBuild(void)
{
HttpRouter *router = HttpRouterCreate();
if (!router)
{
return NULL;
}
#define R(path, func) \
if (!HttpRouterAdd(router, path, func)) \
{ \
Log(LOG_ERR, "Unable to add route: %s", path); \
HttpRouterFree(router); \
return NULL; \
}
R("/.well-known/matrix/(client|server)", RouteWellKnown);
R("/_matrix/client/versions", RouteVersions);
R("/_matrix/static", RouteStaticDefault);
R("/_matrix/static/client/login", RouteStaticLogin);
R("/_matrix/client/v3/login", RouteLogin);
R("/_matrix/client/v3/logout", RouteLogout);
R("/_matrix/client/v3/logout/(all)", RouteLogout);
R("/_matrix/client/v3/register", RouteRegister);
R("/_matrix/client/v3/register/(available)", RouteRegister);
R("/_matrix/client/v3/refresh", RouteRefresh);
R("/_matrix/client/v3/account/whoami", RouteWhoami);
R("/_matrix/client/v3/account/password", RouteChangePwd);
R("/_matrix/client/v1/register/m.login.registration_token/validity", RouteTokenValid);
#if 0
R("/_matrix/client/v3/account/password/(email|msisdn)/requestToken", RouteRequestToken);
R("/_matrix/client/v3/register/(email|msisdn)/requestToken", RouteRequestToken);
#endif
R("/_matrix/client/v3/profile/(.*)", RouteUserProfile);
R("/_matrix/client/v3/profile/(.*)/(avatar_url|displayname)", RouteUserProfile);
#undef R
return router;
}

View file

@ -197,53 +197,3 @@ TelodendriaPrintHeader(void)
"Documentation/Support: https://telodendria.io"); "Documentation/Support: https://telodendria.io");
Log(LOG_INFO, ""); Log(LOG_INFO, "");
} }
HttpRouter *
TelodendriaBuildRouter(void)
{
HttpRouter *router = HttpRouterCreate();
if (!router)
{
return NULL;
}
#define R(path, func) \
if (!HttpRouterAdd(router, path, func)) \
{ \
Log(LOG_ERR, "Unable to add route: %s", path); \
HttpRouterFree(router); \
return NULL; \
}
R("/.well-known/matrix/(client|server)", RouteWellKnown);
R("/_matrix/client/versions", RouteVersions);
R("/_matrix/static", RouteStaticDefault);
R("/_matrix/static/client/login", RouteStaticLogin);
R("/_matrix/client/v3/login", RouteLogin);
R("/_matrix/client/v3/logout", RouteLogout);
R("/_matrix/client/v3/logout/(all)", RouteLogout);
R("/_matrix/client/v3/register", RouteRegister);
R("/_matrix/client/v3/register/(available)", RouteRegister);
R("/_matrix/client/v3/refresh", RouteRefresh);
R("/_matrix/client/v3/account/whoami", RouteWhoami);
R("/_matrix/client/v3/account/password", RouteChangePwd);
R("/_matrix/client/v1/register/m.login.registration_token/validity", RouteTokenValid);
#if 0
R("/_matrix/client/v3/account/password/(email|msisdn)/requestToken", RouteRequestToken);
R("/_matrix/client/v3/register/(email|msisdn)/requestToken", RouteRequestToken);
#endif
R("/_matrix/client/v3/profile/(.*)", RouteUserProfile);
R("/_matrix/client/v3/profile/(.*)/(avatar_url|displayname)", RouteUserProfile);
#undef R
return router;
}

View file

@ -29,6 +29,7 @@
#include <HashMap.h> #include <HashMap.h>
#include <Array.h> #include <Array.h>
#include <HttpServer.h> #include <HttpServer.h>
#include <HttpRouter.h>
#include <Matrix.h> #include <Matrix.h>
#define MATRIX_PATH_EQUALS(pathPart, str) \ #define MATRIX_PATH_EQUALS(pathPart, str) \
@ -40,6 +41,9 @@ typedef struct RouteArgs
HttpServerContext *context; HttpServerContext *context;
} RouteArgs; } RouteArgs;
HttpRouter *
RouterBuild(void);
#define ROUTE(name) \ #define ROUTE(name) \
extern void * \ extern void * \
name(Array *, void *) name(Array *, void *)

View file

@ -49,7 +49,4 @@ extern void
extern void extern void
TelodendriaPrintHeader(void); TelodendriaPrintHeader(void);
extern HttpRouter *
TelodendriaBuildRouter(void);
#endif #endif