goahead之GoAction实现

来源:互联网 发布:怎么申请淘宝直播 编辑:程序博客网 时间:2024/05/29 17:43
最近需要用到goahead服务器,因此花了一番时间对goahead服务器进行了简单的研究。网上都有对goahead服务器的移植进行讲解的博客,虽然讲的不够细节,但是对于启动服务器也已经是足够了,今天我这边博客主要是讲解一下我对goahead中goaction的理解和使用。如果有时间的话,我再出一个详细全面的goahead服务器的移植博客。


在开始我们的博客之前,首先我们来讲一下什么是goaction。为了让大家有个全面的了解,以下关于goaction的描述均为goahead官方网站英文描述。
官方原文:
GoActions


The traditional Common Gateway Interface (CGI) is a slow technique for creating dynamic web pages because CGI processing results in the creation of a new process for every request. This is slow and cumbersome. GoAhead provides a high-performance replacement called GoActions™ that is a more suitable solution for embedded systems that demand compact and efficient solutions.


GoActions are "C" language functions that are directly bound to specific URIs. They respond to client requests without creating a new process for each request. By sharing the address space with GoAhead, GoActions can directly access the full request context. GoActions are serviced by the action handler.


Defining GoActions


GoActions are defined by the websDefineAction API. For example:


static void buy(Webs *wp)
{
    websSetStatus(wp, 200);
    websWriteHeaders(wp, 0, 0);
    websWriteEndHeaders(wp);
    websWrite(wp, "Name %s", websGetVar(wp, "name", "")); 
    websWrite(wp,  "Age %s", websGetVar(wp, "age", ""));
    websDone(wp);
}
websDefineAction("buy", buy);
Calling GoActions


GoActions are bound to URIs that begin with /action/. When a client requests a URI /action/NAME, the action handler is invoked which then looks for a GoAction by NAME. The function bound to NAME is then is invoked to service the request. For example:


/action/buy?name=John&item=candy           
This will invoke the GoAction buy which will run the bound C function. The action handler will automatically decode the query string "name=John&age=30" and define GoAhead variables called "name" and "age".


The GoAction is responsible for writing the HTTP header and HTML document content back to the user's browser.
小编自翻:
Goactions
传统的通关网关接口(也就是CGI)是一个效率低的为了创造出动态交互网页页面的技术,为什么这么说呢?因为CGI为每个请求创造一个新的进程来处理并生成结果。这样的生成动态网页的方式显的效率低下而且繁琐笨重。goahead提供了一个更高效的方式来代替CGI。它被称之为“goactions”,对嵌入式系统来说,这是一个更加合适,严谨,积极有效的方法。


goactions是直接和具体的URL联系在一起的C语言函数。它不需要为每一次的请求创建一个新的进程,通过和goahead分享地址空间,goactions直接访问整个需求,goaction通过行为处理来支持。


Goaction定义
goactions就是websDefineAction API。我们需要先创建一个goaction的函数,在函数体中调用API来生成新的动态页面。最后需要在main函数中注册一个这个函数,调用APIwebsDefineAction进行注册。然后编译就可以了。关于建议方式,官方提供了三种,分别是IDE、Make和makeme命令。


今天我跟各位道友分享一下我对makeme的理解。
首先你需要下载makeme。你可以从 Embedthis MakeMe Download这个网站进行下载和安装。
如何进行安装呢?下面我针对linux系统进行讲解。如果大家想在不同的平台上进行安装,可以去下面的网址看更加详细、更加原版的安装讲解。
网址为https://embedthis.com/makeme/doc/start/installing.html。有需要的同学可以自己去看看,毕竟想成为一个合格的程序员,对英文文档的阅读能力是最基础的。
makeme是一个可获得的代码资源分布,它需要以下环境的支持。
linux - GNU C/C++需要2.6以上的版本
windows - WIN7系统装有VS2010及以上的版本
MAC OS X - 10.8及以上


代码的获取方式有两种,一种是去官方网站下载,另外一种是去GitHub等托管平台下载。请记住下载稳定的版本。请记住下载稳定的版本。请记住下载稳定的版本。重要的事情说三遍。
安装命令为:
tar xfz makeme-0.10.5-0-src.tgz
make boot
sudo make install
你可以简单地调用makeme通过me
$ me
me.es: ERROR: Can't find suitable start.me.
Run "me --gen start" to create stub start.me
这表明你准备好开始创建你的第一个start.me文件。


另外,你可以显示不同的makeme命令选项,如下所示:
$ me help
Usage: me [options] [targets|actions] ...
Options:
--benchmark                              # Measure elapsed time
--chdir dir                              # Directory to build from
--configure /path/to/source              # Configure for building
--continue                               # Continue on errors
--debug                                  # Same as --profile debug
--depth level                            # Set utest depth level
--diagnose                               # Emit diagnostic trace
--dump                                   # Dump the full project me file
--endian [big|little]                    # Define the CPU endianness
--file file.me                           # Use the specified me file
--force                                  # Override warnings
--gen [make|nmake|sh|vs|xcode|main|start]# Generate project file
--help                                   # Print help message
--import                                 # Import standard me environment
--keep                                   # Keep intermediate files
--log logSpec                            # Save errors to a log file
--nocross                                # Build natively
--overwrite                              # Overwrite existing files
--out path                               # Save output to a file
--platform os-arch-profile               # Build for specified platform
--pre                                    # Pre-process a source file to stdout
--prefix dir=path                        # Define installation path prefixes
--profile [debug|release|...]            # Use the build profile
--quiet                                  # Quiet operation. Suppress trace
--rebuild                                # Rebuild all specified targets
--reconfigure                            # Reconfigure with existing settings
--release                                # Same as --profile release
--rom                                    # Build for ROM without a file system
--set [feature=value]                    # Enable and a feature
--show                                   # Show commands executed
--static                                 # Make static libraries
--unicode                                # Set char size to wide (unicode)
--unset feature                          # Unset a feature
--version                                # Display the me version
--verbose                                # Trace operations
--watch [sleep time]                     # Watch for changes and rebuild
--why                                    # Why a target was or was not built
--with PACK[=PATH]                       # Build with package at PATH
--without PACK                           # Build without a package


如果你想了解更多关于makeme,你可以打开链接https://embedthis.com/makeme/doc/start/installing.html进行查看。


如果有什么不对的地方欢迎大家指针。


同样也希望,如果你有什么不一样的见解,非常乐意大家加我的微信一起分享知识。


下一篇我再抽时间写个具体的实际例程。