api: add node info
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
b0e8f71f8d
commit
c9b5e56f14
@ -53,6 +53,7 @@ func run(ctx *cli.Context) {
|
||||
slog.Debug("Starting run()")
|
||||
|
||||
apiMux := http.NewServeMux()
|
||||
apiMux.HandleFunc("GET /node", hvpnnode3.HandleGetNodeInfo(wgLink))
|
||||
apiMux.HandleFunc("GET /peer/{pubkey}", hvpnnode3.HandleGetPeer(wgLink))
|
||||
apiMux.HandleFunc("POST /peer", hvpnnode3.HandlePostPeer(wgLink))
|
||||
apiMux.HandleFunc("DELETE /peer/{pubkey}", hvpnnode3.HandleDeletePeer(wgLink))
|
||||
|
20
handlers.go
20
handlers.go
@ -17,6 +17,26 @@ import (
|
||||
type CtxKey string
|
||||
const CtxReqID CtxKey = "request_id"
|
||||
|
||||
func HandleGetNodeInfo(wgLink *WGLink) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
reqID := r.Context().Value(CtxReqID).(uuid.UUID)
|
||||
debug("Preparing node info for", reqID)
|
||||
dev, err := wgLink.Device(wgLink.Name)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(
|
||||
proto.NodePublicInfo{
|
||||
PublicKey: dev.PublicKey.String(),
|
||||
UDPPort: dev.ListenPort,
|
||||
// TODO: Send endpoint for clients to connect
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func HandleGetPeer(wgLink *WGLink) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
reqID := r.Context().Value(CtxReqID).(uuid.UUID)
|
||||
|
7
proto/node.go
Normal file
7
proto/node.go
Normal file
@ -0,0 +1,7 @@
|
||||
package proto
|
||||
|
||||
type NodePublicInfo struct {
|
||||
PublicKey string `json:"public_key"`
|
||||
UDPPort int `json:"udp_port"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user