Last active
August 12, 2017 16:05
-
-
Save timdorohin/47b7396d963bb8078e707b059e94f085 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/luajit | |
--- Configuration | |
local socket = "/tmp/mysocket-0" | |
local slimdown = require 'slimdown' | |
local header = [[ | |
Content-type: text/html | |
<!DOCTYPE html> | |
<html lang="ru"> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> | |
<style> | |
* { | |
box-sizing: border-box; | |
} | |
.a { | |
margin: 0; | |
padding: 0; | |
} | |
.a li { | |
float: left; | |
} | |
.a li a { | |
display: block; | |
color: #fff; | |
text-align: center; | |
padding: 16px; | |
text-decoration: none; | |
} | |
.a li a:hover { | |
background-color: #333; | |
} | |
.b { | |
padding: 8px; | |
margin: 8px; | |
display: block; | |
} | |
.c { | |
border: 8px solid #999; | |
} | |
.d { | |
list-style-type: none; | |
overflow: hidden; | |
background-color: #666; | |
} | |
.e { | |
margin-left: auto; | |
margin-right: auto; | |
background-color: #ccc; | |
max-width: 40em; | |
} | |
#name { | |
float: right; | |
} | |
.header { | |
background-color: #999; | |
font-weight: bold; | |
} | |
h7 { | |
float: right; | |
} | |
</style> | |
</head> | |
<body class="a e"> | |
<div class="header b"> | |
<ul class="a d"> | |
<li> | |
<a href="/">/</a> | |
</li> | |
<li> | |
<a href="/server">/server</a> | |
</li> | |
<li> | |
<a href="/fuck_you">/info</a> | |
</li> | |
<li id="name"> | |
<a href="/">WTF... is that?!</a> | |
</li> | |
</ul> | |
</div>]] | |
local div_open = [[<div class="b c">]] | |
local div_close = [[</div>]] | |
local footer = [[</body></html>]] | |
local ffi = require("ffi") | |
print("ffi loaded") | |
ffi.cdef[[ | |
int FCGX_Init(void); | |
int FCGX_OpenSocket(const char *path, int backlog); | |
typedef struct FCGX_Stream { | |
unsigned char *rdNext; | |
unsigned char *wrNext; | |
unsigned char *stop; | |
unsigned char *stopUnget; | |
int isReader; | |
int isClosed; | |
int wasFCloseCalled; | |
int FCGI_errno; | |
void (*fillBuffProc) (struct FCGX_Stream *stream); | |
void (*emptyBuffProc) (struct FCGX_Stream *stream, int doClose); | |
void *data; | |
} FCGX_Stream; | |
typedef struct FCGX_Request { | |
int requestId; | |
int role; | |
FCGX_Stream *in; | |
FCGX_Stream *out; | |
FCGX_Stream *err; | |
char **envp; | |
struct Params *paramsPtr; | |
int ipcFd; | |
int isBeginProcessed; | |
int keepConnection; | |
int appStatus; | |
int nWriters; | |
int flags; | |
int listen_sock; | |
int detached; | |
} FCGX_Request; | |
int FCGX_InitRequest(FCGX_Request *request, int sock, int flags); | |
int FCGX_Accept_r(FCGX_Request *request); | |
char *FCGX_GetParam(const char *name, char **envp); | |
int FCGX_PutS(const char *str, FCGX_Stream *stream); | |
void FCGX_Finish_r(FCGX_Request *request); | |
]] | |
print("cdef loaded") | |
local fcgi = ffi.load("fcgi") | |
print("libfcgi loaded") | |
local init = fcgi.FCGX_Init() | |
print("libfcgi initialised") | |
local socketId = fcgi.FCGX_OpenSocket(socket, 20) | |
print("socket initialised:") | |
print(socketId) | |
local request = ffi.new("FCGX_Request") | |
local reqinit = fcgi.FCGX_InitRequest(request, socketId, 0) | |
print("request initialised:") | |
print(reqinit) | |
while true do | |
local rc = fcgi.FCGX_Accept_r(request) | |
print("answering") | |
if rc ~= 0 then break end | |
local uri = ffi.string(fcgi.FCGX_GetParam("REQUEST_URI", request.envp)) | |
local file = "/srv/http/pages/" .. uri:sub(2, 10) .. ".md" | |
local f_desc = io.open(file, "r") | |
if f_desc == nil then f_desc = io.open("/srv/http/pages/default.md", "r") end | |
f = f_desc:read("*all") | |
local post = slimdown.render(f) | |
local answer = header .. div_open .. post .. div_close .. footer | |
fcgi.FCGX_PutS(answer,request.out) | |
fcgi.FCGX_Finish_r(request) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment