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
|
|
@ -1,79 +1,60 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// TODO: split this up
|
||||
|
||||
type Path string
|
||||
|
||||
func (p Path) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
const (
|
||||
PathMasterJoin Path = "/master/join"
|
||||
PathMasterLeave Path = "/master/leave"
|
||||
PathMasterKeepalive Path = "/master/keepalive"
|
||||
PathMasterEventJoin Path = "/master/event_join"
|
||||
PathMasterEventLeave Path = "/master/event_leave"
|
||||
PathMasterEventKeepalive Path = "/master/event_keepalive"
|
||||
|
||||
PathNodeHealthcheck Path = "/node/healthcheck"
|
||||
|
||||
PathDnsCallback Path = "/dns/callback"
|
||||
|
||||
PathHostCallback Path = "/host/callback"
|
||||
PathHostDns Path = "/host/dns"
|
||||
PathHostNs Path = "/host/ns"
|
||||
)
|
||||
|
||||
type Response[T any] struct {
|
||||
Ok bool `json:"ok"`
|
||||
Data T `json:"data,omitempty"`
|
||||
Err string `json:"err,omitempty"`
|
||||
}
|
||||
|
||||
type Route interface {
|
||||
Path() string
|
||||
Handle([]byte) (any, error)
|
||||
}
|
||||
|
||||
type endpoint struct {
|
||||
path string
|
||||
handler func([]byte) (any, error)
|
||||
}
|
||||
|
||||
func (e endpoint) Path() string { return e.path }
|
||||
|
||||
func (e endpoint) Handle(v []byte) (any, error) { return e.handler(v) }
|
||||
|
||||
func PostEndpoint[T any, V any](path Path, handler func(T) (V, error)) Route {
|
||||
return endpoint{
|
||||
path: "POST " + path.String(),
|
||||
handler: func(a []byte) (any, error) {
|
||||
var r T
|
||||
if err := json.Unmarshal(a, &r); err != nil {
|
||||
return nil, fmt.Errorf("unable to unmarshal json: %w", err)
|
||||
}
|
||||
return handler(r)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func GetEndpoint[T any](path Path, handler func() (T, error)) Route {
|
||||
return endpoint{
|
||||
path: "GET " + path.String(),
|
||||
handler: func(a []byte) (any, error) {
|
||||
return handler()
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type Registrator interface {
|
||||
Register(endpoint Route)
|
||||
RegisterRaw(method string, pattern string, handler func(http.ResponseWriter, *http.Request))
|
||||
}
|
||||
// type Path string
|
||||
//
|
||||
// func (p Path) String() string {
|
||||
// return string(p)
|
||||
// }
|
||||
//
|
||||
// const (
|
||||
// PathMasterJoin Path = "/master/join"
|
||||
// PathMasterLeave Path = "/master/leave"
|
||||
// PathMasterKeepalive Path = "/master/keepalive"
|
||||
// PathMasterEventJoin Path = "/master/event_join"
|
||||
// PathMasterEventLeave Path = "/master/event_leave"
|
||||
// PathMasterEventKeepalive Path = "/master/event_keepalive"
|
||||
//
|
||||
// PathNodeHealthcheck Path = "/node/healthcheck"
|
||||
//
|
||||
// PathDnsCallback Path = "/dns/callback"
|
||||
//
|
||||
// PathHostCallback Path = "/host/callback"
|
||||
// PathHostDns Path = "/host/dns"
|
||||
// PathHostNs Path = "/host/ns"
|
||||
// )
|
||||
//
|
||||
// type Route interface {
|
||||
// Path() string
|
||||
// Handle([]byte) (any, error)
|
||||
// }
|
||||
//
|
||||
// type endpoint struct {
|
||||
// path string
|
||||
// handler func([]byte) (any, error)
|
||||
// }
|
||||
//
|
||||
// func (e endpoint) Path() string { return e.path }
|
||||
//
|
||||
// func (e endpoint) Handle(v []byte) (any, error) { return e.handler(v) }
|
||||
//
|
||||
// func PostEndpoint[T any, V any](path Path, handler func(T) (V, error)) Route {
|
||||
// return endpoint{
|
||||
// path: "POST " + path.String(),
|
||||
// handler: func(a []byte) (any, error) {
|
||||
// var r T
|
||||
// if err := json.Unmarshal(a, &r); err != nil {
|
||||
// return nil, fmt.Errorf("unable to unmarshal json: %w", err)
|
||||
// }
|
||||
// return handler(r)
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// func GetEndpoint[T any](path Path, handler func() (T, error)) Route {
|
||||
// return endpoint{
|
||||
// path: "GET " + path.String(),
|
||||
// handler: func(a []byte) (any, error) {
|
||||
// return handler()
|
||||
// },
|
||||
// }
|
||||
// }
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue