Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions cookbook/google-app-engine/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"net/http"
"sync"

"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
Expand All @@ -12,14 +13,18 @@ type (
ID string `json:"id"`
Name string `json:"name"`
}
usersMap struct {
sync.RWMutex
users map[string]user
}
)

var (
users map[string]user
usersStore usersMap
)

func init() {
users = map[string]user{
usersStore.users = map[string]user{
"1": user{
ID: "1",
Name: "Wreck-It Ralph",
Expand All @@ -41,14 +46,20 @@ func createUser(c echo.Context) error {
if err := c.Bind(u); err != nil {
return err
}
users[u.ID] = *u
usersStore.Lock()
defer usersStore.Unlock()
usersStore.users[u.ID] = *u
return c.JSON(http.StatusCreated, u)
}

func getUsers(c echo.Context) error {
return c.JSON(http.StatusOK, users)
usersStore.RLock()
defer usersStore.RUnlock()
return c.JSON(http.StatusOK, usersStore.users)
}

func getUser(c echo.Context) error {
return c.JSON(http.StatusOK, users[c.Param("id")])
usersStore.RLock()
defer usersStore.RUnlock()
return c.JSON(http.StatusOK, usersStore.users[c.Param("id")])
}