Created
April 4, 2012 22:49
-
-
Save MariusSchiffer/2306254 to your computer and use it in GitHub Desktop.
blogapp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* main.c | |
* | |
* Main source in blog_app module. | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <regex.h> | |
#include "xordllist.h" | |
#include "http_request.h" | |
#include "http_response.h" | |
#include "test_server.h" | |
#include "view.h" | |
#include "view_dispatcher.h" | |
#include "safestr.h" | |
#include "dictionary.h" | |
#include "template.h" | |
#include "module.h" | |
#include "fcgi_handler.h" | |
#include "server.h" | |
#include "store.h" | |
typedef struct { | |
View root_view, posts_view; | |
Blog_storage_t storage; | |
} Blog_data_t; | |
static HttpResponse* root_page (Blog_storage_t *storage, HttpRequest *request); | |
char* module_register (Module *module, Server *server) | |
{ | |
Blog_data_t *data; | |
User_t new_user; | |
Blogpost_t new_bp; | |
data = (Blog_data_t *) malloc (sizeof (Blog_data_t)); | |
module->data = (void *) data; | |
store_init(&data->storage); | |
safestr_init_static(&new_user.name, "Someone", 8); | |
store_add_user(&data->storage, &new_user); | |
new_bp.author = &new_user; | |
safestr_init_empty(&new_bp.content); | |
safestr_set(&new_bp.content, "This is a Blogpost."); | |
safestr_init_empty(&new_bp.title); | |
safestr_set(&new_bp.title, "Title"); | |
store_add_blogpost(&data->storage, &new_bp); | |
/* | |
view_init_with_responder (& data->info_view, | |
(HttpResponse* (*) (void *, HttpRequest *)) & test_server_responder, | |
(void *) & data->counter); | |
*/ | |
view_init_with_responder (& data->root_view, | |
(HttpResponse* (*) (void *, HttpRequest *)) & root_page, | |
(void *) &data->storage); | |
/* | |
view_dispatcher_register_urlpattern (& server->view_dispatcher, "^/info$", & data->info_view, 0); | |
*/ | |
view_dispatcher_register_urlpattern (& server->view_dispatcher, "^/$", & data->root_view, 0); | |
return NULL; | |
} | |
static HttpResponse* root_page (Blog_storage_t *storage, HttpRequest *request) | |
{ | |
/* Render from a template. */ | |
Template t; | |
Dictionary d; | |
DictionaryElement *dt[8]; | |
DictionaryElement title_elem; | |
DictionaryElement content_elem; | |
SafeStr r; | |
safestr_init_empty(&r); | |
dictionary_init (& d, dt, 8); | |
dictionary_add (& d, & title_elem, "post_title", (void *) & store_list_posts(storage, 0)->title); | |
//dictionary_add (& d, & content_elem, "post_content", (void *) & (store_list_posts(storage, 0)->content)); | |
template_init_from_file (& t, "/home/marius/fcgicmvc/blog_app/index.html"); | |
template_render (& t, & r, & d); | |
template_deinit (& t); | |
dictionary_deinit (& d); | |
{ | |
HttpResponse *response; | |
response = http_response_new_from_string (safestr_to_string (& r)); | |
safestr_deinit (& r); | |
return response; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "store.h" | |
void store_init(Blog_storage_t* storage) | |
{ | |
storage->users = malloc(sizeof(XorDLList)); | |
xordllist_init(storage->users, NULL, NULL); | |
storage->blogposts = malloc(sizeof(XorDLList)); | |
xordllist_init(storage->blogposts, NULL, NULL); | |
storage->usercount = 0; | |
storage->postcount = 0; | |
// TODO: Add importing from disk. | |
} | |
void store_add_blogpost(Blog_storage_t* storage, Blogpost_t* post) | |
{ | |
post->id = ++storage->postcount; | |
xordllist_cons(storage->blogposts, post, XORDLLIST_DIRECTION_FORWARD); | |
} | |
void store_add_user(Blog_storage_t* storage, User_t* user) | |
{ | |
user->id = ++storage->usercount; | |
xordllist_cons(storage->users, post, XORDLLIST_DIRECTION_FORWARD); | |
} | |
void store_get_user(Blog_storage_t* storage, char* user) | |
{ | |
char* comp; | |
XorDLListIter *iter; | |
iter = malloc(sizeof(XorDLListIter)); | |
xordllist_iter_init(iter, storage->users, XORDLLIST_DIRECTION_FORWARD); | |
while (xordllist_iter_head(iter) != NULL) | |
{ | |
safestr_cat_string(((User_t*)xordllist_iter_head(iter))->name, comp); | |
if (!strcmp(comp,user)) { | |
return (User_t*)xordllist_iter_head(iter); | |
} | |
xordllist_iter_next(iter); | |
} | |
return NULL; | |
} | |
void store_list_posts(Blog_storage_t* storage, int limit) | |
{ | |
XorDLListIter *iter; | |
int i = 0; | |
iter = malloc(sizeof(XorDLListIter)); | |
xordllist_iter_init(iter, storage->blogposts, XORDLLIST_DIRECTION_FORWARD); | |
while (xordllist_iter_head(iter) != NULL && i < limit) | |
{ | |
return (Blogpost_t)xordllist_iter_head(iter); | |
xordllist_iter_next(iter); | |
i++; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* store.h | |
* Provides a storage interface specifically for the blog app | |
*/ | |
typedef struct { | |
int id; | |
SafeStr name; | |
} User_t; | |
typedef struct { | |
int id; | |
User_t *author; | |
SafeStr title; | |
SafeStr content; | |
} Blogpost_t; | |
typedef struct { | |
XorDLList *users; // just temporary, probably by far the slowest thing | |
int usercount; | |
XorDLList *blogposts; | |
int postcount; | |
// tags and stuff? | |
} Blog_storage_t; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment