1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-29 15:57:09 +00:00

init go modules but keep the dep files and vendor folder and update badger sessionsdb's dependency to 1.5.4 from 1.5.3

Former-commit-id: 4f58a9bd0e7eb5f9535953125c27b9c5316ffc0f
This commit is contained in:
Gerasimos (Makis) Maropoulos
2018-10-02 04:05:39 +03:00
parent 2a1f3d4e43
commit 97e96ed6ca
9 changed files with 267 additions and 47 deletions

View File

@@ -1,2 +0,0 @@
A silly example for this issue: https://github.com/kataras/iris/issues/688#issuecomment-318828259.
However it seems useful and therefore is being included in the examples for everyone else.

View File

@@ -1,30 +0,0 @@
package counter
import (
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/core/host"
)
func Configurator(app *iris.Application) {
counterValue := 0
go func() {
ticker := time.NewTicker(time.Second)
for range ticker.C {
counterValue++
}
app.ConfigureHost(func(h *host.Supervisor) { // <- HERE: IMPORTANT
h.RegisterOnShutdown(func() {
ticker.Stop()
})
}) // or put the ticker outside of the gofunc and put the configurator before or after the app.Get, outside of this gofunc
}()
app.Get("/counter", func(ctx iris.Context) {
ctx.Writef("Counter value = %d", counterValue)
})
}

View File

@@ -1,14 +1,28 @@
package main
import (
"github.com/kataras/iris/_examples/http-listening/iris-configurator-and-host-configurator/counter"
"github.com/kataras/iris"
)
func main() {
app := iris.New()
app.Configure(counter.Configurator)
app.Run(iris.Addr(":8080"))
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.Run(iris.Addr(":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.
*/
}

View File

@@ -1,11 +1,10 @@
package main
import (
stdContext "context"
"context"
"time"
"github.com/kataras/iris"
"github.com/kataras/iris/core/host"
)
func main() {
@@ -15,20 +14,20 @@ func main() {
ctx.HTML("<h1>Hello, try to refresh the page after ~10 secs</h1>")
})
// app.ConfigureHost(configureHost) -> or pass "configureHost" as `app.Addr` argument, same result.
app.Logger().Info("Wait 10 seconds and check your terminal again")
// simulate a shutdown action here...
go func() {
<-time.After(10 * time.Second)
timeout := 5 * time.Second
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), timeout)
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.
@@ -37,9 +36,14 @@ func main() {
// wait 10 seconds and check your terminal.
app.Run(iris.Addr(":8080", configureHost), iris.WithoutServerError(iris.ErrServerClosed))
/*
Or for simple cases you can just use the:
iris.RegisterOnInterrupt for global catch of the CTRL/CMD+C and OS events.
Look at the "graceful-shutdown" example for more.
*/
}
func configureHost(su *host.Supervisor) {
func configureHost(su *iris.Supervisor) {
// here we have full access to the host that will be created
// inside the `app.Run` function or `NewHost`.
//