micro--go-micro
76bfeae456
* docs: update all four documentation guides and mark Q2 complete - ai-native-services: add WithMCP one-liner, standalone gateway, WebSocket client example, and OpenTelemetry observability section - mcp-security: add OTel distributed tracing, WebSocket authentication (connection-level and per-message), DeniedReason audit field - tool-descriptions: add manual overrides with WithEndpointDocs and export formats section - agent-patterns: add LangChain/LlamaIndex SDK pattern and standalone gateway production pattern with Docker example - Update roadmap: mark Q2 documentation as complete, Q2 at 100% - Update status: reflect all recent completions, shift priorities https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: add agent demo example and blog post Add examples/agent-demo with a multi-service project management app (projects, tasks, team) that demonstrates AI agents interacting with Go Micro services through MCP. Includes seed data and example prompts. Add blog post 4 "Agents Meet Microservices: A Hands-On Demo" walking through the example code and showing cross-service agent workflows. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: enable multiple services in a single binary Remove global state mutations from service and cmd option functions so that configuring one service no longer overwrites another's settings. Key changes: - service/options.go: remove all DefaultXxx global writes from option functions; newOptions() now creates fresh Server, Client, Store, and Cache per service while sharing Registry, Broker, and Transport - cmd/cmd.go: newCmd() uses local copies instead of pointers to package globals; Before() no longer mutates DefaultXxx vars - cmd/options.go: remove global mutations from all option functions - service/service.go: export ServiceImpl type for cross-package use - service/group.go: new Group type for multi-service lifecycle - micro.go: add Start/Stop to Service interface, expose Group and NewGroup convenience function - examples/multi-service: working example with two services https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * docs: highlight multi-service binary support Add multi-service section to README with code example, update features list, add to examples index, and note in status summary. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: unify service API and clean up developer experience - Unified service creation: micro.New("name", opts...) as canonical API - Clean handler registration: service.Handle(handler, opts...) accepts server.HandlerOption args directly, no need to reach through Server() - Unexported serviceImpl: users interact through Service interface only - Service groups use Service interface (not concrete type) - Fixed Stop() to properly propagate BeforeStop/AfterStop errors - Fixed store init: error-level log instead of fatal on init failure - Updated all examples to use consistent patterns - Updated README, getting-started, MCP docs, and guides - Added blog post about the DX cleanup https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * fix: add blog post 5 to blog index Blog post 5 (Developer Experience Cleanup) existed as a file but was missing from the blog index page. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: make micro new generate MCP-enabled services by default - main.go template includes mcp.WithMCP(":3001") by default - Handler template has agent-friendly doc comments with @example tags - Proto template has descriptive field comments - README includes MCP usage, Claude Code config, and tool description tips - Makefile adds mcp-tools, mcp-test, mcp-serve targets - go.mod updated to Go 1.22 - Added --no-mcp flag to opt out of MCP integration - Post-create output shows MCP endpoint URLs https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * docs: add MCP migration guide and troubleshooting guide - Migration guide: 3 approaches to add MCP to existing services (WithMCP one-liner, standalone gateway, CLI) - Troubleshooting guide: common issues with agents, WebSocket, Claude Code, auth, rate limiting, and performance https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * refactor: rename model/ package to ai/ for AI model providers The model/ package name conflicted with the conventional use of "model" for data models. Renamed to ai/ which better describes the package's purpose (AI provider abstraction for Anthropic, OpenAI, etc.) and frees up model/ for future data model layer use. - Rename model/ → ai/ with package name change - Update all Go imports from go-micro.dev/v5/model to go-micro.dev/v5/ai - Update cmd/micro/server/server.go references (model.X → ai.X) - Update all documentation and roadmap references - All tests pass, CLI builds successfully https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: add model package for typed data access with CRUD and queries New model/ package provides a typed data model layer using Go generics. Supports structured CRUD operations, WHERE filters, ordering, pagination, and automatic schema creation from struct tags. Three backends: - memory: in-memory for development and testing - sqlite: embedded SQL for dev and single-node production - postgres: full PostgreSQL for production deployments Key features: - Generic Model[T] with Create/Read/Update/Delete/List/Count - Query builder: Where(), WhereOp(), OrderAsc/Desc(), Limit(), Offset() - Struct tags: model:"key" for primary key, model:"index" for indexes - Auto table creation from struct schema - 19 tests passing across memory and sqlite backends https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: add model code generation to protoc-gen-micro Extend the micro plugin to generate model structs from proto messages annotated with // @model. Generated alongside client/server code in the same .pb.micro.go file. For a proto message like: // @model message User { string id = 1; string name = 2; } Generates: - UserModel struct with model:"key" and json tags - NewUserModel(db) factory returning *model.Model[UserModel] - UserModelFromProto(*User) *UserModel converter - (*UserModel).ToProto() *User converter Supports @model(table=custom_table, key=custom_field) options. Adds GetComments() to generator for plugin comment inspection. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc * feat: add Model() to Service interface for Client/Server/Model trifecta Every service now exposes Client(), Server(), and Model() — call services, handle requests, and save/query data from the same interface. Includes README docs, blog post, and a full model guide on the docs site. https://claude.ai/code/session_01GkduEhcrqcG45rdfYh8dAc --------- Co-authored-by: Claude <noreply@anthropic.com>
369 行
8.0 KiB
Go
369 行
8.0 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
"go-micro.dev/v5/auth"
|
|
"go-micro.dev/v5/broker"
|
|
"go-micro.dev/v5/cache"
|
|
"go-micro.dev/v5/client"
|
|
"go-micro.dev/v5/cmd"
|
|
"go-micro.dev/v5/config"
|
|
"go-micro.dev/v5/debug/profile"
|
|
"go-micro.dev/v5/debug/trace"
|
|
"go-micro.dev/v5/logger"
|
|
"go-micro.dev/v5/model"
|
|
"go-micro.dev/v5/model/memory"
|
|
"go-micro.dev/v5/registry"
|
|
"go-micro.dev/v5/selector"
|
|
"go-micro.dev/v5/server"
|
|
"go-micro.dev/v5/store"
|
|
"go-micro.dev/v5/transport"
|
|
)
|
|
|
|
// Options for micro service.
|
|
type Options struct {
|
|
Registry registry.Registry
|
|
Store store.Store
|
|
Auth auth.Auth
|
|
Cmd cmd.Cmd
|
|
Config config.Config
|
|
Client client.Client
|
|
Server server.Server
|
|
Model model.Database
|
|
|
|
// Other options for implementations of the interface
|
|
// can be stored in a context
|
|
Context context.Context
|
|
|
|
Cache cache.Cache
|
|
Profile profile.Profile
|
|
Transport transport.Transport
|
|
Logger logger.Logger
|
|
Broker broker.Broker
|
|
// Before and After funcs
|
|
BeforeStart []func() error
|
|
AfterStart []func() error
|
|
AfterStop []func() error
|
|
|
|
BeforeStop []func() error
|
|
|
|
Signal bool
|
|
}
|
|
|
|
type Option func(*Options)
|
|
|
|
func newOptions(opts ...Option) Options {
|
|
opt := Options{
|
|
Auth: auth.DefaultAuth,
|
|
Broker: broker.DefaultBroker,
|
|
Cmd: cmd.NewCmd(),
|
|
Config: config.DefaultConfig,
|
|
// Per-service instances: each service gets its own server, client,
|
|
// store, and cache to allow multiple services in a single binary.
|
|
Client: client.NewClient(),
|
|
Server: server.NewRPCServer(),
|
|
Store: store.NewStore(),
|
|
Model: memory.New(),
|
|
Cache: cache.NewCache(),
|
|
Registry: registry.DefaultRegistry,
|
|
Transport: transport.DefaultTransport,
|
|
Context: context.Background(),
|
|
Signal: true,
|
|
Logger: logger.DefaultLogger,
|
|
}
|
|
|
|
for _, o := range opts {
|
|
o(&opt)
|
|
}
|
|
|
|
return opt
|
|
}
|
|
|
|
// Broker to be used for service.
|
|
func Broker(b broker.Broker) Option {
|
|
return func(o *Options) {
|
|
o.Broker = b
|
|
// Update Client and Server
|
|
o.Client.Init(client.Broker(b))
|
|
o.Server.Init(server.Broker(b))
|
|
}
|
|
}
|
|
|
|
func Cache(c cache.Cache) Option {
|
|
return func(o *Options) {
|
|
o.Cache = c
|
|
}
|
|
}
|
|
|
|
func Cmd(c cmd.Cmd) Option {
|
|
return func(o *Options) {
|
|
o.Cmd = c
|
|
}
|
|
}
|
|
|
|
// Client to be used for service.
|
|
func Client(c client.Client) Option {
|
|
return func(o *Options) {
|
|
o.Client = c
|
|
}
|
|
}
|
|
|
|
// Context specifies a context for the service.
|
|
// Can be used to signal shutdown of the service and for extra option values.
|
|
func Context(ctx context.Context) Option {
|
|
return func(o *Options) {
|
|
o.Context = ctx
|
|
}
|
|
}
|
|
|
|
// Handle will register a handler without any fuss
|
|
func Handle(v interface{}) Option {
|
|
return func(o *Options) {
|
|
o.Server.Handle(
|
|
o.Server.NewHandler(v),
|
|
)
|
|
}
|
|
}
|
|
|
|
// HandleSignal toggles automatic installation of the signal handler that
|
|
// traps TERM, INT, and QUIT. Users of this feature to disable the signal
|
|
// handler, should control liveness of the service through the context.
|
|
func HandleSignal(b bool) Option {
|
|
return func(o *Options) {
|
|
o.Signal = b
|
|
}
|
|
}
|
|
|
|
// Profile to be used for debug profile.
|
|
func Profile(p profile.Profile) Option {
|
|
return func(o *Options) {
|
|
o.Profile = p
|
|
}
|
|
}
|
|
|
|
// Server to be used for service.
|
|
func Server(s server.Server) Option {
|
|
return func(o *Options) {
|
|
o.Server = s
|
|
}
|
|
}
|
|
|
|
// Store sets the store to use.
|
|
func Store(s store.Store) Option {
|
|
return func(o *Options) {
|
|
o.Store = s
|
|
}
|
|
}
|
|
|
|
// Model sets the model database to use.
|
|
func Model(db model.Database) Option {
|
|
return func(o *Options) {
|
|
o.Model = db
|
|
}
|
|
}
|
|
|
|
// Registry sets the registry for the service
|
|
// and the underlying components.
|
|
func Registry(r registry.Registry) Option {
|
|
return func(o *Options) {
|
|
o.Registry = r
|
|
// Update Client and Server
|
|
o.Client.Init(client.Registry(r))
|
|
o.Server.Init(server.Registry(r))
|
|
// Update Broker
|
|
o.Broker.Init(broker.Registry(r))
|
|
}
|
|
}
|
|
|
|
// Tracer sets the tracer for the service.
|
|
func Tracer(t trace.Tracer) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.Tracer(t))
|
|
}
|
|
|
|
}
|
|
|
|
// Auth sets the auth for the service.
|
|
func Auth(a auth.Auth) Option {
|
|
return func(o *Options) {
|
|
o.Auth = a
|
|
}
|
|
}
|
|
|
|
// Config sets the config for the service.
|
|
func Config(c config.Config) Option {
|
|
return func(o *Options) {
|
|
o.Config = c
|
|
}
|
|
}
|
|
|
|
// Selector sets the selector for the service client.
|
|
func Selector(s selector.Selector) Option {
|
|
return func(o *Options) {
|
|
o.Client.Init(client.Selector(s))
|
|
}
|
|
}
|
|
|
|
// Transport sets the transport for the service
|
|
// and the underlying components.
|
|
func Transport(t transport.Transport) Option {
|
|
return func(o *Options) {
|
|
o.Transport = t
|
|
// Update Client and Server
|
|
o.Client.Init(client.Transport(t))
|
|
o.Server.Init(server.Transport(t))
|
|
}
|
|
}
|
|
|
|
// Convenience options
|
|
|
|
// Address sets the address of the server.
|
|
func Address(addr string) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.Address(addr))
|
|
}
|
|
}
|
|
|
|
// Name of the service.
|
|
func Name(n string) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.Name(n))
|
|
}
|
|
}
|
|
|
|
// Version of the service.
|
|
func Version(v string) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.Version(v))
|
|
}
|
|
}
|
|
|
|
// Metadata associated with the service.
|
|
func Metadata(md map[string]string) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.Metadata(md))
|
|
}
|
|
}
|
|
|
|
// Flags that can be passed to service.
|
|
func Flags(flags ...cli.Flag) Option {
|
|
return func(o *Options) {
|
|
o.Cmd.App().Flags = append(o.Cmd.App().Flags, flags...)
|
|
}
|
|
}
|
|
|
|
// Action can be used to parse user provided cli options.
|
|
func Action(a func(*cli.Context) error) Option {
|
|
return func(o *Options) {
|
|
o.Cmd.App().Action = a
|
|
}
|
|
}
|
|
|
|
// RegisterTTL specifies the TTL to use when registering the service.
|
|
func RegisterTTL(t time.Duration) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.RegisterTTL(t))
|
|
}
|
|
}
|
|
|
|
// RegisterInterval specifies the interval on which to re-register.
|
|
func RegisterInterval(t time.Duration) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(server.RegisterInterval(t))
|
|
}
|
|
}
|
|
|
|
// WrapClient is a convenience method for wrapping a Client with
|
|
// some middleware component. A list of wrappers can be provided.
|
|
// Wrappers are applied in reverse order so the last is executed first.
|
|
func WrapClient(w ...client.Wrapper) Option {
|
|
return func(o *Options) {
|
|
// apply in reverse
|
|
for i := len(w); i > 0; i-- {
|
|
o.Client = w[i-1](o.Client)
|
|
}
|
|
}
|
|
}
|
|
|
|
// WrapCall is a convenience method for wrapping a Client CallFunc.
|
|
func WrapCall(w ...client.CallWrapper) Option {
|
|
return func(o *Options) {
|
|
o.Client.Init(client.WrapCall(w...))
|
|
}
|
|
}
|
|
|
|
// WrapHandler adds a handler Wrapper to a list of options passed into the server.
|
|
func WrapHandler(w ...server.HandlerWrapper) Option {
|
|
return func(o *Options) {
|
|
var wrappers []server.Option
|
|
|
|
for _, wrap := range w {
|
|
wrappers = append(wrappers, server.WrapHandler(wrap))
|
|
}
|
|
|
|
// Init once
|
|
o.Server.Init(wrappers...)
|
|
}
|
|
}
|
|
|
|
// WrapSubscriber adds a subscriber Wrapper to a list of options passed into the server.
|
|
func WrapSubscriber(w ...server.SubscriberWrapper) Option {
|
|
return func(o *Options) {
|
|
var wrappers []server.Option
|
|
|
|
for _, wrap := range w {
|
|
wrappers = append(wrappers, server.WrapSubscriber(wrap))
|
|
}
|
|
|
|
// Init once
|
|
o.Server.Init(wrappers...)
|
|
}
|
|
}
|
|
|
|
// Add opt to server option.
|
|
func AddListenOption(option server.Option) Option {
|
|
return func(o *Options) {
|
|
o.Server.Init(option)
|
|
}
|
|
}
|
|
|
|
// Before and Afters
|
|
|
|
// BeforeStart run funcs before service starts.
|
|
func BeforeStart(fn func() error) Option {
|
|
return func(o *Options) {
|
|
o.BeforeStart = append(o.BeforeStart, fn)
|
|
}
|
|
}
|
|
|
|
// BeforeStop run funcs before service stops.
|
|
func BeforeStop(fn func() error) Option {
|
|
return func(o *Options) {
|
|
o.BeforeStop = append(o.BeforeStop, fn)
|
|
}
|
|
}
|
|
|
|
// AfterStart run funcs after service starts.
|
|
func AfterStart(fn func() error) Option {
|
|
return func(o *Options) {
|
|
o.AfterStart = append(o.AfterStart, fn)
|
|
}
|
|
}
|
|
|
|
// AfterStop run funcs after service stops.
|
|
func AfterStop(fn func() error) Option {
|
|
return func(o *Options) {
|
|
o.AfterStop = append(o.AfterStop, fn)
|
|
}
|
|
}
|
|
|
|
// Logger sets the logger for the service.
|
|
func Logger(l logger.Logger) Option {
|
|
return func(o *Options) {
|
|
o.Logger = l
|
|
}
|
|
}
|