# IIS Hosting Golang Program
------------------------------
## Objective
* host your golang app server with Internet Information Server (IIS)
* try to make IIS take full control of your golang app including lifecycle and recovery
## My Footprint
1. Googled `run golang in iis`
2. found there are two ways to achieve our goal ([link](https://www.reddit.com/r/golang/comments/3con77/if_im_forced_to_run_go_on_windows_server/csynnjn)):
* Run your Go app as a Windows service. Serve directly or via reverse proxy like the IIS ARR module
* Run your Go app behind IIS using the httpPlatformHandler ([ref](https://docs.microsoft.com/en-us/iis/extensions/httpplatformhandler/httpplatformhandler-configuration-reference)) (*buggy, will not mention here*)
3. tried the httpPlatformHandler way and found it buggy
4. tried the first solution where the instruction is as follows
## Instruction
### Preparation
1. Provision an EC2 instance of Windows Server 2016
2. Login with RDP
3. Install IIS by clicking **Add roles and features** in Server Manager ([ref](http://slashlook.com/archive2017/20170702.html))
4. Download [WebPI](https://www.microsoft.com/web/downloads/platform.aspx) and install it
### Setup Your Site in IIS
1. Open IIS
2. Open Application Pools and click **Add Application Pool**
3. Fill out the form, choose **No Managed Code** in .NET CLR version field and submit.
4. Create a folder where you like, for example: `C:\GUP`
5. Open Sites and click **Add Website...**
6. Fill out the form and submit
* choose a name of your site
* select application pool that you just created
* fill physical path of content directory which is the folder you created (`C:\GUP`)
* determine which port you'd like to use
* leave the hostname blank
### Install Required Modules
1. Go to your site in IIS, and click **WebPI**
2. search for *URL Rewrite*, *Application Request Routing 3.0*; select and install them
3. after the installation is done, restart your IIS
### Make a Reverse Proxy
1. Go to your site in IIS, and click **URL Rewrite**
2. Click **Add Rule(s)...** and choose **Reverse Proxy**
3. enter the host you are going to forward requests, for example: `127.0.0.1:8888`; submit the form
### Make Your Golang Program a Daemon
1. Refer to [go-svc](https://github.com/judwhite/go-svc) and revise your program
2. Once your revision got done, build it and open `cmd` as Administrator
3. Enter the following command:
```
> sc create YOUR_DAEMON_NAME binPath= "YOUR_GOLANG_EXECUTABLE_PATH" DisplayName= "YOUR_DAEMON_DISPLAY_NAME"
```
(notice the blank after `=` sign)
4. Go to Services via clicking Start button and searching for `Services`
5. Find the service you just create and start it
6. Back to your site in IIS, click **Browse Website**
7. See if it succeeds to forward your request to the daemon
## Future Work
* app log to windows event log ([ref1](https://stackoverflow.com/questions/8502780/how-do-you-read-the-console-output-from-windows-service), [ref2](https://godoc.org/golang.org/x/sys/windows/svc/eventlog), [ref3](https://github.com/sirupsen/logrus/pull/884/commits/0602c483e795c9e69035b25074f3eacb749ef1c8))
* passing arguments to a windows service