Giter Club home page Giter Club logo

udho's Introduction

Udho (উধো)

udho is a tiny http library based on Boost.Beast.

Boost.Beast is based on Boost.Asio which provides low level asynchronous I/O. udho was originally created to add HTTP functionality to an existing application that is using Boost.Asio.

License pipeline status develop pipeline status master Codacy Badge Total alerts Language grade: C/C++

std::string world(udho::contexts::stateless ctx){
    return "{'planet': 'Earth'}";
}
std::string planet(udho::contexts::stateless ctx, std::string name){
    return "Hello "+name;
}
int main(){
    boost::asio::io_service io;
    udho::servers::ostreamed::stateless server(io, std::cout);
    server[udho::configs::server::document_root] = "/path/to/static/files";
    auto urls = udho::router() | "/world"          >> udho::get(&world).json() 
                               | "/planet/(\\w+)"  >> udho::get(&planet).plain();

    server.serve(urls, 9198);

    io.run();
    return 0;
}

Dependencies:

boost depend on boost-beast library. As boost-beast is only available on boost >= 1.66, udho requires a boost version at least 1.66. udho may optionally use ICU library for unicode regex functionality. In that case ICU library may be required.

  • boost > 1.66
  • pugixml
  • icu [optional]

Features:

  • regular expression based url routing to callables (functions / function objects)
  • compile time binding of routing rule with callables
  • compile time travarsable url router
  • response mime type specification in routing rule
  • any default constructible can be used as callable argument type that can be parsed from std::string
  • any ostreamable can be used as return type of callables
  • automatic type coersion for url based method calling
  • throwable http error messages
  • serving static content from disk document root if no rule matched
  • urlencoded/multipart form parsing
  • on memory session for stateful web applications (no globals) and no session for stateless applications
  • strictly typed on memory session storage
  • compile time pluggable logging
  • customizable logging
  • typesafe configuration

Example

std::string login(udho::contexts::stateful<user> ctx){ /// < strictly typed stateful context
    const static username = "derp";
    const static password = "derp123";

    if(ctx.session().exists<user>()){
        user data;
        ctx.session() >> data;  /// < extract session data
        return "already logged in";
    }else{
        if(ctx.form().has("user") && ctx.form().has("pass")){
            std::string usr = ctx.form().field<std::string>("user"); /// < form field value from post request
            std::string psw = ctx.form().field<std::string>("pass"); /// < form field value from post request
            if(usr == username && psw == password){
                ctx.session() << user(usr); /// < put data in session
                return "successful";
            }
        }
    }
    return "failed";
}

std::string echo(udho::contexts::stateful<user> ctx, int num){
    if(ctx.session().exists<user>()){
        user data;
        ctx.session() >> data;
        return boost::format("{'name': '%1%', 'num': %2%}") % data.name % num;
    }
    return "{}";
}

int main(){
    boost::asio::io_service io;
    udho::servers::ostreamed::stateful<user> server(io, std::cout);
    server[udho::configs::server::document_root] = "/path/to/static/files";
    auto router = udho::router()
        | (udho::post(&login).plain() = "^/login$")
        | (udho::get(&echo).json()    = "^/echo/(\\d+)$");

    server.serve(router, 9198);
        
    io.run();
    
    return 0;
}

udho's People

Contributors

neel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

darkomenz

udho's Issues

Linking CXX executable udho-example-hello failed

[ 50%] Built target udho
[ 75%] Linking CXX executable udho-example-hello
CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::re_detail_107000::icu_regex_traits_implementation::transform(int const*, int const*) const': hello.cpp:(.text._ZNK5boost16re_detail_10700031icu_regex_traits_implementation9transformEPKiS3_[_ZNK5boost16re_detail_10700031icu_regex_traits_implementation9transformEPKiS3_]+0x44): undefined reference to boost::re_detail_107000::icu_regex_traits_implementation::do_transform(int const*, int const*, icu_50::Collator const*) const'
CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::re_detail_107000::icu_regex_traits_implementation::transform_primary(int const*, int const*) const': hello.cpp:(.text._ZNK5boost16re_detail_10700031icu_regex_traits_implementation17transform_primaryEPKiS3_[_ZNK5boost16re_detail_10700031icu_regex_traits_implementation17transform_primaryEPKiS3_]+0x44): undefined reference to boost::re_detail_107000::icu_regex_traits_implementation::do_transform(int const*, int const*, icu_50::Collator const*) const'
CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::icu_regex_traits::translate_nocase(int) const': hello.cpp:(.text._ZNK5boost16icu_regex_traits16translate_nocaseEi[_ZNK5boost16icu_regex_traits16translate_nocaseEi]+0x15): undefined reference to u_tolower_50'
CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::basic_regex<int, boost::icu_regex_traits>::assign(int const*, int const*, unsigned int)': hello.cpp:(.text._ZN5boost11basic_regexIiNS_16icu_regex_traitsEE6assignEPKiS4_j[_ZN5boost11basic_regexIiNS_16icu_regex_traitsEE6assignEPKiS4_j]+0x2a): undefined reference to boost::basic_regex<int, boost::icu_regex_traits>::do_assign(int const*, int const*, unsigned int)'
CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::re_detail_107000::perl_matcher<boost::u8_to_u32_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, int>, std::allocator<boost::sub_match<boost::u8_to_u32_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, int> > >, boost::icu_regex_traits>::find_restart_word()': hello.cpp:(.text._ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17find_restart_wordEv[_ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17find_restart_wordEv]+0xe9): undefined reference to boost::icu_regex_traits::isctype(int, unsigned long) const'
hello.cpp:(.text._ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17find_restart_wordEv[_ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17find_restart_wordEv]+0x164): undefined reference to boost::icu_regex_traits::isctype(int, unsigned long) const' CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::re_detail_107000::perl_matcher<boost::u8_to_u32_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, int>, std::allocator<boost::sub_match<boost::u8_to_u32_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, int> > >, boost::icu_regex_traits>::match_word_boundary()':
hello.cpp:(.text._ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE19match_word_boundaryEv[_ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE19match_word_boundaryEv]+0x60): undefined reference to boost::icu_regex_traits::isctype(int, unsigned long) const' hello.cpp:(.text._ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE19match_word_boundaryEv[_ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE19match_word_boundaryEv]+0x14a): undefined reference to boost::icu_regex_traits::isctype(int, unsigned long) const'
CMakeFiles/udho-example-hello.dir/hello.cpp.o: In function boost::re_detail_107000::perl_matcher<boost::u8_to_u32_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, int>, std::allocator<boost::sub_match<boost::u8_to_u32_iterator<__gnu_cxx::__normal_iterator<char const*, std::string>, int> > >, boost::icu_regex_traits>::match_within_word()': hello.cpp:(.text._ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17match_within_wordEv[_ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17match_within_wordEv]+0x6a): undefined reference to boost::icu_regex_traits::isctype(int, unsigned long) const'
CMakeFiles/udho-example-hello.dir/hello.cpp.o:hello.cpp:(.text._ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17match_within_wordEv[_ZN5boost16re_detail_10700012perl_matcherINS_18u8_to_u32_iteratorIN9__gnu_cxx17__normal_iteratorIPKcSsEEiEESaINS_9sub_matchIS8_EEENS_16icu_regex_traitsEE17match_within_wordEv]+0x106): more undefined references to `boost::icu_regex_traits::isctype(int, unsigned long) const' follow
collect2: error: ld returned 1 exit status
make[3]: *** [examples/CMakeFiles/udho-example-hello.dir/build.make:113: examples/udho-example-hello] Error 1
make[2]: *** [CMakeFiles/Makefile2:378: examples/CMakeFiles/udho-example-hello.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:385: examples/CMakeFiles/udho-example-hello.dir/rule] Error 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.