You can migrate web projects in C++ by using the custom runtime of Function Compute and HTTP triggers to the cloud with a few clicks. You can use a browser or an HTTP client tool such as cURL to access a function. You can modify the C++ sample code to perform secondary development.

Prerequisites

The following operations are complete:

Procedure

  1. Run the following command in the target directory to initialize the project:
    s init start-fc-custom-samples -d start-fc-custom-samples
  2. Run the following command to go to the project directory:
    cd start-fc-custom-samples/cpp/http-demo
  3. Optional:Modify the HttpHandler::OnInvoke and HttpHandler::OnInitialize parameters in the sample file /sample/src/handlers/http_handler.cpp to perform secondary development.
  4. Run the following command to install dependencies:
    make build
    Sample output:
    docker build -t fc-cpp-runtime  -f build-image/Dockerfile build-image
    Sending build context to Docker daemon  2.048kB
    Step 1/3 : FROM aliyunfc/runtime-custom:base
     ---> e1d8dfe9d0d4
    Step 2/3 : RUN apt-get update
     ---> Using cache
     ---> 41399c348fcd
    Step 3/3 : RUN apt-get install -y cmake
     ---> Using cache
     ---> ed8388f61d09
    Successfully built ed8388f61d09
    Successfully tagged fc-cpp-runtime:latest
    docker run --rm -it -v $(pwd):/tmp fc-cpp-runtime bash -c "cd /tmp && ./build.sh"
    -- The C compiler identification is GNU 6.3.0
    -- The CXX compiler identification is GNU 6.3.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /tmp/cpp_runtime/release
    Scanning dependencies of target cppruntime
    [ 33%] Building CXX object CMakeFiles/cppruntime.dir/src/handler.cpp.o
    [ 66%] Building CXX object CMakeFiles/cppruntime.dir/src/logging.cpp.o
    [100%] Linking CXX shared library /tmp/bin/libcppruntime.so
    [100%] Built target cppruntime
    -- The C compiler identification is GNU 6.3.0
    -- The CXX compiler identification is GNU 6.3.0
    -- Check for working C compiler: /usr/bin/cc
    -- Check for working C compiler: /usr/bin/cc -- works
    -- Detecting C compiler ABI info
    -- Detecting C compiler ABI info - done
    -- Detecting C compile features
    -- Detecting C compile features - done
    -- Check for working CXX compiler: /usr/bin/c++
    -- Check for working CXX compiler: /usr/bin/c++ -- works
    -- Detecting CXX compiler ABI info
    -- Detecting CXX compiler ABI info - done
    -- Detecting CXX compile features
    -- Detecting CXX compile features - done
    -- Configuring done
    -- Generating done
    -- Build files have been written to: /tmp/sample/release
    Scanning dependencies of target bootstrap
    [ 33%] Building CXX object CMakeFiles/bootstrap.dir/src/register_handler.cpp.o
    [ 66%] Building CXX object CMakeFiles/bootstrap.dir/src/handlers/http_handler.cpp.o
    [100%] Linking CXX executable /tmp/bin/bootstrap
    [100%] Built target bootstrap
  5. Run the following command to deploy the project:
    s deploy -y
    Sample output:
    [2021-09-09T10:56:34.419] [INFO ] [S-CLI] - Start ...
    [2021-09-09T10:56:34.950] [INFO ] [FC-DEPLOY] - Using region: cn-hangzhou
    [2021-09-09T10:56:34.950] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-09-09T10:56:34.951] [INFO ] [FC-DEPLOY] - Using accessKeyID: 188077086902****
    [2021-09-09T10:56:34.951] [INFO ] [FC-DEPLOY] - Using accessKeySecret: LTAI4G4cwJkK4Rza6xd9****
      Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
    [2021-09-09T10:56:35.289] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:56:35.570] [INFO ] [FC-DEPLOY] - Checking Function cppHttpFunc exists
    [2021-09-09T10:56:35.956] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T10:56:36.035] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
      Make service custom-demo success.
      Make function custom-demo/cppHttpFunc success.
      Make trigger custom-demo/cppHttpFunc/http_t success.
    [2021-09-09T10:56:40.239] [INFO ] [FC-DEPLOY] - Checking Service custom-demo exists
    [2021-09-09T10:56:40.286] [INFO ] [FC-DEPLOY] - Checking Function cppHttpFunc exists
    [2021-09-09T10:56:40.365] [INFO ] [FC-DEPLOY] - Checking Trigger http_t exists
    [2021-09-09T10:56:40.488] [INFO ] [FC-DEPLOY] - Using customDomain: auto: fc will try to generate related custom domain resources automatically
      End of request
      Deployed.
      End of request
    [2021-09-09T10:56:44.884] [INFO ] [FC-DEPLOY] - Generated auto custom domain: cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T10:56:44.885] [INFO ] [FC-DEPLOY] - Creating custom domain: cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    [2021-09-09T10:56:45.033] [INFO ] [FC-DOMAIN] - Creating custom domain: cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
    
    Tips for next step
    ======================
    * Display information of the deployed resource: s info
    * Display metrics: s metrics
    * Display logs: s logs
    * Invoke remote function: s invoke
    * Remove Service: s remove service
    * Remove Function: s remove function
    * Remove Trigger: s remove trigger
    * Remove CustomDomain: s remove domain
    
    
    
    fc-cpp_http_demo-fc_cpp_http:
      region: cn-hangzhou
      service:
        name: custom-demo
      function:
        name: cppHttpFunc
        runtime: custom
        handler: http.handler
        memorySize: 512
        timeout: 3
      url:
        system_url: >-
          https://188077086902****.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/custom-demo/cppHttpFunc/
        custom_domain:
          - domain: >-
              http://cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
      triggers:
        - type: http
          name: http_t

Result

After the project is deployed, you can view the temporary domain name generated by Function Compute in the execution output. The temporary domain name can be used to access the newly deployed application. For example, run the following command to access the application:

curl http://cpphttpfunc.custom-demo.188077086902****.cn-hangzhou.fc.devsapp.net
Note The temporary domain name is used only for demonstration and development purposes. It is valid for a limited period of time. You must use a domain name that has obtained an Internet Content Provider (ICP) filing from Alibaba Cloud for production. For more information, see Bind a custom domain name.