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
// Task: user needs to set up a simple HTTP server | |
// Option 1 | |
static void my_handler(struct ns_connection *nc, int ev, void *ev_data) { | |
switch (ev) { | |
case NS_HTTP_REQUEST: | |
mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n hi there!! "); | |
nc->flags |= NSF_FINISHED_SENDING_DATA; // Schedule connection close | |
break; | |
default: // We can catch low-level events like NS_RECV, etc |
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
struct task { | |
socket_t accepted_socket; | |
}; | |
struct queue { | |
struct task tasks[NUM_WORKER_THREADS]; | |
int num_tasks; | |
pthread_mutex_t mutex; | |
pthread_cond_t condvar; | |
}; |
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/env perl | |
# Copyright (c) 2015 Sergey Lyubka | |
# All rights reserved | |
use Encode; | |
my $dir = "/Users/$ENV{USER}/.Trash"; | |
sub read_file($) { local $/; open FD, $_[0] or die $_[0]; binmode FD; <FD>; } |
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8" /> | |
<title>GitHub Stars</title> | |
</head> | |
<body> | |
hi | |
</body> | |
</html> |
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 <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/socket.h> | |
#include <netinet/in.h> | |
#define LISTENING_PORT 8000 | |
static void serve_new_connection(FILE *fp) { | |
char method[10], uri[100], protocol[20]; |
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
// $ cc -W -Wall ~/tmp/mess.c && ./a.out | |
// {"a": 9, "c": "i am bar", "d": [{"a": 3, "b": null}, {"a": 7, "b": "oops"}]} | |
#include <stdio.h> | |
#include <stddef.h> | |
#include <string.h> | |
#include "mess.h" | |
#define STRUCT_NAME foo | |
#define STRUCT_FIELDS ATTR(int, a) ATTR(char *, b) |
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
{ | |
"name": "web_server", | |
"description": "web server skeleton", | |
"version": "1.0.0", | |
"private": false, | |
"dependencies": { | |
"express": "3.x", | |
"sqlite3": "*", | |
"minimist": "*", | |
"supervisor": "*", |
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
var reconnect = function() { | |
var url = 'ws://' + location.host + '/ws'; | |
ws = new WebSocket(url); | |
ws.onopen = function(ev) { console.log(ev); }; | |
ws.onclose = function(ev) { console.log(ev); setTimeout(reconnect, 1000); }; | |
ws.onmessage = function(ev) { console.log('message', ev); }; | |
ws.onerror = function(ev) { console.log('error', ev); ws.close(); }; | |
}; |