-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathe4652924.6c59ca79.js
1 lines (1 loc) · 5.64 KB
/
e4652924.6c59ca79.js
1
(window.webpackJsonp=window.webpackJsonp||[]).push([[10],{66:function(e,t,n){"use strict";n.r(t),n.d(t,"frontMatter",(function(){return i})),n.d(t,"metadata",(function(){return o})),n.d(t,"rightToc",(function(){return c})),n.d(t,"default",(function(){return d}));var a=n(2),l=n(6),r=(n(0),n(73)),i={id:"getting-started-hello-world",title:"Hello World",sidebar_label:"Hello World"},o={unversionedId:"getting-started-hello-world",id:"getting-started-hello-world",isDocsHomePage:!1,title:"Hello World",description:"Serving requests",source:"@site/docs/getting-started-hello-world.md",permalink:"/pistache.github.io/docs/getting-started-hello-world",editUrl:"https://github.com/facebook/docusaurus/edit/master/website/docs/getting-started-hello-world.md",sidebar_label:"Hello World",sidebar:"someSidebar",previous:{title:"Installation",permalink:"/pistache.github.io/docs/"},next:{title:"Http Response",permalink:"/pistache.github.io/docs/guide-http-handler"}},c=[{value:"Include",id:"include",children:[]},{value:"Hello World",id:"hello-world",children:[]},{value:"Final touch",id:"final-touch",children:[]}],s={rightToc:c};function d(e){var t=e.components,n=Object(l.a)(e,["components"]);return Object(r.b)("wrapper",Object(a.a)({},s,n,{components:t,mdxType:"MDXLayout"}),Object(r.b)("h1",{id:"serving-requests"},"Serving requests"),Object(r.b)("h2",{id:"include"},"Include"),Object(r.b)("p",null,"First, let\u2019s start by including the right header"),Object(r.b)("p",null,Object(r.b)("inlineCode",{parentName:"p"},'#include "pistache/endpoint.h"')),Object(r.b)("h2",{id:"hello-world"},"Hello World"),Object(r.b)("p",null,"Requests received by Pistache are handled with an ",Object(r.b)("inlineCode",{parentName:"p"},"Http::Handler"),"."),Object(r.b)("p",null,"Let\u2019s start by defining a simple ",Object(r.b)("inlineCode",{parentName:"p"},"HelloHandler"),":"),Object(r.b)("pre",null,Object(r.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cpp"}),'using namespace Pistache;\n\nclass HelloHandler : public Http::Handler {\npublic:\n\n HTTP_PROTOTYPE(HelloHandler)\n\n void onRequest(const Http::Request& request, Http::ResponseWriter response) {\n response.send(Http::Code::Ok, "Hello, World");\n }\n};\n')),Object(r.b)("p",null,"Handlers must inherit the ",Object(r.b)("inlineCode",{parentName:"p"},"Http::Handler")," class and at least define the ",Object(r.b)("inlineCode",{parentName:"p"},"onRequest")," member function."),Object(r.b)("p",null,"A handler must also define a ",Object(r.b)("inlineCode",{parentName:"p"},"clone()")," member function.\nSimple handlers can use the special ",Object(r.b)("inlineCode",{parentName:"p"},"HTTP_PROTOTYPE")," macro, passing in the name of the class.\nThe macro will automatically expand to a default implementation of the ",Object(r.b)("inlineCode",{parentName:"p"},"clone()")," member function."),Object(r.b)("h2",{id:"final-touch"},"Final touch"),Object(r.b)("p",null,"After defining the handler, the server can now be started:"),Object(r.b)("pre",null,Object(r.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cpp"}),"int main() {\n Net::Address addr(Net::Ipv4::any(), Net::Port(9080));\n\n auto opts = Http::Endpoint::options().threads(1);\n Http::Endpoint server(addr);\n server.init(opts);\n server.setHandler(std::make_shared<HelloHandler>());\n server.serve();\n}\n")),Object(r.b)("p",null,"For simplicity, you can also use the ",Object(r.b)("inlineCode",{parentName:"p"},"listenAndServe")," helper function that will automatically create an endpoint and instantiate your handler:"),Object(r.b)("pre",null,Object(r.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cpp"}),'int main() {\n Http::listenAndServe<HelloHandler>("*:9080");\n}\n')),Object(r.b)("div",{className:"admonition admonition-tip alert alert--success"},Object(r.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-heading"}),Object(r.b)("h5",{parentName:"div"},Object(r.b)("span",Object(a.a)({parentName:"h5"},{className:"admonition-icon"}),Object(r.b)("svg",Object(a.a)({parentName:"span"},{xmlns:"http://www.w3.org/2000/svg",width:"12",height:"16",viewBox:"0 0 12 16"}),Object(r.b)("path",Object(a.a)({parentName:"svg"},{fillRule:"evenodd",d:"M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"})))),"Use more serving threads")),Object(r.b)("div",Object(a.a)({parentName:"div"},{className:"admonition-content"}),Object(r.b)("p",{parentName:"div"},"By default, listenAndServe will only use 1 thread. You can tweak that by passing an Options argument to the function:"),Object(r.b)("pre",{parentName:"div"},Object(r.b)("code",Object(a.a)({parentName:"pre"},{className:"language-cpp"}),'auto opts = Http::Endpoint::options().threads(std::thread::hardware_concurrency());\nHttp::listenAndServe<HelloHandler>("*:9080", opts);\n')))),Object(r.b)("p",null,"And that\u2019s it, now you can fire up your favorite curl request and observe the final result:"),Object(r.b)("pre",null,Object(r.b)("code",Object(a.a)({parentName:"pre"},{className:"language-bash"}),"curl http://localhost:9080/\nHello, World\n")),Object(r.b)("p",null,"Complete code for this example can be found on GitHub: ",Object(r.b)("a",Object(a.a)({parentName:"p"},{href:"https://github.com/oktal/pistache/blob/master/examples/hello_server.cc"}),"examples/hello_server.cc")))}d.isMDXComponent=!0}}]);