
func main(args)
{
    mtx.Lock();
    var db = openDB();
    Sqlite.Exec(db,"CREATE TABLE IF NOT EXISTS messages(id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT, timestamp INTEGER);");
    Sqlite.Close(db);
    mtx.Unlock();

    Net.Http.ListenSimpleWithLoop((ctx)=>{
        if(ctx.Path == "/")
        {
            if(ctx.Method == "POST")
            {
                var message = ctx.QueryParams.TryGetFirst("message");
                if(TypeIsString(message))
                {
                    mtx.Lock();
                    var db = openDB();
                    Sqlite.Exec(db,$"INSERT INTO messages (message,timestamp) VALUES ({Sqlite.Escape(message)},{Sqlite.Escape(DateTime.NowEpoch)});");
                    Sqlite.Close(db);
                    mtx.Unlock();
                }
            }
            ctx.WithMimeType("text/html").SendText(Pages.Index());
            return true;
        }
        else if(ctx.Path == "/pure-min.css")
        {
            ctx.WithMimeType(Net.Http.MimeType("pure-min.css")).SendBytes(embed("pure-min.css"));
            return true;
        }
        else if(ctx.Path == "/posts.json")
        {
            mtx.Lock();
            var db = openDB();
            var messages = Sqlite.Exec(db,"SELECT * FROM messages");
            Sqlite.Close(db);
            mtx.Unlock();

            ctx.WithMimeType("application/json").SendJson(messages);
            return true;
        }
        return false;
    },4210);
}