forked from go-aie/gptbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
148 lines (121 loc) · 3.73 KB
/
http.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Code generated by kun; DO NOT EDIT.
// github.com/RussellLuo/kun
package main
import (
"context"
"net/http"
"github.com/RussellLuo/kun/pkg/httpcodec"
"github.com/RussellLuo/kun/pkg/httpoption"
"github.com/RussellLuo/kun/pkg/oas2"
"github.com/go-chi/chi"
kithttp "github.com/go-kit/kit/transport/http"
)
func NewHTTPRouter(svc Service, codecs httpcodec.Codecs, opts ...httpoption.Option) chi.Router {
r := chi.NewRouter()
options := httpoption.NewOptions(opts...)
r.Method("GET", "/api", oas2.Handler(OASv2APIDoc, options.ResponseSchema()))
var codec httpcodec.Codec
var validator httpoption.Validator
var kitOptions []kithttp.ServerOption
codec = codecs.EncodeDecoder("Chat")
validator = options.RequestValidator("Chat")
r.Method(
"POST", "/chat",
kithttp.NewServer(
MakeEndpointOfChat(svc),
decodeChatRequest(codec, validator),
httpcodec.MakeResponseEncoder(codec, 200),
append(kitOptions,
kithttp.ServerErrorEncoder(httpcodec.MakeErrorEncoder(codec)),
)...,
),
)
codec = codecs.EncodeDecoder("CreateDocuments")
validator = options.RequestValidator("CreateDocuments")
r.Method(
"POST", "/upsert",
kithttp.NewServer(
MakeEndpointOfCreateDocuments(svc),
decodeCreateDocumentsRequest(codec, validator),
httpcodec.MakeResponseEncoder(codec, 200),
append(kitOptions,
kithttp.ServerErrorEncoder(httpcodec.MakeErrorEncoder(codec)),
)...,
),
)
codec = codecs.EncodeDecoder("DeleteDocuments")
validator = options.RequestValidator("DeleteDocuments")
r.Method(
"POST", "/delete",
kithttp.NewServer(
MakeEndpointOfDeleteDocuments(svc),
decodeDeleteDocumentsRequest(codec, validator),
httpcodec.MakeResponseEncoder(codec, 200),
append(kitOptions,
kithttp.ServerErrorEncoder(httpcodec.MakeErrorEncoder(codec)),
)...,
),
)
codec = codecs.EncodeDecoder("UploadFile")
validator = options.RequestValidator("UploadFile")
r.Method(
"POST", "/upload",
kithttp.NewServer(
MakeEndpointOfUploadFile(svc),
decodeUploadFileRequest(codec, validator),
httpcodec.MakeResponseEncoder(codec, 200),
append(kitOptions,
kithttp.ServerErrorEncoder(httpcodec.MakeErrorEncoder(codec)),
)...,
),
)
return r
}
func decodeChatRequest(codec httpcodec.Codec, validator httpoption.Validator) kithttp.DecodeRequestFunc {
return func(_ context.Context, r *http.Request) (interface{}, error) {
var _req ChatRequest
if err := codec.DecodeRequestBody(r, &_req); err != nil {
return nil, err
}
if err := validator.Validate(&_req); err != nil {
return nil, err
}
return &_req, nil
}
}
func decodeCreateDocumentsRequest(codec httpcodec.Codec, validator httpoption.Validator) kithttp.DecodeRequestFunc {
return func(_ context.Context, r *http.Request) (interface{}, error) {
var _req CreateDocumentsRequest
if err := codec.DecodeRequestBody(r, &_req); err != nil {
return nil, err
}
if err := validator.Validate(&_req); err != nil {
return nil, err
}
return &_req, nil
}
}
func decodeDeleteDocumentsRequest(codec httpcodec.Codec, validator httpoption.Validator) kithttp.DecodeRequestFunc {
return func(_ context.Context, r *http.Request) (interface{}, error) {
var _req DeleteDocumentsRequest
if err := codec.DecodeRequestBody(r, &_req); err != nil {
return nil, err
}
if err := validator.Validate(&_req); err != nil {
return nil, err
}
return &_req, nil
}
}
func decodeUploadFileRequest(codec httpcodec.Codec, validator httpoption.Validator) kithttp.DecodeRequestFunc {
return func(_ context.Context, r *http.Request) (interface{}, error) {
var _req UploadFileRequest
if err := codec.DecodeRequestBody(r, &_req); err != nil {
return nil, err
}
if err := validator.Validate(&_req); err != nil {
return nil, err
}
return &_req, nil
}
}