Take config as commandline parameters
This commit is contained in:
47
src/main.c
47
src/main.c
@@ -5,7 +5,7 @@
|
||||
#define NOH_IMPLEMENTATION
|
||||
#include "noh.h"
|
||||
|
||||
|
||||
#define NO_AUTH
|
||||
/* To disable Basic auth for testing:
|
||||
#define NO_AUTH
|
||||
This will ignore any auth headers and always log in with username demo.
|
||||
@@ -14,8 +14,6 @@ This will ignore any auth headers and always log in with username demo.
|
||||
#define SV Noh_String_View
|
||||
#define sv noh_sv_from_cstr
|
||||
|
||||
Noh_Arena temp;
|
||||
|
||||
static enum MHD_Result send_response(struct MHD_Connection *connection, int status, SV message) {
|
||||
struct MHD_Response *response = MHD_create_response_from_buffer(
|
||||
message.count, (void*)message.elems, MHD_RESPMEM_PERSISTENT);
|
||||
@@ -26,13 +24,15 @@ static enum MHD_Result send_response(struct MHD_Connection *connection, int stat
|
||||
return ret;
|
||||
}
|
||||
|
||||
static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url) {
|
||||
static enum MHD_Result handle_opds(struct MHD_Connection *connection, SV url, SV username) {
|
||||
(void)url;
|
||||
(void)username;
|
||||
return send_response(connection, 200, sv("TODO: OPDS\n"));
|
||||
}
|
||||
|
||||
static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url) {
|
||||
static enum MHD_Result handle_get(struct MHD_Connection *connection, SV url, SV username) {
|
||||
(void)url;
|
||||
(void)username;
|
||||
return send_response(connection, 200, sv("TODO: Get\n"));
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ bool is_slash(const char c) { return c == '/'; }
|
||||
|
||||
static enum MHD_Result request_handler(
|
||||
void *cls,
|
||||
struct MHD_Connection *connection,
|
||||
struct MHD_Connection *connection,
|
||||
const char *_url, const char *method, const char *version,
|
||||
const char *upload_data, size_t *upload_data_size,
|
||||
void **con_cls) {
|
||||
@@ -67,7 +67,7 @@ static enum MHD_Result request_handler(
|
||||
if (strcmp(method, "GET") != 0) return send_response(connection, 405, sv(""));
|
||||
|
||||
SV url = sv(_url);
|
||||
noh_sv_trim_left(&url, is_slash);;
|
||||
noh_sv_trim_left(&url, is_slash);
|
||||
SV controller = noh_sv_chop_by_delim(&url, '/');
|
||||
|
||||
if (noh_sv_eq_ci(controller, sv("opds"))) return handle_opds(connection, url, username);
|
||||
@@ -75,18 +75,39 @@ static enum MHD_Result request_handler(
|
||||
return send_response(connection, 404, sv(""));
|
||||
}
|
||||
|
||||
void show_usage(char *program_name, char *error) {
|
||||
noh_log(NOH_INFO, "Usage:");
|
||||
noh_log(NOH_INFO, "%s port library_path", program_name);
|
||||
noh_log(NOH_INFO, " port: The port to listen to, between 1000 and 65535.");
|
||||
noh_log(NOH_INFO, " library_path: The path where the epub libraries are located.");
|
||||
noh_log(NOH_INFO, " The root of this path should have a users.txt file with the usernames and passwords of valid users.");
|
||||
noh_log(NOH_INFO, " For each user, ther must be a subdirectory where this user's library is located.");
|
||||
noh_log(NOH_INFO, "");
|
||||
noh_log(NOH_ERROR, "%s", error);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
temp = noh_arena_init(1 * 1024 * 1024);
|
||||
// Parse input parameters.
|
||||
char *program_name = noh_shift_args(&argc, &argv);
|
||||
|
||||
if (argc < 1) show_usage(program_name, "Port not provided.");
|
||||
char *port_str = noh_shift_args(&argc, &argv);
|
||||
|
||||
int port = atoi(port_str);
|
||||
if (port < 1000 || port > 65535) show_usage(program_name, "Invalid port.");
|
||||
|
||||
if (argc < 1) show_usage(program_name, "Library path not provided.");
|
||||
|
||||
struct MHD_Daemon *server;
|
||||
|
||||
// Start the HTTP server
|
||||
server = MHD_start_daemon(
|
||||
MHD_USE_INTERNAL_POLLING_THREAD,
|
||||
5000,
|
||||
NULL, NULL,
|
||||
&request_handler, NULL,
|
||||
port,
|
||||
NULL, NULL,
|
||||
&request_handler, NULL,
|
||||
MHD_OPTION_END);
|
||||
|
||||
if (!server) {
|
||||
@@ -94,7 +115,7 @@ int main(void)
|
||||
return 1;
|
||||
}
|
||||
|
||||
printf("Server is running on http://localhost:5000\n");
|
||||
printf("Server is running on http://localhost:%d\n", port);
|
||||
|
||||
// Keep the server running
|
||||
getchar();
|
||||
|
||||
Reference in New Issue
Block a user