feat: initial release
This commit is contained in:
parent
a3cf21f5bd
commit
1e0ee5bffe
40 changed files with 2007 additions and 217 deletions
7
internal/types/host.go
Normal file
7
internal/types/host.go
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package types
|
||||
|
||||
type HostState struct {
|
||||
Domains []string
|
||||
Endpoint string
|
||||
Name string
|
||||
}
|
||||
16
internal/types/node.go
Normal file
16
internal/types/node.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package types
|
||||
|
||||
// TODO: consider moving this type back to registry
|
||||
type Node struct {
|
||||
Address string `json:"address"`
|
||||
Name string `json:"name"`
|
||||
Roles []Role `json:"roles"`
|
||||
}
|
||||
|
||||
func NewNode(address string, name string, roles []Role) Node {
|
||||
return Node{
|
||||
Address: address,
|
||||
Name: name,
|
||||
Roles: roles,
|
||||
}
|
||||
}
|
||||
39
internal/types/roles.go
Normal file
39
internal/types/roles.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
package types
|
||||
|
||||
type Role string
|
||||
|
||||
const (
|
||||
MasterRole Role = "master"
|
||||
HostRole Role = "host"
|
||||
DnsRole Role = "dns"
|
||||
NameserverRole Role = "ns"
|
||||
)
|
||||
|
||||
var Roles = []Role{
|
||||
MasterRole,
|
||||
HostRole,
|
||||
DnsRole,
|
||||
NameserverRole,
|
||||
}
|
||||
|
||||
var Names = func() []Role {
|
||||
o := make([]Role, 0, len(Roles))
|
||||
for _, r := range Roles {
|
||||
o = append(o, r)
|
||||
}
|
||||
return o
|
||||
}()
|
||||
|
||||
func (r Role) String() string {
|
||||
return string(r)
|
||||
}
|
||||
|
||||
func Parse(s string) (Role, bool) {
|
||||
for _, r := range Roles {
|
||||
if s == r.String() {
|
||||
return r, true
|
||||
}
|
||||
}
|
||||
|
||||
return "", false
|
||||
}
|
||||
1
internal/types/rpc.go
Normal file
1
internal/types/rpc.go
Normal file
|
|
@ -0,0 +1 @@
|
|||
package types
|
||||
76
internal/types/web.go
Normal file
76
internal/types/web.go
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
package types
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Path string
|
||||
|
||||
func (p Path) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
const (
|
||||
PathMasterJoin Path = "/master/join"
|
||||
PathMasterLeave Path = "/master/leave"
|
||||
PathMasterKeepalive Path = "/master/keepalive"
|
||||
|
||||
PathNodeHealthcheck Path = "/node/healthcheck"
|
||||
PathNodeJoin Path = "/node/join"
|
||||
PathNodeLeave Path = "/node/leave"
|
||||
|
||||
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))
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue