Start implementing server

master
teknomunk 7 months ago
parent 8efab0f0b3
commit 3f43c2085d

1
.gitignore vendored

@ -2,3 +2,4 @@ obj
data
squishyfish
squishyfish.debug
*.inc

@ -0,0 +1,26 @@
#include "model/video.h"
#include "controller/channel.h"
#include "http/server/request.h"
#include <string.h>
#include <stdio.h>
void show_video_list( struct http_request* req, struct video_list* vl )
{
http_request_send_headers( req, 200, "text/html", true );
FILE* f = http_request_get_response_body( req );
#include "view/video/list.html.inc"
}
bool handle_channel( struct http_request* req )
{
struct video_list vl;
memset(&vl,0,sizeof(vl));
show_video_list(req,&vl);
video_list_free(&vl);
return true;
}

@ -0,0 +1,8 @@
#pragma once
#include <stdbool.h>
struct http_request;
bool handle_channel( struct http_request* req );

@ -0,0 +1,28 @@
#include "controller/main.h"
#include "controller/channel.h"
// Submodules
#include "http/server/request.h"
bool route_request( struct http_request* req )
{
if( http_request_route( req, "/@" ) ) {
// Channels
return handle_channel( req );
}
return false;
}
void handle_request( struct http_request* req, void* )
{
//printf( "Handling request from %s\n", http_request_get_remote_host_address( req ) );
if( !route_request( req ) ) {
FILE* f = http_request_get_response_body( req );
http_request_send_headers( req, 404, "text/html", true );
#include "view/404.html.inc"
}
fflush(stdout);
}

@ -0,0 +1,7 @@
#pragma once
#include <stdbool.h>
struct http_request;
void handle_request( struct http_request* req, void* );

@ -1,17 +1,40 @@
#include "model/server.h"
#include "controller/main.h"
// Submodules
#include "http/server/server.h"
// Platform libraries
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/stat.h>
#include <unistd.h>
void init()
{
mkdir("data",0640);
mkdir("data/videos",0640);
mkdir("data/channels",0640);
}
volatile bool terminate;
int main( int argc, char* argv[] )
{
init();
struct server_settings settings;
server_settings_new_composite( &settings );
struct http_server* srv = http_server_new( &settings.http_args, handle_request, NULL );
while( !terminate ) {
http_server_process(srv);
usleep(25000);
}
http_server_release(srv);
return EXIT_SUCCESS;
}

@ -0,0 +1,11 @@
#pragma once
struct channel
{
int id;
};
struct channel* channel_new();
struct channel* channel_from_id( int id );
void channel_free( struct channel* c );

@ -0,0 +1,10 @@
#include "model/server.h"
#include <string.h>
void server_settings_new_composite( struct server_settings* settings )
{
memset(settings,0,sizeof(*settings));
settings->http_args.bind_port = 9826;
}

@ -0,0 +1,11 @@
#pragma once
#include "http/server/server.h"
struct server_settings
{
struct http_server_args http_args;
};
void server_settings_new_composite( struct server_settings* );

@ -0,0 +1,76 @@
#include "video.h"
#include "json/layout.h"
#include "util/dir.h"
#include <stdlib.h>
#include <string.h>
#include <libgen.h>
#define OBJ_TYPE struct video
static struct json_object_field video_layout[] = {
JSON_FIELD_STRING( video_file, true ),
JSON_FIELD_STRING( title, false ),
JSON_FIELD_STRING( description, false ),
JSON_FIELD_STRING( original_url, false ),
JSON_FIELD_INTEGER( owner_id, true ),
JSON_FIELD_END
};
#undef OBJ_TYPE
struct video* video_new()
{
struct video* v;
v = malloc(sizeof(*v));
memset(v,0,sizeof(*v));
return v;
}
static const char* get_video_data_filename( int id, char* buffer, int buffer_size )
{
int ones = id % 1000; id /= 1000;
int thousands = id % 1000; id /= 100;
int millions = id % 1000;
snprintf( buffer, buffer_size, "data/videos/%03dm/%03dk/%03d.json", ones, thousands, millions );
return buffer;
}
struct video* video_from_id( int id )
{
if( id <= 0 ) { return NULL; };
char buffer[512];
const char* filename = get_video_data_filename( id, buffer, sizeof(buffer) );
struct video* v = video_new();
if( !json_read_object_layout_from_file( filename, video_layout, v ) ) {
video_free(v);
return NULL;
}
return v;
}
void video_free( struct video* v )
{
if( !v ) { return; }
free(v);
}
void video_list_free( struct video_list* vl )
{
for( int i = 0; i < vl->count; ++i ) {
video_free( vl->items[i] );
}
free( vl->items );
memset(vl,0,sizeof(*vl));
}

@ -0,0 +1,26 @@
#pragma once
struct video
{
int id;
char* video_file;
char* title;
char* description;
char* original_url;
int owner_id;
};
struct video_list
{
struct video** items;
int count;
};
struct video* video_new();
struct video* video_from_id( int id );
void video_free( struct video* v );
void video_list_free( struct video_list* vl );

@ -1 +1 @@
Subproject commit e133427bb7da0224fc365252406024c207418863
Subproject commit ebdef40c7a028d15cf061c37c777bf3e0d22c938

@ -0,0 +1,5 @@
<html>
<body>
Page not found
</body>
</html>

@ -0,0 +1,8 @@
<html>
<body>
%( for( int i = 0; i < vl->count; ++i ) { )
TODO: put video here
%( } )
</body>
</html>
Loading…
Cancel
Save