mirror of
https://github.com/kataras/iris.git
synced 2025-03-14 08:16:28 +01:00
add locks on the MemoryService of the vue mvc example, keep it simple but runnable:P
Former-commit-id: 5d661f401e486a26d66f5c0c297f5cf897fc1834
This commit is contained in:
parent
d31b8c5274
commit
9c79dcc97a
|
@ -1,20 +1,33 @@
|
||||||
package todo
|
package todo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Service interface {
|
type Service interface {
|
||||||
Get(owner string) []Item
|
Get(owner string) []Item
|
||||||
Save(owner string, newItems []Item) error
|
Save(owner string, newItems []Item) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type MemoryService struct {
|
type MemoryService struct {
|
||||||
|
// key = session id, value the list of todo items that this session id has.
|
||||||
items map[string][]Item
|
items map[string][]Item
|
||||||
|
// protected by locker for concurrent access.
|
||||||
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMemoryService() *MemoryService {
|
func NewMemoryService() *MemoryService {
|
||||||
return &MemoryService{make(map[string][]Item, 0)}
|
return &MemoryService{
|
||||||
|
items: make(map[string][]Item, 0),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MemoryService) Get(sessionOwner string) (items []Item) {
|
func (s *MemoryService) Get(sessionOwner string) []Item {
|
||||||
return s.items[sessionOwner]
|
s.mu.RLock()
|
||||||
|
items := s.items[sessionOwner]
|
||||||
|
s.mu.RUnlock()
|
||||||
|
|
||||||
|
return items
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *MemoryService) Save(sessionOwner string, newItems []Item) error {
|
func (s *MemoryService) Save(sessionOwner string, newItems []Item) error {
|
||||||
|
@ -26,6 +39,8 @@ func (s *MemoryService) Save(sessionOwner string, newItems []Item) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s.mu.Lock()
|
||||||
s.items[sessionOwner] = newItems
|
s.items[sessionOwner] = newItems
|
||||||
|
s.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ func (c *TodoController) Post(newItems []todo.Item) PostItemResponse {
|
||||||
func (c *TodoController) GetSync(conn websocket.Connection) {
|
func (c *TodoController) GetSync(conn websocket.Connection) {
|
||||||
conn.Join(c.Session.ID())
|
conn.Join(c.Session.ID())
|
||||||
conn.On("save", func() { // "save" event from client.
|
conn.On("save", func() { // "save" event from client.
|
||||||
conn.To(c.Session.ID()).Emit("saved", nil) // fire a "save" event to the rest of the clients.
|
conn.To(c.Session.ID()).Emit("saved", nil) // fire a "saved" event to the rest of the clients w.
|
||||||
})
|
})
|
||||||
|
|
||||||
conn.Wait()
|
conn.Wait()
|
||||||
|
|
|
@ -21,28 +21,34 @@ func main() {
|
||||||
// back-end services, you can just stop afer this line and start the server.
|
// back-end services, you can just stop afer this line and start the server.
|
||||||
app.StaticWeb("/", "./public")
|
app.StaticWeb("/", "./public")
|
||||||
|
|
||||||
|
// configure the http sessions.
|
||||||
sess := sessions.New(sessions.Config{
|
sess := sessions.New(sessions.Config{
|
||||||
Cookie: "iris_session",
|
Cookie: "iris_session",
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// configure the websocket server.
|
||||||
ws := websocket.New(websocket.Config{})
|
ws := websocket.New(websocket.Config{})
|
||||||
|
|
||||||
|
// create a sub router and register the client-side library for the iris websockets,
|
||||||
|
// you could skip it but iris websockets supports socket.io-like API.
|
||||||
|
todosRouter := app.Party("/todos")
|
||||||
|
// http://localhost:8080/todos/iris-ws.js
|
||||||
|
// serve the javascript client library to communicate with
|
||||||
|
// the iris high level websocket event system.
|
||||||
|
todosRouter.Any("/iris-ws.js", websocket.ClientHandler())
|
||||||
|
|
||||||
// create our mvc application targeted to /todos relative sub path.
|
// create our mvc application targeted to /todos relative sub path.
|
||||||
m := mvc.New(app.Party("/todos"))
|
todosApp := mvc.New(todosRouter)
|
||||||
|
|
||||||
// any dependencies bindings here...
|
// any dependencies bindings here...
|
||||||
m.AddDependencies(
|
todosApp.AddDependencies(
|
||||||
todo.NewMemoryService(),
|
todo.NewMemoryService(),
|
||||||
mvc.Session(sess),
|
mvc.Session(sess),
|
||||||
ws.Upgrade,
|
ws.Upgrade,
|
||||||
)
|
)
|
||||||
|
|
||||||
// http://localhost:8080/iris-ws.js
|
|
||||||
// serve the javascript client library to communicate with
|
|
||||||
// the iris high level websocket event system.
|
|
||||||
m.Router.Any("/iris-ws.js", websocket.ClientHandler())
|
|
||||||
|
|
||||||
// controllers registration here...
|
// controllers registration here...
|
||||||
m.Register(new(controllers.TodoController))
|
todosApp.Register(new(controllers.TodoController))
|
||||||
|
|
||||||
// start the web server at http://localhost:8080
|
// start the web server at http://localhost:8080
|
||||||
app.Run(iris.Addr(":8080"), iris.WithoutVersionChecker)
|
app.Run(iris.Addr(":8080"), iris.WithoutVersionChecker)
|
||||||
|
|
|
@ -13,7 +13,7 @@ socket.On("saved", function () {
|
||||||
|
|
||||||
function fetchTodos(onComplete) {
|
function fetchTodos(onComplete) {
|
||||||
axios.get("/todos").then(response => {
|
axios.get("/todos").then(response => {
|
||||||
if (response.data == null) {
|
if (response.data === null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,6 @@ var app = new Vue({
|
||||||
// app initial state
|
// app initial state
|
||||||
data: {
|
data: {
|
||||||
todos: todoStorage.fetch(),
|
todos: todoStorage.fetch(),
|
||||||
hasChanges: false,
|
|
||||||
newTodo: '',
|
newTodo: '',
|
||||||
editedTodo: null,
|
editedTodo: null,
|
||||||
visibility: 'all'
|
visibility: 'all'
|
||||||
|
|
Loading…
Reference in New Issue
Block a user