-
Notifications
You must be signed in to change notification settings - Fork 16
/
get.go
89 lines (79 loc) · 2.15 KB
/
get.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
package query
import (
"context"
"net/url"
"path"
"github.com/Bio-OS/bioos/internal/context/notebookserver/domain"
"github.com/Bio-OS/bioos/pkg/errors"
"github.com/Bio-OS/bioos/pkg/notebook"
"github.com/Bio-OS/bioos/pkg/validator"
)
type GetQuery struct {
ID string `validate:"required"`
WorkspaceID string `validate:"required"`
EditNotebook string
}
type GetHandler interface {
Handle(context.Context, *GetQuery) (*NotebookServer, error)
}
type getHandler struct {
readModel ReadModel
runtime domain.Runtime
}
func NewGetHandler(readModel ReadModel, runtime domain.Runtime) GetHandler {
return &getHandler{
readModel: readModel,
runtime: runtime,
}
}
func (r *getHandler) Handle(ctx context.Context, q *GetQuery) (*NotebookServer, error) {
if err := validator.Validate(q); err != nil {
return nil, err
}
settings, err := r.readModel.GetSettingsByID(ctx, q.WorkspaceID, q.ID)
if err != nil {
return nil, err
}
status, err := r.runtime.GetStatus(ctx, &domain.NotebookServer{
ID: q.ID,
WorkspaceID: q.WorkspaceID,
})
if err != nil {
return nil, errors.NewInternalError(err)
}
res := &NotebookServer{
Status: status.Status,
NotebookSettings: *settings,
}
if q.EditNotebook == "" {
res.AccessURL = status.AccessURL
} else { // locate to specified ipynb
// TODO check notebook exist
res.AccessURL = getAccessURL(status, q.EditNotebook)
}
return res, nil
}
func getAccessURL(status *domain.Status, editNotebook string) string {
if status.Status == domain.ServerStatusRunning {
u, _ := url.Parse(status.AccessURL)
return u.JoinPath(getNotebookEditRelativePath(editNotebook)).String()
} else if status.Status == domain.ServerStatusPending {
u, _ := url.Parse(status.AccessURL)
urlQuery := u.Query()
if next := urlQuery.Get("next"); next != "" {
next = path.Join(next, getNotebookEditRelativePath(editNotebook))
urlQuery.Set("next", next)
u.RawQuery = urlQuery.Encode()
}
return u.String()
}
return ""
}
func getNotebookEditRelativePath(editNotebook string) string {
return path.Join(
"lab",
"tree",
notebook.MountRelativePathNotebook,
editNotebook+notebook.NotebookFileExt,
)
}