Add HS256 JWT authentication to both HTTP and gRPC servers using Kratos jwt middleware with selector to skip auth for public share endpoints (GetShareInfo, DownloadShare). Wire DI updated to inject conf.Auth into server constructors.
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
//go:build wireinject
|
|
|
|
package main
|
|
|
|
import (
|
|
"rag/file-system/internal/biz"
|
|
"rag/file-system/internal/conf"
|
|
"rag/file-system/internal/data"
|
|
"rag/file-system/internal/server"
|
|
"rag/file-system/internal/service"
|
|
|
|
"github.com/go-kratos/kratos/v2"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-kratos/kratos/v2/transport/grpc"
|
|
"github.com/go-kratos/kratos/v2/transport/http"
|
|
"github.com/google/wire"
|
|
)
|
|
|
|
// newApp creates a new Kratos application with HTTP and gRPC servers.
|
|
func newApp(logger log.Logger, hs *http.Server, gs *grpc.Server) *kratos.App {
|
|
return kratos.New(
|
|
kratos.Name("file-system"),
|
|
kratos.Logger(logger),
|
|
kratos.Server(hs, gs),
|
|
)
|
|
}
|
|
|
|
// newConfServer extracts the Server config from Bootstrap.
|
|
func newConfServer(bc *conf.Bootstrap) *conf.Server {
|
|
return bc.GetServer()
|
|
}
|
|
|
|
// newConfData extracts the Data config from Bootstrap.
|
|
func newConfData(bc *conf.Bootstrap) *conf.Data {
|
|
return bc.GetData()
|
|
}
|
|
|
|
// newConfAuth extracts the Auth config from Bootstrap.
|
|
func newConfAuth(bc *conf.Bootstrap) *conf.Auth {
|
|
return bc.GetAuth()
|
|
}
|
|
|
|
// initApp wires up the entire dependency graph.
|
|
func initApp(*conf.Bootstrap, log.Logger) (*kratos.App, func(), error) {
|
|
panic(wire.Build(
|
|
// Config extraction
|
|
newConfServer,
|
|
newConfData,
|
|
newConfAuth,
|
|
|
|
// Provider sets from each layer
|
|
data.ProviderSet,
|
|
biz.ProviderSet,
|
|
service.ProviderSet,
|
|
server.ProviderSet,
|
|
|
|
// Interface bindings: biz interfaces → data implementations
|
|
wire.Bind(new(biz.FileRepo), new(*data.FileRepo)),
|
|
wire.Bind(new(biz.FolderRepo), new(*data.FolderRepo)),
|
|
wire.Bind(new(biz.FileMetaRepo), new(*data.FileMetaRepo)),
|
|
wire.Bind(new(biz.FileMetaRepoForShare), new(*data.FileMetaRepo)),
|
|
wire.Bind(new(biz.ShareRepo), new(*data.ShareRepo)),
|
|
|
|
// App constructor
|
|
newApp,
|
|
))
|
|
}
|