goahead(嵌入式) webservice (3.3.0)运行goforms

本文介绍了如何使用Goahead Webservice 3.3.0版本创建并运行Goforms。首先,创建以.html结尾的HTML页面,然后在auth.c文件中注册和定义处理函数。通过在auth.c中声明actionTest函数,实现动态响应用户输入。编译源代码后,需要更新libgoahead.so库以确保改动生效。Goforms的路由如'http://ip:port/action/test',并可以通过'http://ip:port/test.html'访问测试页面。服务启动命令为'./goahead -v --home /etc/test /var/doc ip:port',所有页面位于'doc'目录下。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ASP过程用于页面动态显示内容,而goforms过程用于响应用户输入、更新设置或执行待定动作。

goahead3.3.0编译版https://download.csdn.net/download/qq_40155090/86721990

1.写*.html为结尾的html标签页面:

(参考:配置不改前提下,源代码编译后,页面代码放在doc文件夹下)

  例1:  test.html  页面  

  xmlhttp.open("GET","action/test",true);---- 应用actions

<!DOCTYPE html>
<html>
<head>
    <title>test.html</title>
    <meta charset="UTF-8">
</head>
<body>
    <p>Please log in</p>
    <form action=/action/test method="post">
        <table>
        <tr><td>账号:</td><td><input type="text" name="name"></td></tr>
        <tr><td>密码:</td><td><input type="password" name="address"></td></tr>
        <tr><td><input type="submit" name="submit" value="submit"></td>
            <td><input type="reset" value="reset"></td></tr>
        </table>
    </form>
    
    <script type="text/javascript">
    function loadXMLDoc()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
          {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                }
          }
        xmlhttp.open("GET","action/test",true);
        xmlhttp.send();
    }
    </script>
    
    <div id="myDiv"><h2>需要刷新的局部内容</h2></div>
    <button type="button" onclick="loadXMLDoc()">通过 AJAX 实现局部刷新</button>
    
</body>
</html>

2.在auth.c文件websOpenAuth函数中注册函数 

PUBLIC int websOpenAuth(int minimal)
{
    char    sbuf[64];
    
    assert(minimal == 0 || minimal == 1);

    if ((users = hashCreate(-1)) < 0) {
        return -1;
    }
    if ((roles = hashCreate(-1)) < 0) {
        return -1;
    }
    if (!minimal) {
        fmt(sbuf, sizeof(sbuf), "%x:%x", rand(), time(0));
        secret = websMD5(sbuf);
#if BIT_GOAHEAD_JAVASCRIPT && FUTURE
        websJsDefine("can", jsCan);
#endif
        websDefineAction("login", loginServiceProc);
        websDefineAction("logout", logoutServiceProc);
        
        //注册函数
        websDefineAction("test", actionTest);
        ///

    }
    if (smatch(BIT_GOAHEAD_AUTH_STORE, "file")) {
        verifyPassword = websVerifyPasswordFromFile;
#if BIT_HAS_PAM
    } else if (smatch(BIT_GOAHEAD_AUTH_STORE, "pam")) {
        verifyPassword = websVerifyPasswordFromPam;
#endif
    }
    return 0;
}

3.在auth.c文件中定义actionTest函数 

static void actionTest(Webs *wp)
{
    cchar    *name, *address;

    name = websGetVar(wp, "name", NULL);
    address = websGetVar(wp, "address", NULL);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp);
    websWrite(wp, "<html><body><h2>name: %s, address: %s</h2></body></html>", name, address);
    websFlush(wp);
    websDone(wp);
}

//可在auth.c文件末尾定义函数

/*
    @copy   default
    ......
*/

4.在auth.c 文件声明actionTest函数 

/********************************** Forwards **********************************/

//...... 
//可在Forwards下#if前声明前声明函数

//声明actionTest
static void actionTest(Webs *wp);

//
#if
//...
#endif

修改源代码后,需要 重新编译goahead。 每次修改源码内容时,都需要把libgoahead.so库更新才有效,因为goahead把其他依赖的文件做成了动态库。

说明:1."action/test" 实际上是路由:http://ip:port/action/test 

           2.访问test页面:http://ip:port/test.html

           3.启动服务:./goahead -v --home /etc/test /var/doc  ip:port

           4.页面都放在doc目录下

sudo ./goahead -v --home PATH1 PATH2
 
-v     : 打开运行日志
--home : 指定运行目录, 第一个参数 PATH1 是配置文件的所在目录 第二个参数 PATH2 是web页面所在目录 

doc  //帮助文档

src  //源码

test //测试示例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值