-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
135 lines (123 loc) · 3.12 KB
/
main.go
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package main
import (
"fmt"
"net/http"
"os"
"github.com/gorilla/websocket"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/spf13/cobra"
"github.com/vagmi/hellopb/exporter"
"github.com/vagmi/hellopb/invitations"
_ "github.com/vagmi/hellopb/migrations"
"go.uber.org/zap"
)
var logger *zap.SugaredLogger
func init() {
l, err := zap.NewProduction()
if err != nil {
panic(err)
}
logger = l.Sugar()
}
func main() {
app := pocketbase.New()
addCommands(app)
setupHooks(app)
addEndpoints(app)
if err := app.Start(); err != nil {
logger.Fatal(err)
}
}
func addEndpoints(app core.App) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/api/hello",
Handler: func(c echo.Context) error {
return c.String(200, "Hello world!")
},
Middlewares: []echo.MiddlewareFunc{
apis.RequireAdminOrUserAuth(),
},
})
e.Router.AddRoute(echo.Route{
Method: http.MethodGet,
Path: "/api/websocket",
Handler: func(c echo.Context) error {
upgrader := websocket.Upgrader{}
ws, err := upgrader.Upgrade(c.Response(), c.Request(), nil)
if err != nil {
return err
}
msg := []byte("hello client")
defer ws.Close()
for {
err := ws.WriteMessage(websocket.TextMessage, []byte(fmt.Sprintf("Server %s", string(msg))))
if err != nil {
logger.Error(err)
}
// Read
_, msg, err = ws.ReadMessage()
if err != nil {
logger.Errorf("The client probably closed %v", err)
break
}
logger.Infof("From client %s\n", msg)
}
return nil
},
Middlewares: []echo.MiddlewareFunc{
apis.RequireAdminOrUserAuth(),
},
})
return nil
})
}
func setupHooks(app core.App) {
app.OnRecordAfterCreateRequest().Add(func(e *core.RecordCreateEvent) error {
if e.Record.Collection().Name == "invitations" {
err := invitations.SendInvitation(app, e.Record)
if err != nil {
return err
}
}
return nil
})
}
func addCommands(app *pocketbase.PocketBase) {
var filename string
exportCmd := &cobra.Command{
Use: "export",
Short: "Exports the collection to JSON on STDOUT",
ValidArgs: []string{"filename"},
Run: func(cmd *cobra.Command, args []string) {
collectionFile, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0755)
if err != nil {
logger.Fatal(err)
}
defer collectionFile.Close()
err = exporter.ExportCollections(app, collectionFile)
if err != nil {
panic(err)
}
},
}
exportCmd.Flags().StringVarP(&filename, "output", "o", "collections.json", "Export to file")
importCmd := &cobra.Command{
Use: "import",
Short: "Imports the collection from JSON",
ValidArgs: []string{"filename"},
Run: func(cmd *cobra.Command, args []string) {
err := exporter.ImportCollections(app, filename)
if err != nil {
panic(err)
}
},
}
importCmd.Flags().StringVarP(&filename, "from", "f", "collections.json", "Import from file")
app.RootCmd.AddCommand(exportCmd)
app.RootCmd.AddCommand(importCmd)
}