forked from giantswarm/cert-operator
/
endpoint.go
117 lines (97 loc) · 2.93 KB
/
endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package version
import (
"encoding/json"
"net/http"
microerror "github.com/giantswarm/microkit/error"
micrologger "github.com/giantswarm/microkit/logger"
kitendpoint "github.com/go-kit/kit/endpoint"
kithttp "github.com/go-kit/kit/transport/http"
"golang.org/x/net/context"
"github.com/giantswarm/cert-operator/server/middleware"
"github.com/giantswarm/cert-operator/service"
"github.com/giantswarm/cert-operator/service/version"
)
const (
// Method is the HTTP method this endpoint is registered for.
Method = "GET"
// Name identifies the endpoint. It is aligned to the package path.
Name = "version"
// Path is the HTTP request path this endpoint is registered for.
Path = "/"
)
// Config represents the configuration used to create a version endpoint.
type Config struct {
// Dependencies.
Logger micrologger.Logger
Middleware *middleware.Middleware
Service *service.Service
}
// DefaultConfig provides a default configuration to create a new version
// endpoint by best effort.
func DefaultConfig() Config {
return Config{
// Dependencies.
Logger: nil,
Middleware: nil,
Service: nil,
}
}
// New creates a new configured version endpoint.
func New(config Config) (*Endpoint, error) {
// Dependencies.
if config.Logger == nil {
return nil, microerror.MaskAnyf(invalidConfigError, "logger must not be empty")
}
if config.Middleware == nil {
return nil, microerror.MaskAnyf(invalidConfigError, "middleware must not be empty")
}
if config.Service == nil {
return nil, microerror.MaskAnyf(invalidConfigError, "service must not be empty")
}
newEndpoint := &Endpoint{
Config: config,
}
return newEndpoint, nil
}
type Endpoint struct {
Config
}
func (e *Endpoint) Decoder() kithttp.DecodeRequestFunc {
return func(ctx context.Context, r *http.Request) (interface{}, error) {
return nil, nil
}
}
func (e *Endpoint) Encoder() kithttp.EncodeResponseFunc {
return func(ctx context.Context, w http.ResponseWriter, response interface{}) error {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
return json.NewEncoder(w).Encode(response)
}
}
func (e *Endpoint) Endpoint() kitendpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
serviceResponse, err := e.Service.Version.Get(ctx, version.DefaultRequest())
if err != nil {
return nil, microerror.MaskAny(err)
}
response := DefaultResponse()
response.Description = serviceResponse.Description
response.GitCommit = serviceResponse.GitCommit
response.GoVersion = serviceResponse.GoVersion
response.Name = serviceResponse.Name
response.OSArch = serviceResponse.OSArch
response.Source = serviceResponse.Source
return response, nil
}
}
func (e *Endpoint) Method() string {
return Method
}
func (e *Endpoint) Middlewares() []kitendpoint.Middleware {
return []kitendpoint.Middleware{}
}
func (e *Endpoint) Name() string {
return Name
}
func (e *Endpoint) Path() string {
return Path
}