2017-12-20 16:56:28 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync/atomic"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
2017-12-20 16:56:28 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2017-12-27 03:15:41 +01:00
|
|
|
mvc.New(app.Party("/")).Handle(&globalVisitorsController{visits: 0})
|
2017-12-20 16:56:28 +01:00
|
|
|
|
|
|
|
// http://localhost:8080
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-12-20 16:56:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type globalVisitorsController struct {
|
|
|
|
// When a singleton controller is used then concurent safe access is up to the developers, because
|
2017-12-20 17:09:31 +01:00
|
|
|
// all clients share the same controller instance instead.
|
|
|
|
// Note that any controller's methods
|
|
|
|
// are per-client, but the struct's field can be shared across multiple clients if the structure
|
2017-12-20 17:10:57 +01:00
|
|
|
// does not have any dynamic struct field dependencies that depend on the iris.Context
|
2017-12-20 17:09:31 +01:00
|
|
|
// and ALL field's values are NOT zero, at this case we use uint64 which it's no zero (even if we didn't set it
|
|
|
|
// manually ease-of-understand reasons) because it's a value of &{0}.
|
|
|
|
// All the above declares a Singleton, note that you don't have to write a single line of code to do this, Iris is smart enough.
|
2017-12-20 16:56:28 +01:00
|
|
|
//
|
|
|
|
// see `Get`.
|
|
|
|
visits uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *globalVisitorsController) Get() string {
|
|
|
|
count := atomic.AddUint64(&c.visits, 1)
|
|
|
|
return fmt.Sprintf("Total visitors: %d", count)
|
|
|
|
}
|