Try   HackMD

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):
    • 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) (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)
  4. Download WebPI 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 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)

  1. Go to Services via clicking Start button and searching for Services
  2. Find the service you just create and start it
  3. Back to your site in IIS, click Browse Website
  4. See if it succeeds to forward your request to the daemon

Future Work

  • app log to windows event log (ref1, ref2, ref3)
  • passing arguments to a windows service