1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00
rolens/internal/app/connection.go

102 lines
3.0 KiB
Go
Raw Permalink Normal View History

2023-01-14 19:47:29 +00:00
package app
import (
2023-01-22 20:12:56 +00:00
"context"
"errors"
"time"
2023-01-14 19:47:29 +00:00
"github.com/wailsapp/wails/v2/pkg/runtime"
"go.mongodb.org/mongo-driver/bson"
2023-01-22 20:12:56 +00:00
"go.mongodb.org/mongo-driver/mongo"
mongoOptions "go.mongodb.org/mongo-driver/mongo/options"
2023-01-14 19:47:29 +00:00
)
type OpenConnectionResult struct {
Databases []string `json:"databases"`
Status bson.M `json:"status"`
StatusError string `json:"statusError"`
SystemInfo bson.M `json:"systemInfo"`
SystemInfoError string `json:"systemInfoError"`
2023-06-07 19:52:43 +00:00
}
2023-01-22 20:12:56 +00:00
func (a *App) connectToHost(hostKey string) (*mongo.Client, context.Context, func(), error) {
hosts, err := a.Hosts()
if err != nil {
runtime.LogInfof(a.ctx, "Error while getting hosts: %s", err.Error())
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error getting hosts",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-01-22 20:12:56 +00:00
return nil, nil, nil, errors.New("could not retrieve hosts")
}
h := hosts[hostKey]
if len(h.URI) == 0 {
runtime.LogInfof(a.ctx, "Invalid URI (len == 0) for host %s", hostKey)
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Invalid host information",
Message: "You haven't specified a valid uri for the selected host.",
Type: runtime.ErrorDialog,
})
2023-01-22 20:12:56 +00:00
return nil, nil, nil, errors.New("invalid uri")
}
appName := "Rolens v" + a.Env.Version
opts := mongoOptions.Client()
opts.AppName = &appName
opts.ApplyURI(h.URI)
client, err := mongo.NewClient(opts)
2023-01-22 20:12:56 +00:00
if err != nil {
runtime.LogWarningf(a.ctx, "Could not connect to host %s: %s", hostKey, err.Error())
2023-06-24 18:27:48 +00:00
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error while connecting to " + h.Name,
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-01-22 20:12:56 +00:00
return nil, nil, nil, errors.New("could not establish a connection with " + h.Name)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
client.Connect(ctx)
return client, ctx, func() {
client.Disconnect(ctx)
cancel()
}, nil
}
func (a *App) OpenConnection(hostKey string) (result OpenConnectionResult) {
2023-01-14 19:47:29 +00:00
client, ctx, close, err := a.connectToHost(hostKey)
if err != nil {
2023-06-07 19:52:43 +00:00
return
2023-01-14 19:47:29 +00:00
}
defer close()
2023-06-07 19:52:43 +00:00
result.Databases, err = client.ListDatabaseNames(ctx, bson.M{})
2023-01-14 19:47:29 +00:00
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.LogWarningf(a.ctx, "Could not retrieve database names for host %s: %s", hostKey, err.Error())
runtime.MessageDialog(a.ctx, runtime.MessageDialogOptions{
Title: "Error getting database list",
Message: err.Error(),
Type: runtime.ErrorDialog,
})
2023-06-07 19:52:43 +00:00
}
command := bson.M{"serverStatus": 1}
err = client.Database("admin").RunCommand(ctx, command).Decode(&result.Status)
2023-06-07 19:52:43 +00:00
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.LogWarningf(a.ctx, "Could not retrieve server status: %s", err.Error())
result.StatusError = err.Error()
2023-06-07 19:52:43 +00:00
}
command = bson.M{"hostInfo": 1}
err = client.Database("admin").RunCommand(ctx, command).Decode(&result.SystemInfo)
2023-06-07 19:52:43 +00:00
if err != nil {
2023-06-24 18:27:48 +00:00
runtime.LogWarningf(a.ctx, "Could not retrieve system info: %s", err.Error())
result.SystemInfoError = err.Error()
2023-01-14 19:47:29 +00:00
}
2023-06-07 19:52:43 +00:00
return
2023-01-14 19:47:29 +00:00
}