HTTP service-side JSON server

Source: Internet
Author: User

HTTP service-side JSON server

Last updated on: 2014-5-18

Author: Kagula

Read the Prerequisites: Basic use of CMake tools

Content Introduction:

Cppcms is an open source web development framework that makes it easy to implement HTTP services and JSON services, where the CPPCMS development environment is built. Write a CPPCMS test program that establishes the HTTP service and returns the Hello,world page to the browser. Some third-party libraries that are dependent on cppcms have been introduced in other places, so there is no repetition here.

Environment: Windows8.1 64bit, Visual Studio Professional SP1

zlib-1.2.8, openssl-1.0.1g, pcre-8.35, Icu4c-53_1, cppcms-1.0.4, python-3.4.0, CMake2.8.12.2,

Boost 1.55

Build CPPCMS development environment

CPPCMS relies on third-party libraries or tools such as zlib, OpenSSL, Pcre, icu4c, Python, and Win SDK

Download and install Python-3.4.0.amd64.msi on the Python website, and don't forget to check the wizard to add the Python.exe location to the system's environment variables.

The compiled libraries in My computer, their locations

Zlib

Header file location: d:/sdk/zlib-1.2.8;d:\sdk\zlib-1.2.8\build;

Library file Location: d:/sdk/zlib-1.2.8/build/release

Openssl

Header file Location: D:\SDK\openssl-1.0.1g\include

Library file Location: D:\SDK\openssl-1.0.1g\out32dll

icu4c

Download Http://download.icu-project.org/files/icu4c/53.1/icu4c-53_1-src.zip file

Open the D:\SDK\icu4c-53_1-src\icu\source\allinone\allinone.sln file and build the release release.

Header file Location: D:\SDK\icu4c-53_1-src\icu\include

Library file location:;D: \sdk\icu4c-53_1-src\icu\lib

Pcre

Download Ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.35.zip

Use the CMake tool to generate PCRE.sln files under D:\SDK\pcre-8.35\build\, open and compile with VisualStudio 2013.

Header file location: d:/sdk/pcre-8.35;d:\sdk\pcre-8.35\build;

Library file Location: D:\SDK\pcre-8.35\build\Release

When you're ready, you can build the Cppcms library.

Download the cppcms-1.0.4.tar.bz2 file from Cppcms official website and extract it to "D:\SDK\cppcms-1.0.4", after opening the CMake GUI tool configure, there are many variables

Add variable

Cmake_include_path

Type is filepath

Value is

D:/sdk/pcre-8.35;d:\sdk\pcre-8.35\build;d:/sdk/zlib-1.2.8;d:\sdk\zlib-1.2.8\build;d:\sdk\icu4c-53_1-src\icu\ Include;d:\sdk\openssl-1.0.1g\include

Add variable

Cmake_library_path

Type is filepath

Value is D:\sdk\pcre-8.35\build\release;d:/sdk/zlib-1.2.8/build/release;d:\sdk\icu4c-53_1-src\icu\lib;d:\sdk\ Openssl-1.0.1g\out32dll; C:\ProgramFiles (x86) \microsoft Sdks\windows\v7.1a\lib

Re-configure after generate. Open the newly generated sln file. Buildsolution has four options debug, Minsizerel (minimum release), release, Relwithdebinfo (release with debug information), we choose Release mode build here.

Encountered during the compilation process

[1]zconf.h file cannot find the problem, this file is in my D:\SDK\zlib-1.2.8\build path, give it address, this problem is resolved.

[2]json.cpp file return Is_ code line However, change to Returnbool (Is_), because Visual Studio (C++11 standard) requires IStream explicit (explicitly) conversion of type bool.

[3]test_formatting.cpp and Test_boundary.cpp source files because of the occurrence of special characters (in fact, the file language encoding problem) caused the failure to compile, skipped, because these two test items can not be compiled, does not affect our future use of the CPPCMS library.

[4] Modify the D:\SDK\cppcms-1.0.4\booster\booster\nowide\fstream.h file on line 44th,

if (my_base_type::open (CONVERT (s). C_STR (), mode)) {

Revision changed to

if (Std::basic_filebuf<chartype,traits>::open (s, mode)) {

Otherwise, the debug version of the call to Cppcms will be hung out.

[5] Because the release version of Cppcms application class of the Main method std::string an argument is released error, so I changed it to a const char * type, the following three steps is the concrete step

[5-1] "D:\SDK\cppcms-1.0.4\cppcms\application.h" File line No. 298

virtual void main (std::string URL);

Revision changed to

virtual void main (const char* URL);

[5-2] "D:\SDK\cppcms-1.0.4\src\application.cpp" File line No. 200

void Application::main (std::string URL)

Revision changed to

void Application::main (const char* URL)

[5-3] "D:\SDK\cppcms-1.0.4\src\url_dispatcher.cpp" File line 49th

[6] Modifying json_rpc_server related files to make the JSON service available

[6-1]

"D:\SDK\cppcms-1.0.4\cppcms\rpc_json.h" 152 lines

virtual void main (std::string);

Switch

virtual void Main (const char*);

[6-2] "D:\SDK\cppcms-1.0.4\src\rpc_json.cpp" 148 lines

void Json_rpc_server::main (std::string/*unused*/)

Switch

void Json_rpc_server::main (const char*/*unused*/)

App_->main (match_[select_]);

Revision changed to

std::string tmp = match_[select_];

App_->main (Tmp.c_str ());

Now rebuild the entire solution, wait a few minutes, Visual Studio 2013 hints 77 successes, 2 failures (just say the file language encoding problem), 3 skipped. That's all you can do. The Cppcms.dll and Cppcms.lib files after build can be found in the. \cppcms-1.0.4\build\release directory.

Below we write a HelloWorld program with CPPCMS

Hello World

When we built our first example, we used the boost library to turn GBK strings to UTF strings, and my computer's boost library installation path was "D:\SDK\boost_1_55_0".

When you create a new WIN32 console project in Visual Studio, the Additonaloptions option is empty project.

Set header File search path

D:\SDK\cppcms-1.0.4

D:\SDK\cppcms-1.0.4\build

D:\SDK\cppcms-1.0.4\booster

D:\SDK\cppcms-1.0.4\build\booster

D:\SDK\boost_1_55_0

Set library file search Path

D:\SDK\cppcms-1.0.4\build\booster\Release

D:\SDK\cppcms-1.0.4\build\Release

D:\SDK\boost_1_55_0\stage\lib

Add a link library cppcms.lib

Copy the following 7 dynamic link libraries into your project folder, otherwise you will be prompted not to find these DLL files

D:\SDK\pcre-8.35\build\Release\pcre.dll

D:\SDK\zlib-1.2.8\build\Release\zlib.dll

D:\SDK\cppcms-1.0.4\build\Release\cppcms.dll

D:\SDK\cppcms-1.0.4\build\booster\Release\booster.dll

D:\SDK\icu4c-53_1-src\icu\bin\icuuc53.dll

D:\SDK\icu4c-53_1-src\icu\bin\icudt53.dll

D:\SDK\icu4c-53_1-src\icu\bin\icuin53.dll

To add the Source.cpp file, the source code list is as follows

#include <cppcms/application.h> #include <cppcms/applications_pool.h> #include <cppcms/service.h> #include <cppcms/http_response.h> #include <boost/locale.hpp>class hello:public cppcms::application { Public:hello (Cppcms::service &srv): Cppcms::application (SRV) {}void main (const char* URL);}; void Hello::main (const char* URL) {//GBK to utfstd::string utfstr = boost::locale::conv::to_utf<char> ("

To add the Configure.js file, the source code list is as follows

{"      service": {"          API": "http", "          port": 8080      },      "http": {          "script_names": ["/hello"]      }
   }  

Press [F5] to run in debug mode.

Now you can access your HTTP server using the http://localhost:8080/address.

JSON Server Example

I tested the following code posted in this link, can be used, but if your test program is in debug mode, you can only call debug mode compiled out of the CPPCMS dynamic library , if you are in release mode , you can only call the release mode compiled CPPCMS dynamic library , otherwise it will throw "bad allocation" error.

Service-side code

#include <cppcms/application.h> #include <cppcms/applications_pool.h> #include <cppcms/service.h> #include <cppcms/http_response.h> #include <cppcms/rpc_json.h> #include <cppcms/json.h> #include <cppcms/mount_point.h> #include <map> #include <string> #include <exception> #include < iostream>/* Title: Test Cppcms provides the JSON_RPC service feature Source: Use CPPCMS development JSON_RPC Services http://www.zeuux.com/group/candcplus/bbs/ Content/55785/*/using namespace std;using cppcms::rpc::json_rpc_server;using cppcms::rpc::json_method;class calc_ Service:public json_rpc_server {public:calc_service (cppcms::service &srv): Json_rpc_server (SRV) {bind ("sum", Json_method (&calc_service::sum, this), method_role); Bind ("Div", Json_method (&calc_service::d IV, this), Method_role);} void sum (int x, int y) {cppcms::json::value v;v["x"] = x;v["y"] = y;v["result"] = x + y;return_result (v);} void Div (int x, int y) {if (y = = 0) {return_error ("division by zero.");} else {return_result (x/y);}};Class Hello_service:public Json_rpc_server {public:hello_service (cppcms::service &srv): Json_rpc_server (SRV) { Bind ("Hello", Json_method (&hello_service::hello, this), Method_role), bind ("Hello_all", Json_method (&hello_ Service::hello_all, this), Method_role), bind ("hello_x", Json_method (&hello_service::hello_x, this), Method_role );} void Hello (string name) {string say = "Hello," + name + "."; Return_result (say);} void Hello_all (vector<string> names) {string say = "Hello,"; for (unsigned i = 0; I<names.size (); i++) {say + = Names[i] + "";} Return_result (say);} void Hello_x (Cppcms::json::value val) {Return_result (val);}}; int main (int argc, char * * argv) {try {Cppcms::service srv (argc, argv); Srv.applications_pool (). Mount (cppcms:: Applications_factory<calc_service> (), Cppcms::mount_point ("/calc")), Srv.applications_pool (). Mount (cppcms: :applications_factory

Testing the HTML source code for the server


Configure.js Code

{"    service": {        "API": "http",        "port": 8080,        },    "http": {        "script_names": ["/calc", "/hello"]    }}


Supplementary reading materials

CPPCMS compilation and installation under Linux

http://blog.csdn.net/csfreebird/article/details/6730623

Hello World Example of official website

Http://cppcms.com/wikipp/en/page/cppcms_1x_tut_hello

Deep learning Cppcms

http://remonstrate.wordpress.com/2012/04/09/%E6%B7%B1%E5%85%A5%E5%AD%A6%E4%B9%A0-cppcms/

Cppcms and Nginx work together

Http://www.cnblogs.com/believeit/archive/2011/09/03/2183531.html

Cppcms Support File Upload

http://www.oschina.net/question/565065_66895

Boost Library learning with five Boost.locale character conversion Gbkutf8 Big5 string wstring etc.

http://blog.csdn.net/leitianjun/article/details/24658655

How to Build Libiconv with Microsoft VisualStudio

Http://www.codeproject.com/Articles/302012/How-to-Build-libiconv-with-Microsoft-Visual-Studio

Note that IE6 does not support JSON_RPC technology.

HTTP service-side JSON server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.