refactor: move http api to a new transport layer
This commit is contained in:
parent
476c4b056f
commit
0448f66ab2
41 changed files with 822 additions and 390 deletions
16
internal/transport/codec/codec.go
Normal file
16
internal/transport/codec/codec.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package codec
|
||||
|
||||
type Codec interface {
|
||||
Decode(data []byte, out any) error
|
||||
Encode(data any) ([]byte, error)
|
||||
}
|
||||
|
||||
func Decode[T any](c Codec, data []byte) (T, error) {
|
||||
var out T
|
||||
err := c.Decode(data, &out)
|
||||
return out, err
|
||||
}
|
||||
|
||||
func Encode[T any](c Codec, data T) ([]byte, error) {
|
||||
return c.Encode(data)
|
||||
}
|
||||
15
internal/transport/codec/json.go
Normal file
15
internal/transport/codec/json.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package codec
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type jsonCodec struct{}
|
||||
|
||||
var JSON = jsonCodec{}
|
||||
|
||||
func (jsonCodec) Decode(data []byte, out any) error {
|
||||
return json.Unmarshal(data, out)
|
||||
}
|
||||
|
||||
func (jsonCodec) Encode(data any) ([]byte, error) {
|
||||
return json.Marshal(data)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue