Host
Listen and Serve
You can start the server(s) listening to any type of net.Listener
or even http.Server
instance. The method for initialization of the server should be passed at the end, via Run
function.
The most common method that Go developers are use to serve their servers are by passing a network address with form of "hostname:ip". With Iris we use the iris.Addr
which is an iris.Runner
type
Sometimes you choose to have full control over the http.Server
instance.
The most advanced usage is to create a custom net.Listener
and pass that to app.Run
.
A more complete example, using the unix-only socket files feature.
UNIX and BSD hosts can take advandage of the reuse port feature.
HTTP/2 and Secure
If you have local certification and server key files you can use the iris.TLS
to serve https://
.
The method you should use when your app is ready for the outside world is the iris.AutoTLS
which starts a secure server with certifications provided by https://letsencrypt.org for free.
Any iris.Runner
iris.Runner
There may be times that you want something very special to listen on, which is not a type of net.Listener
. You are able to do that by iris.Raw
, but you're responsible of that method
Host configurators
All the above forms of listening are accepting a last, variadic argument of func(*iris.Supervisor)
. This is used to add configurators for that specific host you passed via those functions.
For example let's say that we want to add a callback which is fired when the server is shutdown
You can even do that before app.Run
method, but the difference is that these host configurators will be executed to all hosts that you may use to serve your web app (via app.NewHost
we'll see that in a minute)
Access to all hosts that serve your application can be provided by the Application#Hosts
field, after the Run
method.
But the most common scenario is that you may need access to the host before the app.Run
method, there are two ways of gain access to the host supervisor, read below.
We have already saw how to configure all application's hosts by second argument of app.Run
or app.ConfigureHost
. There is one more way which suits better for simple scenarios and that is to use the app.NewHost
to create a new host and use one of its Serve
or Listen
functions to start the application via the iris#Raw
Runner.
Note that this way needs an extra import of the net/http
package.
Example Code:
Multi hosts
You can serve your Iris web app using more than one server, the iris.Router
is compatible with the net/http/Handler
function therefore, as you can understand, it can be used to be adapted at any net/http
server, however there is an easier way, by using the app.NewHost
which is also copying all the host configurators and it closes all the hosts attached to the particular web app on app.Shutdown
.
Shutdown (Gracefully)
Let's continue by learning how to catch CONTROL+C/COMMAND+C or unix kill command and shutdown the server gracefully.
Gracefully Shutdown on CONTROL+C/COMMAND+C or when kill command sent is ENABLED BY-DEFAULT.
In order to manually manage what to do when app is interrupted, we have to disable the default behavior with the option WithoutInterruptHandler
and register a new interrupt handler (globally, across all possible hosts).
Example code:
Last updated