smr/src/smr.c

181 lines
3.9 KiB
C

#include <kore/kore.h>
#include <kore/http.h>
#ifdef BUILD_PROD
#include <luajit.h>
#else
#define LUA_OK 0
#endif
#include <lauxlib.h>
#include <lua.h>
#include <lualib.h>
#include "smr.h"
#include "libkore.h"
#include "libcrypto.h"
#include <dirent.h>
int home(struct http_request *);
int post_story(struct http_request *);
int edit_story(struct http_request *);
int edit_bio(struct http_request *);
int read_story(struct http_request *);
int login(struct http_request *);
int claim(struct http_request *);
int download(struct http_request *);
int style(struct http_request *);
int miligram(struct http_request *);
int do_lua(struct http_request *req, const char *name);
int errhandeler(lua_State *);
lua_State *L;
/*
static / index
static / _post post
static / _edit edit
dynamic / .* read
static / _login login
static / _claim claim
*/
int
errhandeler(lua_State *L){
printf("Error: %s\n",lua_tostring(L,1));
lua_getglobal(L,"debug");
lua_getglobal(L,"print");
lua_getfield(L,-2,"traceback");
lua_call(L,0,1);
lua_call(L,1,0);
lua_pop(L,1);
return 1;
}
int
do_lua(struct http_request *req, const char *name){
printf("About to do lua %s\n",name);
lua_pushcfunction(L,errhandeler);
printf("Pushed errhandler function\n");
lua_getglobal(L,name);//err(),name()
if(!lua_isfunction(L,-1)){
printf("Could not find a method named %s\n",name);
lua_pop(L,2);//
return (KORE_RESULT_OK);
}
printf("About to push light userdata: %p\n",(void*)req);
lua_pushlightuserdata(L,req);//err,name(),ud_req
printf("About to pcall\n");
int err = lua_pcall(L,1,0,-3);
if(err != LUA_OK){
printf("Failed to run %s: %s\n",name,lua_tostring(L,-1));
http_response(req, 500, NULL, 0);
lua_pop(L,lua_gettop(L));
return (KORE_RESULT_OK);
}
return KORE_RESULT_OK;
}
int
post_story(struct http_request *req){
printf("We want to post!\n");
return do_lua(req,"paste");
}
int
edit_story(struct http_request *req){
printf("We want to edit!\n");
return do_lua(req,"edit");
}
int
edit_bio(struct http_request *req){
printf("We want to edit bio!\n");
return do_lua(req,"edit_bio");
}
int
read_story(struct http_request *req){
printf("We want to read!\n");
return do_lua(req,"read");
}
int
login(struct http_request *req){
printf("We want to login!\n");
return do_lua(req,"login");
}
int
claim(struct http_request *req){
printf("We want to claim!\n");
return do_lua(req,"claim");
}
int
download(struct http_request *req){
printf("We want to do download!\n");
return do_lua(req,"download");
}
int
home(struct http_request *req){
return do_lua(req,"home");
}
void
kore_worker_configure(void){
printf("Configuring worker...\n");
int err;
/*DIR *dp;*/
/*struct dirent *ep;*/
/*dp = opendir("./");*/
/*if(dp != NULL){*/
/*while(ep = readdir(dp)){*/
/*printf("%s\n",ep->d_name);*/
/*}*/
/*closedir(dp);*/
/*}*/
L = luaL_newstate();
luaL_openlibs(L);
load_kore_libs(L);
load_crypto_libs(L);
lua_pushcfunction(L,errhandeler);
printf("About to run loadfile...\n");
luaL_loadfile(L,SM_INIT);
printf("Done running loadfile...\n");
if(!lua_isfunction(L,-1)){//failed to loadfile()
printf("Failed to load %s: %s\n",SM_INIT,lua_tostring(L,-1));
lua_pop(L,1);
return;
}
printf("About to pcall\n");
err = lua_pcall(L,0,LUA_MULTRET,-2);
printf("Done pcalling\n");
//err = luaL_dofile(L,"init.lua");
if(err){
printf("Failed to run %s\n",SM_INIT);
return;
}
lua_pushcfunction(L,errhandeler);
lua_getglobal(L,"configure");
err = lua_pcall(L,0,0,-2);
if(err != LUA_OK){
printf("Failed to run configure(): %s\n",lua_tostring(L,-1));
lua_pop(L,2);
return;
}
lua_pop(L,lua_gettop(L));
printf("Finished configuring worker\n");
}
void
kore_worker_teardown(void){
int err;
lua_pushcfunction(L,errhandeler);
lua_getglobal(L,"teardown");
err = lua_pcall(L,0,0,1);
if(err != LUA_OK){
printf("Failed to run configure(): %s\n",lua_tostring(L,-1));
lua_pop(L,2);
return;
}
lua_close(L);
}