diff --git a/agent/agent.go b/agent/agent.go new file mode 100644 index 0000000..9ccbc0a --- /dev/null +++ b/agent/agent.go @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2024 OrigAdmin. All rights reserved. + */ + +// Package agent implements the functions, types, and interfaces for the module. +package agent + +import ( + "fmt" + + "github.com/go-kratos/kratos/v2/transport/http" +) + +const ( + DefaultPrefix = "/api" + DefaultVersion = "v1" +) + +type Agent interface { + URI() string + Server() *http.Server + Route() *http.Router +} + +type agent struct { + prefix string + version string + server *http.Server +} + +func (obj *agent) SetPrefix(prefix string) { + obj.prefix = prefix +} + +func (obj *agent) SetVersion(version string) { + obj.version = version +} + +func (obj *agent) Server() *http.Server { + return obj.server +} + +func (obj *agent) Route() *http.Router { + return obj.server.Route(obj.URI()) +} + +func (obj *agent) URI() string { + return fmt.Sprintf("%s/%s", obj.prefix, obj.version) +} + +func New(server *http.Server) Agent { + return &agent{ + prefix: DefaultPrefix, + version: DefaultVersion, + server: server, + } +}