2017-12-14 04:56:23 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-07-15 17:45:22 +02:00
|
|
|
"strings"
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12/_examples/tutorial/vuejs-todo-mvc/src/todo"
|
|
|
|
"github.com/kataras/iris/v12/_examples/tutorial/vuejs-todo-mvc/src/web/controllers"
|
2017-12-14 04:56:23 +01:00
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
|
|
|
"github.com/kataras/iris/v12/mvc"
|
|
|
|
"github.com/kataras/iris/v12/sessions"
|
|
|
|
"github.com/kataras/iris/v12/websocket"
|
2017-12-14 04:56:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
2017-12-23 16:07:39 +01:00
|
|
|
|
2017-12-14 04:56:23 +01:00
|
|
|
// serve our app in public, public folder
|
|
|
|
// contains the client-side vue.js application,
|
|
|
|
// no need for any server-side template here,
|
|
|
|
// actually if you're going to just use vue without any
|
|
|
|
// back-end services, you can just stop afer this line and start the server.
|
2019-06-21 18:43:25 +02:00
|
|
|
app.HandleDir("/", "./public")
|
2017-12-14 04:56:23 +01:00
|
|
|
|
2017-12-24 00:22:44 +01:00
|
|
|
// configure the http sessions.
|
2017-12-14 04:56:23 +01:00
|
|
|
sess := sessions.New(sessions.Config{
|
2017-12-23 16:07:39 +01:00
|
|
|
Cookie: "iris_session",
|
2017-12-14 04:56:23 +01:00
|
|
|
})
|
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
// create a sub router and register the http controllers.
|
2017-12-24 00:22:44 +01:00
|
|
|
todosRouter := app.Party("/todos")
|
|
|
|
|
2017-12-23 16:07:39 +01:00
|
|
|
// create our mvc application targeted to /todos relative sub path.
|
2017-12-24 00:22:44 +01:00
|
|
|
todosApp := mvc.New(todosRouter)
|
|
|
|
|
2017-12-16 05:38:28 +01:00
|
|
|
// any dependencies bindings here...
|
2017-12-27 03:15:41 +01:00
|
|
|
todosApp.Register(
|
2017-12-23 16:07:39 +01:00
|
|
|
todo.NewMemoryService(),
|
2017-12-25 19:57:04 +01:00
|
|
|
sess.Start,
|
2017-12-16 05:38:28 +01:00
|
|
|
)
|
2017-12-14 04:56:23 +01:00
|
|
|
|
2019-07-15 17:45:22 +02:00
|
|
|
todosController := new(controllers.TodoController)
|
2017-12-14 04:56:23 +01:00
|
|
|
// controllers registration here...
|
2019-07-15 17:45:22 +02:00
|
|
|
todosApp.Handle(todosController)
|
|
|
|
|
|
|
|
// Create a sub mvc app for websocket controller.
|
|
|
|
// Inherit the parent's dependencies.
|
|
|
|
todosWebsocketApp := todosApp.Party("/sync")
|
|
|
|
todosWebsocketApp.HandleWebsocket(todosController).
|
|
|
|
SetNamespace("todos").
|
|
|
|
SetEventMatcher(func(methodName string) (string, bool) {
|
|
|
|
return strings.ToLower(methodName), true
|
|
|
|
})
|
|
|
|
|
|
|
|
websocketServer := websocket.New(websocket.DefaultGorillaUpgrader, todosWebsocketApp)
|
|
|
|
idGenerator := func(ctx iris.Context) string {
|
|
|
|
id := sess.Start(ctx).ID()
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
todosWebsocketApp.Router.Get("/", websocket.Handler(websocketServer, idGenerator))
|
2017-12-14 04:56:23 +01:00
|
|
|
|
|
|
|
// start the web server at http://localhost:8080
|
2018-10-21 18:20:05 +02:00
|
|
|
app.Run(iris.Addr(":8080"))
|
2017-12-14 04:56:23 +01:00
|
|
|
}
|