Skip to content

Commit

Permalink
feat(runtime): add agent package
Browse files Browse the repository at this point in the history
- 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
godcong committed Dec 23, 2024
1 parent a168f22 commit 50299d2
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions agent/agent.go
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,
}
}

0 comments on commit 50299d2

Please sign in to comment.