gofast

2025-12-07 0 857

gofast

gofast是纯粹写在Golang的FastCGI“客户端”库。

内容

  • 真的做什么?
  • 为什么?
  • 如何使用?
    • 简单示例
    • 高级示例
      • 正常的PHP应用
      • 使用中间件自定义请求会话
      • FastCGI授权者
      • FastCGI过滤器
      • 汇总客户
    • 完整的例子
  • 作者
  • 贡献
  • 执照

真的做什么?

在FastCGI规范中,FastCGI系统具有2个组件: (a)Web服务器(b)应用程序服务器。 Web服务器应通过套接字将请求信息交给应用程序服务器。应用程序服务器始终倾听插座并响应套接字请求。

gofast可帮助您在此图片的Web服务器部分上编写代码。它可以帮助您将请求传递给应用程序服务器并从中接收响应。

您可能会认为gofast是“客户库”,以消费任何FastCGI应用程序服务器。

为什么?

许多受欢迎的语言(例如Python,PHP,Nodejs)具有FastCGI应用程序服务器的实现。使用gofast ,您可以简单地使用这些语言混合使用。

另外,这很有趣:-)

如何使用?

您基本上将使用Handler作为http.handler。您可以使用默认的ServeMux或其他兼容的路由器(例如Gorilla,Pat)进一步MUX。然后,您可以在此Golang HTTP服务器中使用FastCGI。

简单示例

请注意,这只是Web服务器组件。您需要在其他地方启动应用程序组件。

gofast\”
)

func main() {
// Get fastcgi application server tcp address
// from env FASTCGI_ADDR. Then configure
// connection factory for the address.
address := os.Getenv(\”FASTCGI_ADDR\”)
connFactory := gofast .SimpleConnFactory(\”tcp\”, address)

// route all requests to a single php file
http.Handle(\”/\”, gofast .NewHandler(
gofast .NewFileEndpoint(\”/var/www/html/index.php\”)( gofast .BasicSession),
gofast .SimpleClientFactory(connFactory),
))

// serve at 8080 port
log.Fatal(http.ListenAndServe(\”:8080\”, nil))
}
\”>

 // this is a very simple fastcgi web server
package main

import (
	\"log\"
	\"net/http\"
	\"os\"

	\"github.com/yookoala/ gofast \"
)

func main () {
	// Get fastcgi application server tcp address
	// from env FASTCGI_ADDR. Then configure
	// connection factory for the address.
	address := os . Getenv ( \"FASTCGI_ADDR\" )
	connFactory := gofast . SimpleConnFactory ( \"tcp\" , address )

	// route all requests to a single php file
	http . Handle ( \"/\" , gofast . NewHandler (
		gofast . NewFileEndpoint ( \"/var/www/html/index.php\" )( gofast . BasicSession ),
		gofast . SimpleClientFactory ( connFactory ),
	))

	// serve at 8080 port
	log . Fatal ( http . ListenAndServe ( \":8080\" , nil ))
}

高级示例

正常的PHP应用

要服务普通的PHP应用程序,您需要:

  1. 从文件系统服务静态资产;和
  2. 仅使用相关的PHP文件服务路径。
代码

gofast\”
)

func main() {
// Get fastcgi application server tcp address
// from env FASTCGI_ADDR. Then configure
// connection factory for the address.
address := os.Getenv(\”FASTCGI_ADDR\”)
connFactory := gofast .SimpleConnFactory(\”tcp\”, address)

// handles static assets in the assets folder
http.Handle(\”/assets/\”,
http.StripPrefix(\”/assets/\”,
http.FileServer(http.FileSystem(http.Dir(\”/var/www/html/assets\”)))))

// route all requests to relevant PHP file
http.Handle(\”/\”, gofast .NewHandler(
gofast .NewPHPFS(\”/var/www/html\”)( gofast .BasicSession),
gofast .SimpleClientFactory(connFactory),
))

// serve at 8080 port
log.Fatal(http.ListenAndServe(\”:8080\”, nil))
}
\”>

 package main

import (
	\"fmt\"
	\"net/http\"
	\"os\"

	\"github.com/yookoala/ gofast \"
)

func main () {
	// Get fastcgi application server tcp address
	// from env FASTCGI_ADDR. Then configure
	// connection factory for the address.
	address := os . Getenv ( \"FASTCGI_ADDR\" )
	connFactory := gofast . SimpleConnFactory ( \"tcp\" , address )

	// handles static assets in the assets folder
	http . Handle ( \"/assets/\" ,
		http . StripPrefix ( \"/assets/\" ,
			http . FileServer ( http . FileSystem ( http . Dir ( \"/var/www/html/assets\" )))))

	// route all requests to relevant PHP file
	http . Handle ( \"/\" , gofast . NewHandler (
		gofast . NewPHPFS ( \"/var/www/html\" )( gofast . BasicSession ),
		gofast . SimpleClientFactory ( connFactory ),
	))

	// serve at 8080 port
	log . Fatal ( http . ListenAndServe ( \":8080\" , nil ))
}

使用中间件自定义请求会话

每个Web服务器请求将导致gofast 。并且每个gofast .Request将首先通过SessionHandler运行,然后将gofast .client的Do()方法交给DO()方法。

默认的gofast .Basicsession实施无济于事。图书馆的功能像gofast .Newphpfs, gofast .newfileendpoint是gofast .Middleware实现,它们是较低级别的中间件链。

因此,您可以通过实现gofast .Middleware自定义自己的会话。

代码

gofast\”
)

func main() {
// Get fastcgi application server tcp address
// from env FASTCGI_ADDR. Then configure
// connection factory for the address.
address := os.Getenv(\”FASTCGI_ADDR\”)
connFactory := gofast .SimpleConnFactory(\”tcp\”, address)

// a custom authentication handler
customAuth := func(inner gofast .SessionHandler) gofast .SessionHandler {
return func(client gofast .Client, req * gofast .Request) (* gofast .ResponsePipe, error) {
user, err := someCustomAuth(
req.Raw.Header.Get(\”Authorization\”))
if err != nil {
// if login not success
return nil, err
}
// set REMOTE_USER accordingly
req.Params[\”REMOTE_USER\”] = user
// run inner session handler
return inner(client, req)
}
}

// session handler
sess := gofast .Chain(
customAuth, // maps REMOTE_USER
gofast .BasicParamsMap, // maps common CGI parameters
gofast .MapHeader, // maps header fields into HTTP_* parameters
gofast .MapRemoteHost, // maps REMOTE_HOST
)( gofast .BasicSession)

// route all requests to a single php file
http.Handle(\”/\”, gofast .NewHandler(
gofast .NewFileEndpoint(\”/var/www/html/index.php\”)(sess),
gofast .SimpleClientFactory(connFactory),
))

// serve at 8080 port
log.Fatal(http.ListenAndServe(\”:8080\”, nil))
}
\”>

 package main

import (
	\"fmt\"
	\"net/http\"
	\"os\"

	\"github.com/yookoala/ gofast \"
)

func main () {
	// Get fastcgi application server tcp address
	// from env FASTCGI_ADDR. Then configure
	// connection factory for the address.
	address := os . Getenv ( \"FASTCGI_ADDR\" )
	connFactory := gofast . SimpleConnFactory ( \"tcp\" , address )

	// a custom authentication handler
	customAuth := func ( inner gofast . SessionHandler ) gofast . SessionHandler {
		return func ( client gofast . Client , req * gofast . Request ) ( * gofast . ResponsePipe , error ) {
			user , err := someCustomAuth (
				req . Raw . Header . Get ( \"Authorization\" ))
			if err != nil {
				// if login not success
				return nil , err
			}
			// set REMOTE_USER accordingly
			req . Params [ \"REMOTE_USER\" ] = user
			// run inner session handler
			return inner ( client , req )
		}
	}

	// session handler
	sess := gofast . Chain (
		customAuth ,            // maps REMOTE_USER
		gofast . BasicParamsMap , // maps common CGI parameters
		gofast . MapHeader ,      // maps header fields into HTTP_* parameters
		gofast . MapRemoteHost ,  // maps REMOTE_HOST
	)( gofast . BasicSession )

	// route all requests to a single php file
	http . Handle ( \"/\" , gofast . NewHandler (
		gofast . NewFileEndpoint ( \"/var/www/html/index.php\" )( sess ),
		gofast . SimpleClientFactory ( connFactory ),
	))

	// serve at 8080 port
	log . Fatal ( http . ListenAndServe ( \":8080\" , nil ))
}

FastCGI授权者

FASTCGI指定了授权使用“授权器应用程序”的HTTP请求的授权者角色。与通常的FASTCGI应用程序(IE响应者)不同,它仅进行授权检查。

规格摘要

在实际服务HTTP请求之前,Web服务器可以将正常的FASTCGI请求格式化为仅使用FastCGI参数( FCGI_PARAMS流)的授权器应用程序。此应用程序负责确定请求是否正确验证并授权该请求。

如果有效,

  • 授权器应用程序应使用HTTP状态200 (确定)响应。

  • 它可以通过将Variable-SOME-HEADER头标头字段添加到其对Web服务器的响应中,从而将其他变量(例如SOME-HEADER )添加到子序列请求中。

  • Web服务器将从旧的请求创建一个新的HTTP请求,并附加附加标题变量(例如Some-Header ),然后将修改后的请求发送到子任务应用程序。

如果无效,

  • 授权器应用程序应以HTTP状态为不200 http状态响应,并且要显示登录失败的内容。

  • Web服务器将跳过响应者,直接显示授权者的回应。

代码

gofast\”
)

func myApp() http.Handler {
// … any normal http.Handler, using gofast or not
return h
}

func main() {
address := os.Getenv(\”FASTCGI_ADDR\”)
connFactory := gofast .SimpleConnFactory(\”tcp\”, address)
clientFactory := gofast .SimpleClientFactory(connFactory)

// authorization with php
authSess := gofast .Chain(
gofast .NewAuthPrepare(),
gofast .NewFileEndpoint(\”/var/www/html/authorization.php\”),
)( gofast .BasicSession)
authorizer := gofast .NewAuthorizer(
authSess,
gofast .SimpleConnFactory(network, address)
)

// wrap the actual app
http.Handle(\”/\”, authorizer.Wrap(myApp()))

// serve at 8080 port
log.Fatal(http.ListenAndServe(\”:8080\”, nil))
}
\”>

 package main

import (
	\"net/http\"
	\"time\"

	\"github.com/yookoala/ gofast \"
)

func myApp () http. Handler {
  // ... any normal http.Handler, using gofast or not
	return h
}

func main () {
	address := os . Getenv ( \"FASTCGI_ADDR\" )
	connFactory := gofast . SimpleConnFactory ( \"tcp\" , address )
	clientFactory := gofast . SimpleClientFactory ( connFactory )

	// authorization with php
	authSess := gofast . Chain (
		gofast . NewAuthPrepare (),
		gofast . NewFileEndpoint ( \"/var/www/html/authorization.php\" ),
	)( gofast . BasicSession )
	authorizer := gofast . NewAuthorizer (
		authSess ,
		gofast . SimpleConnFactory ( network , address )
	)

	// wrap the actual app
	http . Handle ( \"/\" , authorizer . Wrap ( myApp ()))

	// serve at 8080 port
	log . Fatal ( http . ListenAndServe ( \":8080\" , nil ))
}

FastCGI过滤器

FastCGI在发送之前指定了过滤Web服务器资产的过滤角色。与通常的FastCGI应用程序(IE响应者)不同,请求的数据位于Web服务器端。因此,当要求时,Web服务器将将这些数据传递给应用程序。

代码

gofast\”
)

func main() {
address := os.Getenv(\”FASTCGI_ADDR\”)
connFactory := gofast .SimpleConnFactory(\”tcp\”, address)
clientFactory := gofast .SimpleClientFactory(connFactory)

// Note: The local file system \”/var/www/html/\” only need to be
// local to web server. No need for the FastCGI application to access
// it directly.
connFactory := gofast .SimpleConnFactory(network, address)
http.Handle(\”/\”, gofast .NewHandler(
gofast .NewFilterLocalFS(\”/var/www/html/\”)( gofast .BasicSession),
clientFactory,
))

// serve at 8080 port
log.Fatal(http.ListenAndServe(\”:8080\”, nil))
}
\”>

 package main

import (
	\"net/http\"
	\"time\"

	\"github.com/yookoala/ gofast \"
)

func main () {
	address := os . Getenv ( \"FASTCGI_ADDR\" )
	connFactory := gofast . SimpleConnFactory ( \"tcp\" , address )
	clientFactory := gofast . SimpleClientFactory ( connFactory )

	// Note: The local file system \"/var/www/html/\" only need to be
	// local to web server. No need for the FastCGI application to access
	// it directly.
	connFactory := gofast . SimpleConnFactory ( network , address )
	http . Handle ( \"/\" , gofast . NewHandler (
		gofast . NewFilterLocalFS ( \"/var/www/html/\" )( gofast . BasicSession ),
		clientFactory ,
	))

	// serve at 8080 port
	log . Fatal ( http . ListenAndServe ( \":8080\" , nil ))
}

汇总客户

为了拥有更好,更受控的扩展属性,您可以使用客户室扩展客户端。

代码

gofast\”
)

func main() {
// Get fastcgi application server tcp address
// from env FASTCGI_ADDR. Then configure
// connection factory for the address.
address := os.Getenv(\”FASTCGI_ADDR\”)
connFactory := gofast .SimpleConnFactory(\”tcp\”, address)

// handles static assets in the assets folder
http.Handle(\”/assets/\”,
http.StripPrefix(\”/assets/\”,
http.FileSystem(http.Dir(\”/var/www/html/assets\”))))

// handle all scripts in document root
// extra pooling layer
pool := gofast .NewClientPool(
gofast .SimpleClientFactory(connFactory),
10, // buffer size for pre-created client-connection
30*time.Second, // life span of a client before expire
)
http.Handle(\”/\”, gofast .NewHandler(
gofast .NewPHPFS(\”/var/www/html\”)( gofast .BasicSession),
pool.CreateClient,
))

// serve at 8080 port
log.Fatal(http.ListenAndServe(\”:8080\”, nil))
}
\”>

 package main

import (
	\"fmt\"
	\"net/http\"
	\"os\"

	\"github.com/yookoala/ gofast \"
)

func main () {
	// Get fastcgi application server tcp address
	// from env FASTCGI_ADDR. Then configure
	// connection factory for the address.
	address := os . Getenv ( \"FASTCGI_ADDR\" )
	connFactory := gofast . SimpleConnFactory ( \"tcp\" , address )

	// handles static assets in the assets folder
	http . Handle ( \"/assets/\" ,
		http . StripPrefix ( \"/assets/\" ,
			http . FileSystem ( http . Dir ( \"/var/www/html/assets\" ))))

	// handle all scripts in document root
	// extra pooling layer
	pool := gofast . NewClientPool (
		gofast . SimpleClientFactory ( connFactory ),
		10 , // buffer size for pre-created client-connection
		30 * time . Second , // life span of a client before expire
	)
	http . Handle ( \"/\" , gofast . NewHandler (
		gofast . NewPHPFS ( \"/var/www/html\" )( gofast . BasicSession ),
		pool . CreateClient ,
	))

	// serve at 8080 port
	log . Fatal ( http . ListenAndServe ( \":8080\" , nil ))
}

完整的例子

请参阅示例用法:

  • php
  • python3
  • nodejs

作者

该图书馆由考拉·杨(Koala Yeung)撰写。

贡献

欢迎您为这个图书馆做出贡献。

要报告错误,请使用问题跟踪器。

要修复现有错误或实施新功能,请:

  1. 检查问题跟踪器并提取现有讨论请求。
  2. 如果没有,请打开一个新问题进行讨论。
  3. 写测试。
  4. 打开引用该问题的拉请请求。
  5. 玩得开心 :-)

执照

该库是根据BSD式许可发布的。请在此存储库中找到许可证文件

下载源码

通过命令行克隆项目:

git clone https://github.com/yookoala/gofast.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 开发教程 gofast https://www.zuozi.net/31999.html

material start
上一篇: material start
ocLazyLoad
下一篇: ocLazyLoad
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务