改进 认证和上下文信息获取

This commit is contained in:
ivamp 2024-07-16 02:05:02 +08:00
parent 9a8a33beac
commit df831cd6af
4 changed files with 51 additions and 12 deletions

View File

@ -26,10 +26,10 @@ func Execute() {
rootCmd.AddCommand(workerCommand)
rootCmd.AddCommand(scheduleCommand)
//rootCmd.AddCommand(httpCommand)
rootCmd.AddCommand(httpCommand)
rootCmd.AddCommand(migrateCommand)
if config.DebugMode.Enable {
rootCmd.AddCommand(migrateCommand)
rootCmd.AddCommand(dsnCommand)
rootCmd.AddCommand(createGoMigrateCommand)
rootCmd.AddCommand(genMigrateCommand)

View File

@ -85,9 +85,10 @@ func (a *AuthLogic) GinUser(c *gin.Context) *models.User {
return user.(*models.User)
}
func GetUserId(ctx *gin.Context) string {
logic := AuthLogic{}
return logic.GinUser(ctx).Token.Sub
func GetUserId(ctx context.Context) string {
user := GetUser(ctx)
return user.Token.Sub
}
func GetUser(ctx context.Context) *models.User {

View File

@ -1,7 +1,7 @@
package logic
import (
"github.com/gin-gonic/gin"
"context"
"leafdev.top/leaf/rag/ent"
"leafdev.top/leaf/rag/ent/library"
"leafdev.top/leaf/rag/internal/providers"
@ -16,11 +16,11 @@ func NewLibraryLogic() *LibraryLogic {
return &LibraryLogic{}
}
func (l *LibraryLogic) ListLibrary(ctx *gin.Context) ([]*ent.Library, error) {
func (l *LibraryLogic) ListLibrary(ctx context.Context) ([]*ent.Library, error) {
return client.Library.Query().Where(library.UserID(GetUserId(ctx))).All(ctx)
}
func (l *LibraryLogic) CreateLibrary(ctx *gin.Context, name string) (*ent.Library, error) {
func (l *LibraryLogic) CreateLibrary(ctx context.Context, name string) (*ent.Library, error) {
return client.Library.Create().
SetName(name).
SetUserID(GetUserId(ctx)).

View File

@ -2,13 +2,51 @@ package libraryService
import (
"context"
"leafdev.top/leaf/rag/api/library"
"github.com/mitchellh/mapstructure"
libraryApi "leafdev.top/leaf/rag/api/library"
"leafdev.top/leaf/rag/internal/logic"
)
type LibraryService struct {
library.UnimplementedLibraryServiceServer
libraryApi.UnimplementedLibraryServiceServer
}
func (LibraryService) ListLibrary(ctx context.Context, req *library.ListLibraryRequest) (*library.ListLibraryResponse, error) {
return &library.ListLibraryResponse{}, nil
var libraryLogic = logic.NewLibraryLogic()
func (LibraryService) ListLibrary(ctx context.Context, _ *libraryApi.ListLibraryRequest) (*libraryApi.ListLibraryResponse, error) {
libraries, err := libraryLogic.ListLibrary(ctx)
if err != nil {
return nil, err
}
libraryResponse := &libraryApi.ListLibraryResponse{}
// convert
for _, library := range libraries {
var libraryResponseItem = libraryApi.Library{
Id: int64(library.ID),
Name: library.Name,
UserId: library.UserID,
CreatedAt: library.CreatedAt.String(),
UpdatedAt: library.UpdatedAt.String(),
}
libraryResponse.Libraries = append(libraryResponse.Libraries, &libraryResponseItem)
}
return libraryResponse, err
}
func (LibraryService) CreateLibrary(ctx context.Context, req *libraryApi.CreateLibraryRequest) (*libraryApi.CreateLibraryResponse, error) {
library, err := libraryLogic.CreateLibrary(ctx, req.Name)
if err != nil {
return nil, err
}
var libraryApiResponse libraryApi.CreateLibraryResponse
err = mapstructure.Decode(library, &libraryApiResponse)
if err != nil {
return nil, err
}
return &libraryApiResponse, nil
}