generated from origadmin/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implement Agent interface with methods for URI, Server, and Route - Create agent struct with prefix, version, and server fields - Add methods to set prefix and version - Define default prefix and version constants - Implement New function to create agent instance
- Loading branch information
Showing
1 changed file
with
57 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} |