-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
72 lines (62 loc) · 1.58 KB
/
Copy pathapp.go
File metadata and controls
72 lines (62 loc) · 1.58 KB
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
package main
import (
"context"
"fmt"
"net/http"
"time"
"path/filepath"
"code-mind/internal/collab"
"code-mind/internal/server"
"code-mind/internal/store"
)
const desktopAPIAddress = "127.0.0.1:34117"
type App struct {
ctx context.Context
apiServer *http.Server
collab *collab.Server
store *store.FileStore
startError error
}
func NewApp(fileStore *store.FileStore) *App {
return &App{
store: fileStore,
}
}
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
// settingsDir is the parent of the maps directory (i.e. "data/")
settingsDir := filepath.Dir(a.store.Dir())
tokenStore, err := store.NewTokenStore(filepath.Join(settingsDir, "tokens.json"))
if err != nil {
a.startError = err
fmt.Println("token store error:", err)
}
apiHandler := server.NewWithTokenStore(a.store, settingsDir, tokenStore).Handler()
a.apiServer = &http.Server{
Addr: desktopAPIAddress,
Handler: apiHandler,
ReadHeaderTimeout: 10 * time.Second,
}
a.collab = collab.NewServer(collab.DefaultAddress, tokenStore)
go func() {
err := a.apiServer.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
a.startError = err
fmt.Println("desktop api server error:", err)
}
}()
go func() {
err := a.collab.ListenAndServe()
if err != nil && err != http.ErrServerClosed {
fmt.Println("collaboration server disabled:", err)
}
}()
}
func (a *App) shutdown(_ context.Context) {
if a.collab != nil {
_ = a.collab.Shutdown(context.Background())
}
if a.apiServer != nil {
_ = a.apiServer.Shutdown(context.Background())
}
}