mirror of
https://github.com/kataras/iris.git
synced 2025-12-20 03:17:04 +00:00
reorganization of _examples and add some new examples such as iris+groupcache+mysql+docker
Former-commit-id: ed635ee95de7160cde11eaabc0c1dcb0e460a620
This commit is contained in:
251
_examples/http-server/README.md
Normal file
251
_examples/http-server/README.md
Normal file
@@ -0,0 +1,251 @@
|
||||
# Hosts
|
||||
|
||||
## 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 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
|
||||
|
||||
```go
|
||||
// Listening on tcp with network address 0.0.0.0:8080
|
||||
app.Listen(":8080")
|
||||
```
|
||||
|
||||
Sometimes you have created a standard net/http server somewhere else in your app and want to use that to serve the Iris web app
|
||||
|
||||
```go
|
||||
// Same as before but using a custom http.Server which may being used somewhere else too
|
||||
app.Run(iris.Server(&http.Server{Addr:":8080"}))
|
||||
```
|
||||
|
||||
The most advanced usage is to create a custom or a standard `net.Listener` and pass that to `app.Run`
|
||||
|
||||
```go
|
||||
// Using a custom net.Listener
|
||||
l, err := net.Listen("tcp4", ":8080")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
app.Run(iris.Listener(l))
|
||||
```
|
||||
|
||||
A more complete example, using the unix-only socket files feature
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"net"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
// UNIX socket
|
||||
if errOs := os.Remove(socketFile); errOs != nil && !os.IsNotExist(errOs) {
|
||||
app.Logger().Fatal(errOs)
|
||||
}
|
||||
|
||||
l, err := net.Listen("unix", socketFile)
|
||||
|
||||
if err != nil {
|
||||
app.Logger().Fatal(err)
|
||||
}
|
||||
|
||||
if err = os.Chmod(socketFile, mode); err != nil {
|
||||
app.Logger().Fatal(err)
|
||||
}
|
||||
|
||||
app.Run(iris.Listener(l))
|
||||
}
|
||||
```
|
||||
|
||||
UNIX and BSD hosts can take advantage of the reuse port feature
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
// Package tcplisten provides customizable TCP net.Listener with various
|
||||
// performance-related options:
|
||||
//
|
||||
// - SO_REUSEPORT. This option allows linear scaling server performance
|
||||
// on multi-CPU servers.
|
||||
// See https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ for details.
|
||||
//
|
||||
// - TCP_DEFER_ACCEPT. This option expects the server reads from the accepted
|
||||
// connection before writing to them.
|
||||
//
|
||||
// - TCP_FASTOPEN. See https://lwn.net/Articles/508865/ for details.
|
||||
"github.com/valyala/tcplisten"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// go get github.com/valyala/tcplisten
|
||||
// go run main.go
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello World!</h1>")
|
||||
})
|
||||
|
||||
listenerCfg := tcplisten.Config{
|
||||
ReusePort: true,
|
||||
DeferAccept: true,
|
||||
FastOpen: true,
|
||||
}
|
||||
|
||||
l, err := listenerCfg.NewListener("tcp", ":8080")
|
||||
if err != nil {
|
||||
app.Logger().Fatal(err)
|
||||
}
|
||||
|
||||
app.Run(iris.Listener(l))
|
||||
}
|
||||
```
|
||||
|
||||
### HTTP/2 and Secure
|
||||
|
||||
If you have signed file keys you can use the `iris.TLS` to serve `https` based on those certification keys
|
||||
|
||||
```go
|
||||
// TLS using files
|
||||
app.Run(iris.TLS("127.0.0.1:443", "mycert.cert", "mykey.key"))
|
||||
```
|
||||
|
||||
The method you should use when your app is ready for **production** is the `iris.AutoTLS` which starts a secure server with automated certifications provided by https://letsencrypt.org for **free**
|
||||
|
||||
```go
|
||||
// Automatic TLS
|
||||
app.Run(iris.AutoTLS(":443", "example.com", "admin@example.com"))
|
||||
```
|
||||
|
||||
### Any `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
|
||||
|
||||
```go
|
||||
// Using any func() error,
|
||||
// the responsibility of starting up a listener is up to you with this way,
|
||||
// for the sake of simplicity we will use the
|
||||
// ListenAndServe function of the `net/http` package.
|
||||
app.Run(iris.Raw(&http.Server{Addr:":8080"}).ListenAndServe)
|
||||
```
|
||||
|
||||
## 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
|
||||
|
||||
```go
|
||||
app.Run(iris.Addr(":8080", func(h *iris.Supervisor) {
|
||||
h.RegisterOnShutdown(func() {
|
||||
println("server terminated")
|
||||
})
|
||||
}))
|
||||
```
|
||||
|
||||
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)
|
||||
|
||||
```go
|
||||
app := iris.New()
|
||||
app.ConfigureHost(func(h *iris.Supervisor) {
|
||||
h.RegisterOnShutdown(func() {
|
||||
println("server terminated")
|
||||
})
|
||||
})
|
||||
app.Listen(":8080")
|
||||
```
|
||||
|
||||
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:
|
||||
|
||||
```go
|
||||
h := app.NewHost(&http.Server{Addr:":8080"})
|
||||
h.RegisterOnShutdown(func(){
|
||||
println("server terminated")
|
||||
})
|
||||
|
||||
app.Run(iris.Raw(h.ListenAndServe))
|
||||
```
|
||||
|
||||
## 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`.
|
||||
|
||||
```go
|
||||
app := iris.New()
|
||||
app.Get("/", indexHandler)
|
||||
|
||||
// run in different goroutine in order to not block the main "goroutine".
|
||||
go app.Listen(":8080")
|
||||
// start a second server which is listening on tcp 0.0.0.0:9090,
|
||||
// without "go" keyword because we want to block at the last server-run.
|
||||
app.NewHost(&http.Server{Addr:":9090"}).ListenAndServe()
|
||||
```
|
||||
|
||||
## 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:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
timeout := 10 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
// close all hosts
|
||||
app.Shutdown(ctx)
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||
})
|
||||
|
||||
app.Listen(":8080", iris.WithoutInterruptHandler)
|
||||
}
|
||||
```
|
||||
32
_examples/http-server/custom-httpserver/easy-way/main.go
Normal file
32
_examples/http-server/custom-httpserver/easy-way/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// Any custom fields here. Handler and ErrorLog are set to the server automatically
|
||||
srv := &http.Server{Addr: ":8080"}
|
||||
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/mypath
|
||||
app.Run(iris.Server(srv)) // same as app.Listen(":8080")
|
||||
|
||||
// More:
|
||||
// see "multi" if you need to use more than one server at the same app.
|
||||
//
|
||||
// for a custom listener use: iris.Listener(net.Listener) or
|
||||
// iris.TLS(cert,key) or iris.AutoTLS(), see "custom-listener" example for those.
|
||||
}
|
||||
47
_examples/http-server/custom-httpserver/multi/main.go
Normal file
47
_examples/http-server/custom-httpserver/multi/main.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// Note: It's not needed if the first action is "go app.Run".
|
||||
if err := app.Build(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// start a secondary server listening on localhost:9090.
|
||||
// use "go" keyword for Listen functions if you need to use more than one server at the same app.
|
||||
//
|
||||
// http://localhost:9090/
|
||||
// http://localhost:9090/mypath
|
||||
srv1 := &http.Server{Addr: ":9090", Handler: app}
|
||||
go srv1.ListenAndServe()
|
||||
println("Start a server listening on http://localhost:9090")
|
||||
|
||||
// start a "second-secondary" server listening on localhost:5050.
|
||||
//
|
||||
// http://localhost:5050/
|
||||
// http://localhost:5050/mypath
|
||||
srv2 := &http.Server{Addr: ":5050", Handler: app}
|
||||
go srv2.ListenAndServe()
|
||||
println("Start a server listening on http://localhost:5050")
|
||||
|
||||
// Note: app.Run is totally optional, we have already built the app with app.Build,
|
||||
// you can just make a new http.Server instead.
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/mypath
|
||||
app.Listen(":8080") // Block here.
|
||||
}
|
||||
40
_examples/http-server/custom-httpserver/std-way/main.go
Normal file
40
_examples/http-server/custom-httpserver/std-way/main.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// call .Build before use the 'app' as a http.Handler on a custom http.Server
|
||||
app.Build()
|
||||
|
||||
// create our custom server and assign the Handler/Router
|
||||
srv := &http.Server{Handler: app, Addr: ":8080"} // you have to set Handler:app and Addr, see "iris-way" which does this automatically.
|
||||
// http://localhost:8080/
|
||||
// http://localhost:8080/mypath
|
||||
println("Start a server listening on http://localhost:8080")
|
||||
srv.ListenAndServe() // same as app.Listen(":8080")
|
||||
|
||||
// Notes:
|
||||
// Banner is not shown at all. Same for the Interrupt Handler, even if app's configuration allows them.
|
||||
//
|
||||
// `.Run` is the only one function that cares about those three.
|
||||
|
||||
// More:
|
||||
// see "multi" if you need to use more than one server at the same app.
|
||||
//
|
||||
// for a custom listener use: iris.Listener(net.Listener) or
|
||||
// iris.TLS(cert,key) or iris.AutoTLS(), see "custom-listener" example for those.
|
||||
}
|
||||
28
_examples/http-server/custom-listener/main.go
Normal file
28
_examples/http-server/custom-listener/main.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from %s", ctx.Path())
|
||||
})
|
||||
|
||||
// create any custom tcp listener, unix sock file or tls tcp listener.
|
||||
l, err := net.Listen("tcp4", ":8080")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// use of the custom listener
|
||||
app.Run(iris.Listener(l))
|
||||
}
|
||||
42
_examples/http-server/custom-listener/unix-reuseport/main.go
Normal file
42
_examples/http-server/custom-listener/unix-reuseport/main.go
Normal file
@@ -0,0 +1,42 @@
|
||||
// +build linux darwin dragonfly freebsd netbsd openbsd rumprun
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
// Package tcplisten provides customizable TCP net.Listener with various
|
||||
// performance-related options:
|
||||
//
|
||||
// - SO_REUSEPORT. This option allows linear scaling server performance
|
||||
// on multi-CPU servers.
|
||||
// See https://www.nginx.com/blog/socket-sharding-nginx-release-1-9-1/ for details.
|
||||
//
|
||||
// - TCP_DEFER_ACCEPT. This option expects the server reads from the accepted
|
||||
// connection before writing to them.
|
||||
//
|
||||
// - TCP_FASTOPEN. See https://lwn.net/Articles/508865/ for details.
|
||||
"github.com/valyala/tcplisten"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// You can run the same app as many times as you want.
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<b>Hello World!</b>")
|
||||
})
|
||||
|
||||
listenerCfg := tcplisten.Config{
|
||||
ReusePort: true,
|
||||
DeferAccept: true,
|
||||
FastOpen: true,
|
||||
}
|
||||
|
||||
l, err := listenerCfg.NewListener("tcp4", ":8080")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app.Run(iris.Listener(l))
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
// +build windows
|
||||
|
||||
package main
|
||||
|
||||
func main() {
|
||||
panic("windows operating system does not support this feature")
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
stdContext "context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||
})
|
||||
|
||||
go func() {
|
||||
ch := make(chan os.Signal, 1)
|
||||
signal.Notify(ch,
|
||||
// kill -SIGINT XXXX or Ctrl+c
|
||||
os.Interrupt,
|
||||
syscall.SIGINT, // register that too, it should be ok
|
||||
// os.Kill is equivalent with the syscall.Kill
|
||||
os.Kill,
|
||||
syscall.SIGKILL, // register that too, it should be ok
|
||||
// kill -SIGTERM XXXX
|
||||
syscall.SIGTERM,
|
||||
)
|
||||
select {
|
||||
case <-ch:
|
||||
println("shutdown...")
|
||||
|
||||
timeout := 10 * time.Second
|
||||
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
|
||||
defer cancel()
|
||||
app.Shutdown(ctx)
|
||||
}
|
||||
}()
|
||||
|
||||
// Start the server and disable the default interrupt handler in order to
|
||||
// handle it clear and simple by our own, without any issues.
|
||||
app.Listen(":8080", iris.WithoutInterruptHandler)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
stdContext "context"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
// Before continue:
|
||||
//
|
||||
// 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).
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
iris.RegisterOnInterrupt(func() {
|
||||
timeout := 10 * time.Second
|
||||
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
|
||||
defer cancel()
|
||||
// close all hosts
|
||||
app.Shutdown(ctx)
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML(" <h1>hi, I just exist in order to see if the server is closed</h1>")
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
app.Listen(":8080", iris.WithoutInterruptHandler)
|
||||
}
|
||||
7
_examples/http-server/http3-quic/go.mod
Normal file
7
_examples/http-server/http3-quic/go.mod
Normal file
@@ -0,0 +1,7 @@
|
||||
module github.com/kataras/iris/_examples/http-server/http3-quic
|
||||
|
||||
go 1.14
|
||||
|
||||
require (
|
||||
github.com/prometheus/client_golang v1.0.0
|
||||
)
|
||||
18
_examples/http-server/http3-quic/localhost.cert
Normal file
18
_examples/http-server/http3-quic/localhost.cert
Normal file
@@ -0,0 +1,18 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIC+zCCAeOgAwIBAgIJAI5gi8BzcdQgMA0GCSqGSIb3DQEBBQUAMBQxEjAQBgNV
|
||||
BAMMCWxvY2FsaG9zdDAeFw0xOTA3MDkxMjExNDBaFw0yOTA3MDYxMjExNDBaMBQx
|
||||
EjAQBgNVBAMMCWxvY2FsaG9zdDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
|
||||
ggEBAM1eSGwoYznwYtt+/hrv9o4FYxJFlxIMX6WN3c2y3rr8uOwExkz2RuU9SzgF
|
||||
qn0ctP1DoIKWNO0/L5j5Bjy2do0k8wHYPbTqb9zG64NGZj1lhkHgYXWyCD9U41DX
|
||||
V1DiJ2JiCRBadowFRRf3/KIPf3xnrCBSCoQwdfIeJJtAF9El2/TnTrGq9N98FJqR
|
||||
dCNyi+zY/iuymcA3aDOyYNjxSiuV//7ONEql5dxvRlhkjCHgrQ/rIbH/lSFAS+NG
|
||||
H/6ksEBX2+Q1LlQBaFiGeEnjVpCymrRfADw7bKyqMav39Nndw4UZ1r5MSG20YtXM
|
||||
dE4lA6VfAKzIZs2n87WF7OO8Y2sCAwEAAaNQME4wHQYDVR0OBBYEFKYItamcYz4Z
|
||||
NiDy3I2zflU4A7ywMB8GA1UdIwQYMBaAFKYItamcYz4ZNiDy3I2zflU4A7ywMAwG
|
||||
A1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBACSoSrEArlgPJ1ftlSMkThUR
|
||||
atTqJ/yB0rSyRxsct0C04qX880VP7avnKc0UhaDruXRjdAVn4X8KI+j6azQSKT40
|
||||
KRSVBinnonE0D4DBMCUVDFtkBW3FZJXAYyIYdF/6J3Khn/ksm7VDcVxYI1rjg87B
|
||||
U6aJytOkoGA2WGQOB1L0HtnTsarg/SKP/LSDUFT+XK6zTE7uogAUrpbwlpIaxc+8
|
||||
3jXvgxEdPj9Rq9Nt8/zjCkCGB2EusPPnqxcbqZb5WcGPCIlg3ChKq7vpaQld6KqG
|
||||
70jT7BZ2fqWSVJ5szRoS8WpKy1SZEx/+AA7VojMzkkw4RLb66zr1v7029b51ol4=
|
||||
-----END CERTIFICATE-----
|
||||
27
_examples/http-server/http3-quic/localhost.key
Normal file
27
_examples/http-server/http3-quic/localhost.key
Normal file
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpAIBAAKCAQEAzV5IbChjOfBi237+Gu/2jgVjEkWXEgxfpY3dzbLeuvy47ATG
|
||||
TPZG5T1LOAWqfRy0/UOggpY07T8vmPkGPLZ2jSTzAdg9tOpv3Mbrg0ZmPWWGQeBh
|
||||
dbIIP1TjUNdXUOInYmIJEFp2jAVFF/f8og9/fGesIFIKhDB18h4km0AX0SXb9OdO
|
||||
sar033wUmpF0I3KL7Nj+K7KZwDdoM7Jg2PFKK5X//s40SqXl3G9GWGSMIeCtD+sh
|
||||
sf+VIUBL40Yf/qSwQFfb5DUuVAFoWIZ4SeNWkLKatF8APDtsrKoxq/f02d3DhRnW
|
||||
vkxIbbRi1cx0TiUDpV8ArMhmzafztYXs47xjawIDAQABAoIBADvaKok7DBAquuT1
|
||||
keEP5m9lqoX8uhaMfKOnQOleJAOi+9HtYk2zyN2ui2l8XT+xSh41w2XLmQk7zQds
|
||||
LCEtnEduaVQ0TWeYm5lgb+sGbW2fVQ2F82F1zWmHt+grmkr8XjYSFEor0zjjoEtn
|
||||
/rzMf38mR8fzoRT9eqJhnpGQkGBnfo0SyDKIDu9yYFM7yJ5s4KOTVsMGfavjQYgJ
|
||||
ssQm0KQTo8HbYHieS6drEYFRwAgT8U1NFoVq24yU+Voyy7CR5rjfOsO9gSyStVSH
|
||||
nTkePmSIcpeQBfpfT3jh+STSS12eqhFCx1SqAUptUehQpOJhnWAyeSGjVLlrHqDR
|
||||
bCtSWCECgYEA62J2AC8bRQ8omoEOc5h1/kwPTcLYjhOCxwwU8ky7qve173MYZUeT
|
||||
dxWZx3haahccPyUKtsiGBdKYyYKSOIJMSMmwkG4uy6r5nhwNV+btEIL6Npj+XKMe
|
||||
PaATA4gBLRQNwcbUlZWLYc0Y6CXFnPA+atEa7EOEBfdgUqHOym1HsAUCgYEA31rU
|
||||
GPyv8R7Z1UXmxu70M8RwxKS4XhlP7RRg3Zuuqx6WWX4M85Np3wS4UWgQjGKICwoM
|
||||
D4XKZQOya5p+v7a1RUZt/OD6eJ0TjypW5fBmK2yvBUQkQCsVcFjBL3F+yv2Yk1sh
|
||||
KlPky4wXpDcmWXGmesgmyhpKIwL+qXEAJEaL0K8CgYABWqKlI6A7iHfKU726ioD7
|
||||
QoLABsPqJVCWRoqETk6yEBS62OWmB4Bgqf4leJrEi3d9IYBrRsIGnIyGdDrVGmLH
|
||||
9GkQm6GnSEeBUlX9UHXCp4467CxiagnNfvM9DPY8xSXDHJqydZbErEJda4I0gelK
|
||||
AgPuogDLa/3g289tuK015QKBgQDKcj9Qrqiiur3jC8rTgX8i9OjptAvQbsz9LL1n
|
||||
4FZ/j+fjEdeXZ4RMurB+SP7G4ABDUUYBQ9lhmeo8kfpUtryzH9VNonYkoOs7lrrR
|
||||
DAbvUUGKWmspJmP2QtxHrm2ofBexaKY1AXmd7Ur4c2x1IggtvgE6qn2MIojE+EGS
|
||||
n8bWzQKBgQClc/j4GYNNMUYOknxXMH/ec8PBDUtn098YuFL+s7DmRHimtkkjRo1A
|
||||
BtV7F8KpLruWohxXWy4QZ6HsAO255gIJ8DCbEAFCj96EHNx8KADSm3qbslu2fIB0
|
||||
zCsVaETGNAjV/d5hAEdYmgynCY49gNXV55ehV1UqzFoEfZVM9j5hUg==
|
||||
-----END RSA PRIVATE KEY-----
|
||||
29
_examples/http-server/http3-quic/main.go
Normal file
29
_examples/http-server/http3-quic/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
|
||||
"github.com/lucas-clemente/quic-go/http3"
|
||||
)
|
||||
|
||||
/*
|
||||
$ go get -u github.com/lucas-clemente/quic-go/...
|
||||
# or if you're using GO MODULES:
|
||||
$ go get github.com/lucas-clemente/quic-go@master
|
||||
*/
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from Index")
|
||||
})
|
||||
|
||||
// app.Configure(iris.WithOptimizations, or any other core config here)
|
||||
// app.Build()
|
||||
// http3.ListenAndServe(":443", "./localhost.cert", "./localhost.key", app)
|
||||
// OR:
|
||||
app.Run(iris.Raw(func() error {
|
||||
return http3.ListenAndServe(":443", "./localhost.cert", "./localhost.key", app)
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.ConfigureHost(func(host *iris.Supervisor) { // <- HERE: IMPORTANT
|
||||
// You can control the flow or defer something using some of the host's methods:
|
||||
// host.RegisterOnError
|
||||
// host.RegisterOnServe
|
||||
host.RegisterOnShutdown(func() {
|
||||
app.Logger().Infof("Application shutdown on signal")
|
||||
})
|
||||
})
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello</h1>\n")
|
||||
})
|
||||
|
||||
app.Listen(":8080", iris.WithoutServerError(iris.ErrServerClosed))
|
||||
|
||||
/* There are more easy ways to notify for global shutdown using the `iris.RegisterOnInterrupt` for default signal interrupt events.
|
||||
You can even go it even further by looking at the: "graceful-shutdown" example.
|
||||
*/
|
||||
}
|
||||
36
_examples/http-server/listen-addr-public/main.go
Normal file
36
_examples/http-server/listen-addr-public/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package main
|
||||
|
||||
import "github.com/kataras/iris/v12"
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello World!</h1>")
|
||||
|
||||
// Will print the ngrok public domain
|
||||
// that your app is using to be served online.
|
||||
ctx.Writef("From: %s",
|
||||
ctx.Application().ConfigurationReadOnly().GetVHost())
|
||||
})
|
||||
|
||||
app.Listen(":8080", iris.WithTunneling, iris.WithLogLevel("debug"))
|
||||
|
||||
/* The full configuration can be set as:
|
||||
app.Listen(":8080", iris.WithConfiguration(
|
||||
iris.Configuration{
|
||||
Tunneling: iris.TunnelingConfiguration{
|
||||
AuthToken: "my-ngrok-auth-client-token",
|
||||
Bin: "/bin/path/for/ngrok",
|
||||
Region: "eu",
|
||||
WebInterface: "127.0.0.1:4040",
|
||||
Tunnels: []iris.Tunnel{
|
||||
{
|
||||
Name: "MyApp",
|
||||
Addr: ":8080",
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
*/
|
||||
}
|
||||
17
_examples/http-server/listen-addr/main.go
Normal file
17
_examples/http-server/listen-addr/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello World!</h1>")
|
||||
})
|
||||
|
||||
// http://localhost:8080
|
||||
// Identical to: app.Run(iris.Addr(":8080"))
|
||||
app.Listen(":8080")
|
||||
}
|
||||
24
_examples/http-server/listen-addr/omit-server-errors/main.go
Normal file
24
_examples/http-server/listen-addr/omit-server-errors/main.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello World!</h1>")
|
||||
})
|
||||
|
||||
err := app.Listen(":8080", iris.WithoutServerError(iris.ErrServerClosed))
|
||||
if err != nil {
|
||||
// do something
|
||||
}
|
||||
// same as:
|
||||
// err := app.Listen(":8080")
|
||||
// import "errors"
|
||||
// if errors.Is(err, iris.ErrServerClosed) {
|
||||
// [...]
|
||||
// }
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
stdContext "context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func logger(app *iris.Application) *bytes.Buffer {
|
||||
buf := &bytes.Buffer{}
|
||||
|
||||
app.Logger().SetOutput(buf)
|
||||
|
||||
// disable the "Now running at...." in order to have a clean log of the error.
|
||||
// we could attach that on `Run` but better to keep things simple here.
|
||||
app.Configure(iris.WithoutStartupLog)
|
||||
return buf
|
||||
}
|
||||
|
||||
func TestListenAddr(t *testing.T) {
|
||||
app := iris.New()
|
||||
// we keep the logger running as well but in a controlled way.
|
||||
log := logger(app)
|
||||
|
||||
// close the server at 3-6 seconds
|
||||
go func() {
|
||||
time.Sleep(3 * time.Second)
|
||||
ctx, cancel := stdContext.WithTimeout(stdContext.TODO(), 3*time.Second)
|
||||
defer cancel()
|
||||
app.Shutdown(ctx)
|
||||
}()
|
||||
|
||||
err := app.Listen(":9829")
|
||||
// in this case the error should be logged and return as well.
|
||||
if err != iris.ErrServerClosed {
|
||||
t.Fatalf("expecting err to be `iris.ErrServerClosed` but got: %v", err)
|
||||
}
|
||||
|
||||
expectedMessage := iris.ErrServerClosed.Error()
|
||||
|
||||
if got := log.String(); !strings.Contains(got, expectedMessage) {
|
||||
t.Fatalf("expecting to log to contains the:\n'%s'\ninstead of:\n'%s'", expectedMessage, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListenAddrWithoutServerErr(t *testing.T) {
|
||||
app := iris.New()
|
||||
// we keep the logger running as well but in a controlled way.
|
||||
log := logger(app)
|
||||
|
||||
// close the server at 3-6 seconds
|
||||
go func() {
|
||||
time.Sleep(3 * time.Second)
|
||||
ctx, cancel := stdContext.WithTimeout(stdContext.TODO(), 3*time.Second)
|
||||
defer cancel()
|
||||
app.Shutdown(ctx)
|
||||
}()
|
||||
|
||||
// we disable the ErrServerClosed, so the error should be nil when server is closed by `app.Shutdown`
|
||||
// or by an external issue.
|
||||
err := app.Listen(":9827", iris.WithoutServerError(iris.ErrServerClosed))
|
||||
if err != nil {
|
||||
t.Fatalf("expecting err to be nil but got: %v", err)
|
||||
}
|
||||
|
||||
if got := log.String(); got != "" {
|
||||
t.Fatalf("expecting to log nothing but logged: '%s'", got)
|
||||
}
|
||||
}
|
||||
32
_examples/http-server/listen-letsencrypt/main.go
Normal file
32
_examples/http-server/listen-letsencrypt/main.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// Package main provide one-line integration with letsencrypt.org
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from SECURE SERVER!")
|
||||
})
|
||||
|
||||
app.Get("/test2", func(ctx iris.Context) {
|
||||
ctx.Writef("Welcome to secure server from /test2!")
|
||||
})
|
||||
|
||||
app.Get("/redirect", func(ctx iris.Context) {
|
||||
ctx.Redirect("/test2")
|
||||
})
|
||||
|
||||
// NOTE: This will not work on domains like this,
|
||||
// use real whitelisted domain(or domains split by whitespaces)
|
||||
// and a non-public e-mail instead or edit your hosts file.
|
||||
app.Run(iris.AutoTLS(":443", "example.com", "mail@example.com"))
|
||||
|
||||
// Note: to disable automatic "http://" to "https://" redirections pass the `iris.TLSNoRedirect`
|
||||
// host configurator to TLS or AutoTLS functions, e.g:
|
||||
//
|
||||
// app.Run(iris.AutoTLS(":443", "example.com", "mail@example.com", iris.TLSNoRedirect))
|
||||
}
|
||||
27
_examples/http-server/listen-tls/main.go
Normal file
27
_examples/http-server/listen-tls/main.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the SECURE server")
|
||||
})
|
||||
|
||||
app.Get("/mypath", func(ctx iris.Context) {
|
||||
ctx.Writef("Hello from the SECURE server on path /mypath")
|
||||
})
|
||||
|
||||
// Start the server (HTTPS) on port 443,
|
||||
// and a secondary of (HTTP) on port :80 which redirects requests to their HTTPS version.
|
||||
// This is a blocking func.
|
||||
app.Run(iris.TLS("127.0.0.1:443", "mycert.cert", "mykey.key"))
|
||||
|
||||
// Note: to disable automatic "http://" to "https://" redirections pass the `iris.TLSNoRedirect`
|
||||
// host configurator to TLS or AutoTLS functions, e.g:
|
||||
//
|
||||
// app.Run(iris.TLS("127.0.0.1:443", "mycert.cert", "mykey.key", iris.TLSNoRedirect))
|
||||
}
|
||||
19
_examples/http-server/listen-tls/mycert.cert
Normal file
19
_examples/http-server/listen-tls/mycert.cert
Normal file
@@ -0,0 +1,19 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDAzCCAeugAwIBAgIJAPDsxtKV4v3uMA0GCSqGSIb3DQEBBQUAMBgxFjAUBgNV
|
||||
BAMMDTEyNy4wLjAuMTo0NDMwHhcNMTYwNjI5MTMxMjU4WhcNMjYwNjI3MTMxMjU4
|
||||
WjAYMRYwFAYDVQQDDA0xMjcuMC4wLjE6NDQzMIIBIjANBgkqhkiG9w0BAQEFAAOC
|
||||
AQ8AMIIBCgKCAQEA0KtAOHKrcbLwWJXgRX7XSFyu4HHHpSty4bliv8ET4sLJpbZH
|
||||
XeVX05Foex7PnrurDP6e+0H5TgqqcpQM17/ZlFcyKrJcHSCgV0ZDB3Sb8RLQSLns
|
||||
8a+MOSbn1WZ7TkC7d/cWlKmasQRHQ2V/cWlGooyKNEPoGaEz8MbY0wn2spyIJwsB
|
||||
dciERC6317VTXbiZdoD8QbAsT+tBvEHM2m2A7B7PQmHNehtyFNbSV5uZNodvv1uv
|
||||
ZTnDa6IqpjFLb1b2HNFgwmaVPmmkLuy1l9PN+o6/DUnXKKBrfPAx4JOlqTKEQpWs
|
||||
pnfacTE3sWkkmOSSFltAXfkXIJFKdS/hy5J/KQIDAQABo1AwTjAdBgNVHQ4EFgQU
|
||||
zr1df/c9+NyTpmyiQO8g3a8NswYwHwYDVR0jBBgwFoAUzr1df/c9+NyTpmyiQO8g
|
||||
3a8NswYwDAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOCAQEACG5shtMSDgCd
|
||||
MNjOF+YmD+PX3Wy9J9zehgaDJ1K1oDvBbQFl7EOJl8lRMWITSws22Wxwh8UXVibL
|
||||
sscKBp14dR3e7DdbwCVIX/JyrJyOaCfy2nNBdf1B06jYFsIHvP3vtBAb9bPNOTBQ
|
||||
QE0Ztu9kCqgsmu0//sHuBEeA3d3E7wvDhlqRSxTLcLtgC1NSgkFvBw0JvwgpkX6s
|
||||
M5WpSBZwZv8qpplxhFfqNy8Uf+xrpSW0pGfkHumehkQGC6/Ry7raganS0aHhDPK9
|
||||
Z1bEJ2com1bFFAQsm9yIXrRVMGGCtihB2Au0Q4jpEjUbzWYM+ItZyvRAGRM6Qex6
|
||||
s/jogMeRsw==
|
||||
-----END CERTIFICATE-----
|
||||
27
_examples/http-server/listen-tls/mykey.key
Normal file
27
_examples/http-server/listen-tls/mykey.key
Normal file
@@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpQIBAAKCAQEA0KtAOHKrcbLwWJXgRX7XSFyu4HHHpSty4bliv8ET4sLJpbZH
|
||||
XeVX05Foex7PnrurDP6e+0H5TgqqcpQM17/ZlFcyKrJcHSCgV0ZDB3Sb8RLQSLns
|
||||
8a+MOSbn1WZ7TkC7d/cWlKmasQRHQ2V/cWlGooyKNEPoGaEz8MbY0wn2spyIJwsB
|
||||
dciERC6317VTXbiZdoD8QbAsT+tBvEHM2m2A7B7PQmHNehtyFNbSV5uZNodvv1uv
|
||||
ZTnDa6IqpjFLb1b2HNFgwmaVPmmkLuy1l9PN+o6/DUnXKKBrfPAx4JOlqTKEQpWs
|
||||
pnfacTE3sWkkmOSSFltAXfkXIJFKdS/hy5J/KQIDAQABAoIBAQDCd+bo9I0s8Fun
|
||||
4z3Y5oYSDTZ5O/CY0O5GyXPrSzCSM4Cj7EWEj1mTdb9Ohv9tam7WNHHLrcd+4NfK
|
||||
4ok5hLVs1vqM6h6IksB7taKATz+Jo0PzkzrsXvMqzERhEBo4aoGMIv2rXIkrEdas
|
||||
S+pCsp8+nAWtAeBMCn0Slu65d16vQxwgfod6YZfvMKbvfhOIOShl9ejQ+JxVZcMw
|
||||
Ti8sgvYmFUrdrEH3nCgptARwbx4QwlHGaw/cLGHdepfFsVaNQsEzc7m61fSO70m4
|
||||
NYJv48ZgjOooF5AccbEcQW9IxxikwNc+wpFYy5vDGzrBwS5zLZQFpoyMWFhtWdjx
|
||||
hbmNn1jlAoGBAPs0ZjqsfDrH5ja4dQIdu5ErOccsmoHfMMBftMRqNG5neQXEmoLc
|
||||
Uz8WeQ/QDf302aTua6E9iSjd7gglbFukVwMndQ1Q8Rwxz10jkXfiE32lFnqK0csx
|
||||
ltruU6hOeSGSJhtGWBuNrT93G2lmy23fSG6BqOzdU4rn/2GPXy5zaxM/AoGBANSm
|
||||
/E96RcBUiI6rDVqKhY+7M1yjLB41JrErL9a0Qfa6kYnaXMr84pOqVN11IjhNNTgl
|
||||
g1lwxlpXZcZh7rYu9b7EEMdiWrJDQV7OxLDHopqUWkQ+3MHwqs6CxchyCq7kv9Df
|
||||
IKqat7Me6Cyeo0MqcW+UMxlCRBxKQ9jqC7hDfZuXAoGBAJmyS8ImerP0TtS4M08i
|
||||
JfsCOY21qqs/hbKOXCm42W+be56d1fOvHngBJf0YzRbO0sNo5Q14ew04DEWLsCq5
|
||||
+EsDv0hwd7VKfJd+BakV99ruQTyk5wutwaEeJK1bph12MD6L4aiqHJAyLeFldZ45
|
||||
+TUzu8mA+XaJz+U/NXtUPvU9AoGBALtl9M+tdy6I0Fa50ujJTe5eEGNAwK5WNKTI
|
||||
5D2XWNIvk/Yh4shXlux+nI8UnHV1RMMX++qkAYi3oE71GsKeG55jdk3fFQInVsJQ
|
||||
APGw3FDRD8M4ip62ki+u+tEr/tIlcAyHtWfjNKO7RuubWVDlZFXqCiXmSdOMdsH/
|
||||
bxiREW49AoGACWev/eOzBoQJCRN6EvU2OV0s3b6f1QsPvcaH0zc6bgbBFOGmJU8v
|
||||
pXhD88tsu9exptLkGVoYZjR0n0QT/2Kkyu93jVDW/80P7VCz8DKYyAJDa4CVwZxO
|
||||
MlobQSunSDKx/CCJhWkbytCyh1bngAtwSAYLXavYIlJbAzx6FvtAIw4=
|
||||
-----END RSA PRIVATE KEY-----
|
||||
19
_examples/http-server/listen-unix/main.go
Normal file
19
_examples/http-server/listen-unix/main.go
Normal file
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/kataras/iris/v12"
|
||||
"github.com/kataras/iris/v12/core/netutil"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
l, err := netutil.UNIX("/tmpl/srv.sock", 0666) // see its code to see how you can manually create a new file listener, it's easy.
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
app.Run(iris.Listener(l))
|
||||
}
|
||||
|
||||
// Look "custom-listener/unix-reuseport" too.
|
||||
59
_examples/http-server/notify-on-shutdown/main.go
Normal file
59
_examples/http-server/notify-on-shutdown/main.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/kataras/iris/v12"
|
||||
)
|
||||
|
||||
func main() {
|
||||
app := iris.New()
|
||||
|
||||
app.Get("/", func(ctx iris.Context) {
|
||||
ctx.HTML("<h1>Hello, try to refresh the page after ~5 secs</h1>")
|
||||
})
|
||||
|
||||
app.Logger().Info("Wait 5 seconds and check your terminal again")
|
||||
// simulate a shutdown action here...
|
||||
go func() {
|
||||
<-time.After(5 * time.Second)
|
||||
timeout := 10 * time.Second
|
||||
ctx, cancel := context.WithTimeout(context.Background(), timeout)
|
||||
defer cancel()
|
||||
// close all hosts, this will notify the callback we had register
|
||||
// inside the `configureHost` func.
|
||||
app.Shutdown(ctx)
|
||||
}()
|
||||
|
||||
// app.ConfigureHost(configureHost) -> or pass "configureHost" as `app.Addr` argument, same result.
|
||||
|
||||
// start the server as usual, the only difference is that
|
||||
// we're adding a second (optional) function
|
||||
// to configure the just-created host supervisor.
|
||||
//
|
||||
// http://localhost:8080
|
||||
// wait 10 seconds and check your terminal.
|
||||
app.Run(iris.Addr(":8080", configureHost), iris.WithoutServerError(iris.ErrServerClosed))
|
||||
|
||||
time.Sleep(500 * time.Millisecond) // give time to the separate go routine(`onServerShutdown`) to finish.
|
||||
|
||||
/* See
|
||||
iris.RegisterOnInterrupt(callback) for global catch of the CTRL/CMD+C and OS events.
|
||||
Look at the "graceful-shutdown" example for more.
|
||||
*/
|
||||
}
|
||||
|
||||
func onServerShutdown() {
|
||||
println("server is closed")
|
||||
}
|
||||
|
||||
func configureHost(su *iris.Supervisor) {
|
||||
// here we have full access to the host that will be created
|
||||
// inside the `app.Run` function or `NewHost`.
|
||||
//
|
||||
// we're registering a shutdown "event" callback here:
|
||||
su.RegisterOnShutdown(onServerShutdown)
|
||||
// su.RegisterOnError
|
||||
// su.RegisterOnServe
|
||||
}
|
||||
Reference in New Issue
Block a user