Skip to content

Commit

Permalink
restapi second release
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperBuker authored and a3s7p committed Oct 6, 2022
1 parent 770f094 commit 57c9df6
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
21 changes: 19 additions & 2 deletions relaycfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package relaycfg
import (
"errors"
"fmt"
"os"
"time"

"github.com/wireleap/common/api/duration"
Expand Down Expand Up @@ -34,6 +35,8 @@ type C struct {
// NetUsage is the allocated bandwith per time period.
// NetUsage is disabled if UsageCap.Duration is 0.
NetUsage NetUsage `json:"network_usage,omitempty"`
// RestApi configures the API REST services
RestApi RestApi `json:"rest_api,omitempty"`
// Contracts is the map of service contracts used by this wireleap-relay.
Contracts map[texturl.URL]*relayentry.T `json:"contracts,omitempty"`
// AutoUpgrade sets whether this relay should attempt auto-upgrades.
Expand All @@ -42,6 +45,16 @@ type C struct {
DangerZone DangerZone `json:"danger_zone,omitempty"`
}

// RestApi
type RestApi struct {
// Socket enabling
Socket bool `json:"socket_enabled"`
// Socket Umask
Umask os.FileMode `json:"socket_umask"`
// Address:Port
Address string `json:"tcp_addr"`
}

// Network usage soft-cap
// Soft-cap per contract defined in relayentry.T
type NetUsage struct {
Expand All @@ -65,8 +78,12 @@ func Defaults() C {
AutoSubmitInterval: duration.T(time.Minute * 5),
Timeout: duration.T(time.Second * 5),
BufSize: 4096,
Contracts: map[texturl.URL]*relayentry.T{},
AutoUpgrade: true,
RestApi: RestApi{
Socket: true,
Umask: 0600,
},
Contracts: map[texturl.URL]*relayentry.T{},
AutoUpgrade: true,
}
}

Expand Down
4 changes: 2 additions & 2 deletions relaylib/relaystatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type RelayStatus struct {
}

type RelayFlags struct {
Enrolled bool
NetCapReached bool
Enrolled bool `json:"enrolled"`
NetCapReached bool `json:"network_cap_reached"`
/**
ToDo: More flags to define the current status
- Failed heartbeats
Expand Down
33 changes: 27 additions & 6 deletions restapi/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/wireleap/common/api/provide"
"github.com/wireleap/common/api/status"
"github.com/wireleap/relay/contractmanager"
"github.com/wireleap/relay/relaycfg"
)

type T struct {
Expand Down Expand Up @@ -44,31 +45,51 @@ func New(manager *contractmanager.Manager) (t *T) {
return
}

func UnixServer(p string, fm os.FileMode, t *T) error {
if err := os.RemoveAll(p); err != nil {
func UnixServer(path string, fm os.FileMode, t *T) error {
if err := os.RemoveAll(path); err != nil {
return err
}

l, err := net.Listen("unix", p)
l, err := net.Listen("unix", path)
if err != nil {
return err
}
defer l.Close()

if err := os.Chmod(p, fm); err != nil {
if err := os.Chmod(path, fm); err != nil {
return err
}

h := &http.Server{Handler: t.mux}
return h.Serve(l)
}

func TCPServer(port string, t *T) error {
l, err := net.Listen("tcp", ":"+port)
func TCPServer(addr string, t *T) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
}

h := &http.Server{Handler: t.mux}
return h.Serve(l)
}

func Run(cfg relaycfg.C, path string, t *T) {
if len(cfg.RestApi.Address) > 0 {
go func() {
log.Println("Launching TCP Server")
if err := TCPServer(cfg.RestApi.Address, t); err != nil {
log.Print(err)
}
}()
}

if cfg.RestApi.Socket {
go func() {
log.Println("Launching UnixSocket Server")
if err := UnixServer(path, cfg.RestApi.Umask, t); err != nil {
log.Print(err)
}
}()
}
}
12 changes: 5 additions & 7 deletions sub/startcmd/startcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,18 @@ func serverun(fm fsdir.T) {
r.Manager.Stop()

fm.Del(filenames.Pid)
fm.Del(filenames.Socket)

if c.RestApi.Socket {
fm.Del(filenames.Socket)
}
return true
}

defer shutdown()

// Launch API REST goroutine
api := restapi.New(r.Manager)
go func() {
log.Println("Launching UnixSocket Server")
if err := restapi.UnixServer(fm.Path(filenames.Socket), 0660, api); err != nil {
log.Print(err)
}
}()
restapi.Run(c, fm.Path(filenames.Socket), api) // Launches go routines

// check limit on open files (includes tcp connections)
var rlim syscall.Rlimit
Expand Down

0 comments on commit 57c9df6

Please sign in to comment.