Last active
October 1, 2018 15:38
-
-
Save inparallel/5683899 to your computer and use it in GitHub Desktop.
Simple HTTP server in GO, Libevent and Node.js
This file contains hidden or 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 <sys/types.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <unistd.h> | |
#include <event.h> | |
#include <evhttp.h> | |
void generic_request_handler(struct evhttp_request *req, void *arg) | |
{ | |
struct evbuffer *returnbuffer = evbuffer_new(); | |
evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "text/plain"); | |
evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "keep-alive"); | |
evbuffer_add_printf(returnbuffer, "Thanks for the request!"); | |
evhttp_send_reply_start(req, HTTP_OK, "OK"); | |
// Chuncked reply | |
evhttp_send_reply_chunk(req, returnbuffer); | |
evhttp_send_reply_end(req); | |
evbuffer_free(returnbuffer); | |
return; | |
} | |
int main(int argc, char **argv) | |
{ | |
short http_port = 8081; | |
char *http_addr = "127.0.0.1"; | |
struct evhttp *http_server = NULL; | |
event_init(); | |
http_server = evhttp_start(http_addr, http_port); | |
evhttp_set_gencb(http_server, generic_request_handler, NULL); // Callback on event | |
fprintf(stderr, "Server started on port %d\n", http_port); | |
event_dispatch(); | |
return(0); | |
} |
This file contains hidden or 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
package main | |
import ( | |
"fmt" | |
"net/http" | |
) | |
type Hello struct{} | |
func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) { | |
w.Header().Set("Content-Type", "text/plain"); | |
w.Header().Set("Connection", "keep-alive"); | |
fmt.Fprint(w, "Thanks for the request!") | |
w.(http.Flusher).Flush() | |
} | |
func main() { | |
var h Hello | |
http.ListenAndServe("localhost:8081", h) | |
} |
This file contains hidden or 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 http = require('http'); | |
http.createServer(function (req, res) | |
{ | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Thanks for the request!'); | |
}).listen(8081, '127.0.0.1'); | |
console.log('Server started on port 8081'); |
@olekukonko I don't quite remember, but they were the latest by the time (apologies, but I didn't get to read your question except now)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What version of
go
andnodejs
are you using ?