package main import log github com gin-gonic gin type Fetcher interfac

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"log"
"github.com/gin-gonic/gin"
)
type Fetcher interface {
GetUser(userID uint64) (models.User, error)
FindByEmail(email string) (*User, error)
FindByChangePasswordHash(hash string) (*User, error)
FindByValidationHash(hash string) (*User, error)
}
type Writer interface {
Update(user *User) error
Store(user *User) (entity.ID, error)
AddCompany(id entity.ID, company *Company) error
AddInvite(userID entity.ID, companyID entity.ID) error
}
type Repository interface {
Reader
Writer
}
type StateProvider interface {
getState() (*State, error)
subscribeToComments() (<-chan Comments, error)
run() error // lauchs gorotine and collect data in background
... etc
}
type Controller interface {
newComments() (<-chan Comments, error)
NewUser(username string) error
... etc
}
type repo struct {
*mongo Repository
*provider StateProvider
*logger logger.Logger
}
func main() {
controller := NewController(mongo, stateProvider, logger)
r := gin.Default()
r.WS("/new_comments", func(c *gin.Context) {
commentsChan := controller.newComments()
c.SSEvent <- commentsChan
})
r.POST("/users",
func(c *gin.Context) {
username := c.PostForm("username")
err := controller.NewUser(User{
Username: username,
})
if err != nil {
c.JSON(200, gin.H{
"status": "created",
"message": "ok",
"username": username,
})
} else {
c.JSON(403, gin.H{
"status": "not created",
"message": "not ok",
"username": username,
})
}
}
)
log.Fatal(r.Run())
}