GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Taymindis/fcgi-function: A cross-platform module to writing C/C++ service for nginx.

BranchesTags
Open more actions menu

Latest commit

 

History

166 Commits
166 Commits

Folders and files

NameName
Last commit message
Last commit date
DockerExample
DockerExample
 
 
Testing/Temporary
Testing/Temporary
 
 
examples
examples
 
 
images
images
 
 
.gitmodules
.gitmodules
 
 
CMakeLists.txt
CMakeLists.txt
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
cmake_uninstall.cmake.in
cmake_uninstall.cmake.in
 
 
extra_tips.md
extra_tips.md
 
 
ffunc.c
ffunc.c
 
 
ffunc.h
ffunc.h
 
 
payload.txt
payload.txt
 
 

Repository files navigation

fcgi-function

fcgi Function is a C/CPP language based interface, which build on top of FastCGI with built in functional handler support, it also provided useful collection tools to enhance the facilities. It can be integrated with Nginx Fastcgi module

How it works

Image of architecture

Prerequisition Installed

Nginx -- https://www.nginx.com/

fcgi library -- https://github.com/FastCGI-Archives/FastCGI.com

cmake and make

Supported OS: LINUX, MAC OSX, and Windows

Clone the Repository Recursively

git clone --recursive https://github.com/Taymindis/fcgi-function.git

How to Build

Just Include ffunc.h and ffunc.c in your project, simplest way ( applicable to Linux, Darwin and Windows platforms )

Build sample

gcc profile_service.c ffunc.c ffunc.h -lfcgi -pthread -ldl -rdynamic

g++ profile_service.cpp ffunc.c ffunc.h -lfcgi -pthread -ldl -rdynamic

g++ -std=c++11 -I/home/xxx/cpplib/spdlog-0.17.0/include -DSPDLOG_FMT_PRINTF service_with_spdlog.cpp ffunc.c ffunc.h -lfcgi -pthread -ldl -rdynamic

when you type

./simple_service

it will result:-

Service starting
sock_port=2005, backlog=16
total function = 3
64 threads in process
Socket on hook 2005

Running on background by setting ffunc_conf->daemon = 1 , see example for mode details

int ffunc_main(int argc, char *argv[], ffunc_config_t *ffunc_conf) {
  ffunc_conf->sock_port = 2005;
  ffunc_conf->backlog = 160;
  ffunc_conf->max_thread = 64;
  ffunc_conf->daemon = 1; // run as daemon
  ffunc_parse_function(ffunc_conf, "getProfile", "postError", "postProfile");
  return 0;
}

Running on valgrind (performance will impact)

valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes ./simple_service

Edit the nginx.conf in your nginx config folder by append in your server block:-

location /getProfile {
  add_header Allow "GET, POST, HEAD" always;
  if ( $request_method !~ ^(GET|HEAD)$ ) {
    return 405;
  }
  include /etc/nginx/fastcgi_params;
  fastcgi_param FN_HANDLER getProfile;
  fastcgi_pass 127.0.0.1:2005;
}

location /postProfile {
  add_header Allow "GET, POST, HEAD" always;
  if ( $request_method !~ ^(POST)$ ) {
    return 405;
  }
   include /etc/nginx/fastcgi_params;
  fastcgi_param FN_HANDLER postProfile;
  fastcgi_pass 127.0.0.1:2005;
}

Edit the httpd.conf in your httpd config folder by append in your block

<Location "/getProfile">
    # Timeout values are in seconds
    ProxyPass "fcgi://127.0.0.1:2005" connectiontimeout=300 timeout=300
    ProxyPassReverse "fcgi://127.0.0.1:2005"
    SetEnv  FN_HANDLER "getProfile"
</Location>

You will see the FN_HANDLER is function name mapping with the function inside simple_service code, the fastcgi port 2005 is the service you start with(please look at step 10 for more details.

start the nginx/httpd server

Using apache benchmark for get request load test

ab -c 100 -n 10000 http://127.0.0.1:80/getProfile

For post request load test

ab -p "payload.txt" -T application/json -c 100 -n 10000 http://127.0.0.1:80/postProfile

the payload.txt is inside the root directory

shutdown the background instance

kill the process by using kill -2 <pid>, please do not use -9 as it will direct kill the process, unless -2 is not working

for valgrind log, you will get the summary report after kill -2 <pid>

Example of how to build a docker image

cd DockerExample
docker build -t nginx_ffunc -f Dockerfile_ngx_ffunc .
docker run -d -v ~/fcgi-function/DockerExample:/tmp -p 80:80 --name testffunc nginx_ffunc
curl "http://127.0.0.1/getProfile"

Logging Recommendation

Due to built in logging mechanism will slow down the process speed, suggested to use third party logging mechanism for your application layer such as:-

C++ spdlog

C++ g3log

C mini-async-log

Please contact [email protected] for More End to End tier project examples.

About

A cross-platform module to writing C/C++ service for nginx.

www.nginx.com/resources/wiki/modules/fcgi_function/

Topics

ccjsonfastcgiindexingjsonlock-freenginxwebserver

Resources

Readme
MIT license
Activity

Stars

37 stars

Watchers

8 watching

Forks

9 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.