#9 09.03.10 14:32
Re: MySQL ubuntu 9.10
а давайте не будем ставить раздутый apache2, лучше напишем свой простой веб-сервер под конкретные нужды!
файл main.vala
Код: vala:
using GLib;
public class Httpd.main : Object {
public void run() {
var server = new Soup.Server(Soup.SERVER_PORT, 8080);
var api = new Httpd.api();
server.add_handler("/", api.default_handler);
server.run();
}
public static int main(string[] args) {
var httpd = new Httpd.main();
httpd.run();
return 0;
}
}
файл api.vala
Код: vala:
using GLib;
public class Httpd.api : Object {
public void default_handler(Soup.Server server,
Soup.Message msg, string path, GLib.HashTable? query,
Soup.ClientContext client) {
string file_path;
string response = "";
if (path == "/") file_path = "index.xhtml";
else file_path = path.substring(1);
var file = File.new_for_path(file_path);
if (!file.query_exists(null)) {
response = "File '%s' doesn't exist.\n".printf(file.get_path());
} else {
try {
var in_stream = new DataInputStream(file.read (null));
string line;
while ((line = in_stream.read_line(null, null)) != null) {
response += line;
}
} catch (Error e) {
response = e.message;
}
}
msg.set_response("text/html", Soup.MemoryUse.COPY,
response, response.size ());
}
}
makefile
Код: makefile:
CC = valac APP = httpd SRC = main.vala api.vala LIB = --pkg libsoup-2.4 --thread all: $(APP) $(APP): $(SRC) @$(CC) $(LIB) $^ -o $@ clean: @$(RM) $(APP)
make - собирает сервер
./httpd - запускает
Исправлено LLlypka (09.03.10 14:38)
Offline

