fix restore and profile tokens

This commit is contained in:
d34dscene
2025-08-04 15:10:54 +02:00
parent d84fbb7f35
commit 2d9fdf9c81
3 changed files with 49 additions and 7 deletions

View File

@@ -46,6 +46,13 @@ func (s *ProfileService) CreateProfile(
if err != nil { if err != nil {
return nil, connect.NewError(connect.CodeInternal, err) return nil, connect.NewError(connect.CodeInternal, err)
} }
s.app.Event.Broadcast(&mantraev1.EventStreamResponse{
Action: mantraev1.EventAction_EVENT_ACTION_CREATED,
Data: &mantraev1.EventStreamResponse_Profile{
Profile: result.ToProto(),
},
})
return connect.NewResponse(&mantraev1.CreateProfileResponse{ return connect.NewResponse(&mantraev1.CreateProfileResponse{
Profile: result.ToProto(), Profile: result.ToProto(),
}), nil }), nil
@@ -62,12 +69,25 @@ func (s *ProfileService) UpdateProfile(
} }
if req.Msg.GetRegenerateToken() { if req.Msg.GetRegenerateToken() {
params.Token = util.GenerateToken(6) params.Token = util.GenerateToken(6)
} else {
profile, err := s.app.Conn.GetQuery().GetProfile(ctx, params.ID)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
params.Token = profile.Token
} }
result, err := s.app.Conn.GetQuery().UpdateProfile(ctx, params) result, err := s.app.Conn.GetQuery().UpdateProfile(ctx, params)
if err != nil { if err != nil {
return nil, connect.NewError(connect.CodeInternal, err) return nil, connect.NewError(connect.CodeInternal, err)
} }
s.app.Event.Broadcast(&mantraev1.EventStreamResponse{
Action: mantraev1.EventAction_EVENT_ACTION_UPDATED,
Data: &mantraev1.EventStreamResponse_Profile{
Profile: result.ToProto(),
},
})
return connect.NewResponse(&mantraev1.UpdateProfileResponse{ return connect.NewResponse(&mantraev1.UpdateProfileResponse{
Profile: result.ToProto(), Profile: result.ToProto(),
}), nil }), nil
@@ -77,9 +97,20 @@ func (s *ProfileService) DeleteProfile(
ctx context.Context, ctx context.Context,
req *connect.Request[mantraev1.DeleteProfileRequest], req *connect.Request[mantraev1.DeleteProfileRequest],
) (*connect.Response[mantraev1.DeleteProfileResponse], error) { ) (*connect.Response[mantraev1.DeleteProfileResponse], error) {
profile, err := s.app.Conn.GetQuery().GetProfile(ctx, req.Msg.Id)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, err)
}
if err := s.app.Conn.GetQuery().DeleteProfile(ctx, req.Msg.Id); err != nil { if err := s.app.Conn.GetQuery().DeleteProfile(ctx, req.Msg.Id); err != nil {
return nil, connect.NewError(connect.CodeInternal, err) return nil, connect.NewError(connect.CodeInternal, err)
} }
s.app.Event.Broadcast(&mantraev1.EventStreamResponse{
Action: mantraev1.EventAction_EVENT_ACTION_DELETED,
Data: &mantraev1.EventStreamResponse_Profile{
Profile: profile.ToProto(),
},
})
return connect.NewResponse(&mantraev1.DeleteProfileResponse{}), nil return connect.NewResponse(&mantraev1.DeleteProfileResponse{}), nil
} }

View File

@@ -7,6 +7,7 @@ import (
"os" "os"
"time" "time"
"github.com/google/uuid"
"github.com/mizuchilabs/mantrae/server/internal/storage" "github.com/mizuchilabs/mantrae/server/internal/storage"
"github.com/mizuchilabs/mantrae/server/internal/store/db" "github.com/mizuchilabs/mantrae/server/internal/store/db"
"github.com/mizuchilabs/mantrae/server/internal/store/schema" "github.com/mizuchilabs/mantrae/server/internal/store/schema"
@@ -201,6 +202,7 @@ func DynamicToDB(
continue continue
} }
entryPointSet[ep] = db.CreateEntryPointParams{ entryPointSet[ep] = db.CreateEntryPointParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: ep, Name: ep,
IsDefault: false, IsDefault: false,
@@ -215,6 +217,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateHttpRouter(ctx, db.CreateHttpRouterParams{ if _, err := q.CreateHttpRouter(ctx, db.CreateHttpRouterParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapRouter(v), Config: schema.WrapRouter(v),
@@ -228,6 +231,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateHttpService(ctx, db.CreateHttpServiceParams{ if _, err := q.CreateHttpService(ctx, db.CreateHttpServiceParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapService(v), Config: schema.WrapService(v),
@@ -240,6 +244,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateHttpMiddleware(ctx, db.CreateHttpMiddlewareParams{ if _, err := q.CreateHttpMiddleware(ctx, db.CreateHttpMiddlewareParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapMiddleware(v), Config: schema.WrapMiddleware(v),
@@ -254,6 +259,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateTcpRouter(ctx, db.CreateTcpRouterParams{ if _, err := q.CreateTcpRouter(ctx, db.CreateTcpRouterParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapTCPRouter(v), Config: schema.WrapTCPRouter(v),
@@ -267,6 +273,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateTcpService(ctx, db.CreateTcpServiceParams{ if _, err := q.CreateTcpService(ctx, db.CreateTcpServiceParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapTCPService(v), Config: schema.WrapTCPService(v),
@@ -293,6 +300,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateUdpRouter(ctx, db.CreateUdpRouterParams{ if _, err := q.CreateUdpRouter(ctx, db.CreateUdpRouterParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapUDPRouter(v), Config: schema.WrapUDPRouter(v),
@@ -306,6 +314,7 @@ func DynamicToDB(
continue continue
} }
if _, err := q.CreateUdpService(ctx, db.CreateUdpServiceParams{ if _, err := q.CreateUdpService(ctx, db.CreateUdpServiceParams{
ID: uuid.New().String(),
ProfileID: profileID, ProfileID: profileID,
Name: k, Name: k,
Config: schema.WrapUDPService(v), Config: schema.WrapUDPService(v),

View File

@@ -1,16 +1,16 @@
<script lang="ts"> <script lang="ts">
import * as Dialog from '$lib/components/ui/dialog/index.js'; import { buildConnectionString, profileClient } from '$lib/api';
import { Button } from '$lib/components/ui/button/index.js'; import { Button } from '$lib/components/ui/button/index.js';
import * as Dialog from '$lib/components/ui/dialog/index.js';
import { Input } from '$lib/components/ui/input'; import { Input } from '$lib/components/ui/input';
import { Label } from '$lib/components/ui/label'; import { Label } from '$lib/components/ui/label';
import { toast } from 'svelte-sonner';
import Separator from '../ui/separator/separator.svelte';
import type { Profile } from '$lib/gen/mantrae/v1/profile_pb'; import type { Profile } from '$lib/gen/mantrae/v1/profile_pb';
import { buildConnectionString, profileClient } from '$lib/api';
import { ConnectError } from '@connectrpc/connect';
import { profile as profileStore } from '$lib/stores/profile'; import { profile as profileStore } from '$lib/stores/profile';
import CopyInput from '../ui/copy-input/copy-input.svelte'; import { ConnectError } from '@connectrpc/connect';
import { RotateCcw } from '@lucide/svelte'; import { RotateCcw } from '@lucide/svelte';
import { toast } from 'svelte-sonner';
import CopyInput from '../ui/copy-input/copy-input.svelte';
import Separator from '../ui/separator/separator.svelte';
interface Props { interface Props {
item: Profile; item: Profile;
@@ -75,8 +75,10 @@
description: item.description, description: item.description,
regenerateToken: true regenerateToken: true
}); });
if (!response.profile) throw new Error('Failed to regenerate token');
toast.success(`Token regenerated successfully`); toast.success(`Token regenerated successfully`);
if (response.profile) profileStore.value = response.profile; item = response.profile;
profileStore.value = response.profile;
} catch (err) { } catch (err) {
const e = ConnectError.from(err); const e = ConnectError.from(err);
toast.error('Failed to regenerate token', { description: e.message }); toast.error('Failed to regenerate token', { description: e.message });