Skip to content

Commit

Permalink
restapi cosmetic refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperBuker authored and a3s7p committed Oct 6, 2022
1 parent 8779e11 commit 5cc2ede
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 29 deletions.
56 changes: 28 additions & 28 deletions restapi/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,25 @@ func (t *T) reply(w http.ResponseWriter, x interface{}) {
w.Write(b) //err to check
}

func New(manager *contractmanager.Manager) (t *T) {
t = &T{
manager: manager,
l: log.Default(),
mux: http.NewServeMux(),
}
func (t *T) Run(cfg relaycfg.RestApi) {
if cfg.Address == nil {
// Not defined, pass
} else if cfg.Address.Scheme == "http" {
log.Printf("Launching HTTP Server: %s\n", cfg.Address.Host)
if err := t.TCPServer(cfg.Address.Host); err != nil {
log.Print(err)
}
} else if cfg.Address.Scheme == "file" {
os.RemoveAll(cfg.Address.Path)

t.mux.Handle("/api/status", provide.MethodGate(provide.Routes{http.MethodGet: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
o := t.manager.Status()
t.reply(w, o)
})}))
return
log.Printf("Launching UnixSocket Server: %s\n", cfg.Address.Path)
if err := t.UnixServer(cfg.Address.Path, os.FileMode(cfg.Umask)); err != nil {
log.Print(err)
}
}
}

func UnixServer(path string, fm os.FileMode, t *T) error {
func (t *T) UnixServer(path string, fm os.FileMode) error {
if err := os.RemoveAll(path); err != nil {
return err
}
Expand All @@ -64,7 +68,7 @@ func UnixServer(path string, fm os.FileMode, t *T) error {
return h.Serve(l)
}

func TCPServer(addr string, t *T) error {
func (t *T) TCPServer(addr string) error {
l, err := net.Listen("tcp", addr)
if err != nil {
return err
Expand All @@ -74,20 +78,16 @@ func TCPServer(addr string, t *T) error {
return h.Serve(l)
}

func Run(cfg relaycfg.RestApi, t *T) {
if cfg.Address == nil {
// Not defined, pass
} else if cfg.Address.Scheme == "http" {
log.Printf("Launching HTTP Server: %s\n", cfg.Address.Host)
if err := TCPServer(cfg.Address.Host, t); err != nil {
log.Print(err)
}
} else if cfg.Address.Scheme == "file" {
os.RemoveAll(cfg.Address.Path)

log.Printf("Launching UnixSocket Server: %s\n", cfg.Address.Path)
if err := UnixServer(cfg.Address.Path, os.FileMode(cfg.Umask), t); err != nil {
log.Print(err)
}
func New(manager *contractmanager.Manager) (t *T) {
t = &T{
manager: manager,
l: log.Default(),
mux: http.NewServeMux(),
}

t.mux.Handle("/api/status", provide.MethodGate(provide.Routes{http.MethodGet: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
o := t.manager.Status()
t.reply(w, o)
})}))
return
}
2 changes: 1 addition & 1 deletion sub/startcmd/startcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func serverun(fm fsdir.T) {

// Launch API REST goroutine
api := restapi.New(r.Manager)
go restapi.Run(c.RestApi, api)
go api.Run(c.RestApi)

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

0 comments on commit 5cc2ede

Please sign in to comment.