摸石头过河

虽然说go自带web服务器,但实际运行时并不能完全符合非功能上的要求。比如很多客户喜欢在一台服务器的一个端口(:80)上运行很多不同语言的web应用,例如php、c#、java,甚至asp等等,那么使用iis肯定是第一选择。

HttpPlatformHandlerHttpPlatformHandler

HttpPlatformHandler module doesn’t work for me. I found this post by Sutean Rutjanalard on Medium very helpful, which use ASP.NET Core Module instead.”

正式开始

ASP.NET Core Module
golang
goWeb.exe
asp.net core modulegoWeb.exe
// 1. 安装ASP.NET Core Module后,获取iis中该站点实际端口,这样以后再也不用管某个应用的端口号是什么了
port := os.Getenv("ASPNETCORE_PORT")
// 2. 常用端口号
// port := "80"

err := http.ListenAndServe(":"+port, nil)
if err != nil {
	fmt.Println("服务器启动错误:", err)
}
web.config

这是最重要的一步

goWeb.exeweb.configasp.net core modulegoWeb.exe
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
        </handlers>
        <aspNetCore processPath=".\goWeb.exe" />
    </system.webServer>
</configuration>
四、运行

在浏览器里打开该站点,页面顺利展现!

goWeb.exe
五、实测
a.phpa.htmllocalhost/a.phplocalhost/a.html.php.htmlgoWeb.exea.htmla.htmllocalhostlocalhost/a.htmllocalhosta.html

这印证了“所有请求的处理都是交给go,怎么处理都由go决定”这句话,iis只是一个发包单位,页面只能根据go程序内设置好的路由进行显示。