MS Casablanca SDK Sample (TrivialServer)

MS Casablanca SDK Sample


// TrivialHTTP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "astreambuf.h"

using namespace http;
using namespace http::listener;

int main(int argc, char* argv[])
{
   http_listener::create("http://localhost:4711/trivial", [](http_request message)
   {
      message.reply(http::status_codes::OK, "Hello World!");
      //std::cout << "Served a request..." << std::endl;
   })

   /* Prevent Listen() from returning until user hits 'Enter' */
   .listen([]() { fgetc(stdin); }).wait();

   return 0;
}

lambda의 향연 덕분에 Casablanca SDK를 이용한 서버 코드가 딸랑 4줄.

서버 시작(종료는 아무키나 누르면 됩니다)


firefox 접속화면

http_listener::create()

HTTP listener object 생성

create() 의 첫번째 인자는  접속 URI, 두번째 인자는 HTTP GET 메소드에 대한 핸들러 (람다).


// TrivialHTTP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include "astreambuf.h"

using namespace http;
using namespace http::listener;

int main(int argc, char* argv[])
{
   http_listener::create("http://localhost:4711/trivial", [](http_request message)
   {
      message.reply(http::status_codes::OK, "Hello World!");
      //std::cout << "Served a request..." << std::endl;
   })

   /* Prevent Listen() from returning until user hits 'Enter' */
   .listen([]() { fgetc(stdin); }).wait();

   return 0;
}

Advertisements

About sydlife

Lazy programmer, Dad and Husband.
%d bloggers like this: