This repository has been archived by the owner on Feb 13, 2024. It is now read-only.
/
app.go
176 lines (147 loc) · 4.7 KB
/
app.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package app
import (
"context"
"errors"
"github.com/dchest/uniuri"
"github.com/owncloud/ocis/v2/ocis-pkg/log"
"github.com/wkloucek/cs3-wopi-server/pkg/internal/logging"
registryv1beta1 "github.com/cs3org/go-cs3apis/cs3/app/registry/v1beta1"
gatewayv1beta1 "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
rpcv1beta1 "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
"github.com/cs3org/reva/v2/pkg/mime"
"github.com/cs3org/reva/v2/pkg/rgrpc/todo/pool"
"github.com/gofrs/uuid"
"github.com/owncloud/ocis/v2/ocis-pkg/config/envdecode"
"github.com/owncloud/ocis/v2/ocis-pkg/registry"
"google.golang.org/grpc"
)
type Service struct {
Namespace string
Name string `env:"WOPI_SERVICE_NAME"`
}
func (s Service) GetServiceFQDN() string {
return s.Namespace + "." + s.Name
}
type GRPC struct {
BindAddr string `env:"WOPI_GRPC_BIND_ADDR"`
}
type HTTP struct {
Addr string `env:"WOPI_HTTP_ADDR"`
BindAddr string `env:"WOPI_HTTP_BIND_ADDR"`
Scheme string `env:"WOPI_HTTP_SCHEME"`
}
type WopiApp struct {
Addr string `env:"WOPI_APP_ADDR"`
Insecure bool `env:"WOPI_APP_INSECURE"`
}
type CS3api struct {
GatewayServiceName string `env:"WOPI_CS3API_GATEWAY_SERVICENAME"`
CS3DataGatewayInsecure bool `env:"WOPI_CS3API_DATA_GATEWAY_INSECURE"`
}
type Config struct {
Service
GRPC
HTTP
WopiApp
CS3api
WopiSecret string `env:"WOPI_SECRET"` // used as jwt secret and to encrypt access tokens
AppName string `env:"WOPI_APP_NAME"`
AppDescription string `env:"WOPI_APP_DESCRIPTION"`
AppIcon string `env:"WOPI_APP_ICON"`
AppLockName string `env:"WOPI_APP_LOCK_NAME"`
}
type demoApp struct {
gwc gatewayv1beta1.GatewayAPIClient
grpcServer *grpc.Server
appURLs map[string]map[string]string
Config Config
Logger log.Logger
}
func New() (*demoApp, error) {
app := &demoApp{
Config: Config{
AppName: "WOPI app",
AppDescription: "Open office documents with a WOPI app",
AppIcon: "image-edit",
AppLockName: "com.github.wkloucek.cs3-wopi-server",
WopiSecret: uniuri.NewLen(32),
CS3api: CS3api{
GatewayServiceName: "com.owncloud.api.gateway",
CS3DataGatewayInsecure: true, // TODO: this should have a secure default
},
Service: Service{
Namespace: "com.github.wkloucek.cs3-wopi-server",
},
GRPC: GRPC{
BindAddr: "127.0.0.1:5678",
},
HTTP: HTTP{
Addr: "127.0.0.1:6789",
BindAddr: "127.0.0.1:6789",
Scheme: "http",
},
WopiApp: WopiApp{
Addr: "https://127.0.0.1:8080",
Insecure: true, // TODO: this should have a secure default
},
},
}
err := envdecode.Decode(app)
if err != nil {
if !errors.Is(err, envdecode.ErrNoTargetFieldsAreSet) {
return nil, err
}
}
app.Logger = logging.Configure("wopiserver")
return app, nil
}
func (app *demoApp) GetCS3apiClient() error {
// establish a connection to the cs3 api endpoint
// in this case a REVA gateway, started by oCIS
gwc, err := pool.GetGatewayServiceClient(app.Config.CS3api.GatewayServiceName)
if err != nil {
return err
}
app.gwc = gwc
return nil
}
func (app *demoApp) RegisterOcisService(ctx context.Context) error {
svc := registry.BuildGRPCService(app.Config.Service.GetServiceFQDN(), uuid.Must(uuid.NewV4()).String(), app.Config.GRPC.BindAddr, "0.0.0")
return registry.RegisterService(ctx, svc, app.Logger)
}
func (app *demoApp) RegisterDemoApp(ctx context.Context) error {
mimeTypesMap := make(map[string]bool)
for _, extensions := range app.appURLs {
for ext := range extensions {
m := mime.Detect(false, ext)
mimeTypesMap[m] = true
}
}
mimeTypes := make([]string, 0, len(mimeTypesMap))
for m := range mimeTypesMap {
mimeTypes = append(mimeTypes, m)
}
// TODO: REVA has way to filter supported mimetypes (do we need to implement it here or is it in the registry?)
// TODO: an added app provider shouldn't last forever. Instead the registry should use a TTL
// and delete providers that didn't register again. If an app provider dies or get's disconnected,
// the users will be no longer available to choose to open a file with it (currently, opening a file just fails)
req := ®istryv1beta1.AddAppProviderRequest{
Provider: ®istryv1beta1.ProviderInfo{
Name: app.Config.AppName,
Description: app.Config.AppDescription,
Icon: app.Config.AppIcon,
Address: app.Config.Service.GetServiceFQDN(),
MimeTypes: mimeTypes,
},
}
resp, err := app.gwc.AddAppProvider(ctx, req)
if err != nil {
app.Logger.Error().Err(err).Msg("AddAppProvider failed")
return err
}
if resp.Status.Code != rpcv1beta1.Code_CODE_OK {
app.Logger.Error().Str("status_code", resp.Status.Code.String()).Msg("AddAppProvider failed")
return errors.New("status code != CODE_OK")
}
return nil
}