Skip to content

Commit

Permalink
server: enable debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
nalgeon committed Nov 10, 2024
1 parent 6561fca commit ccea62a
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
28 changes: 21 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,37 @@ var (

// startServer starts the HTTP API sandbox server.
func startServer(port int) *server.Server {
const host = "" // listen on all interfaces
logx.Log("codapi %s, commit %s, built at %s", version, commit, date)
logx.Log("listening on port %d...", port)
logx.Log("listening on 0.0.0.0:%d...", port)
router := server.NewRouter()
srv := server.NewServer(port, router)
srv := server.NewServer(host, port, router)
srv.Start()
return srv
}

// startDebug servers the debug handlers.
func startDebug(port int) *server.Server {
const host = "localhost"
logx.Log("debugging on localhost:%d...", port)
router := server.NewDebug()
srv := server.NewServer(host, port, router)
srv.Start()
return srv
}

// listenSignals listens for termination signals
// and performs graceful shutdown.
func listenSignals(srv *server.Server) {
func listenSignals(servers []*server.Server) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
logx.Log("stopping...")
err := srv.Stop()
if err != nil {
logx.Log("failed to stop: %v", err)
for _, srv := range servers {
err := srv.Stop()
if err != nil {
logx.Log("failed to stop: %v", err)
}
}
}

Expand All @@ -65,5 +78,6 @@ func main() {
logx.Log("boxes: %v", cfg.BoxNames())
logx.Log("commands: %v", cfg.CommandNames())

listenSignals(srv)
debug := startDebug(6060)
listenSignals([]*server.Server{srv, debug})
}
12 changes: 12 additions & 0 deletions internal/server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"net/http"
"net/http/pprof"

"github.com/nalgeon/codapi/internal/engine"
"github.com/nalgeon/codapi/internal/logx"
Expand All @@ -19,6 +20,17 @@ func NewRouter() http.Handler {
return mux
}

// NewDebug creates HTTP routes for debugging.
func NewDebug() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
return mux
}

// exec runs a sandbox command on the supplied code.
func exec(w http.ResponseWriter, r *http.Request) {
// only POST is allowed
Expand Down
4 changes: 2 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ type Server struct {
}

// NewServer creates a new Server.
func NewServer(port int, handler http.Handler) *Server {
addr := fmt.Sprintf(":%d", port)
func NewServer(host string, port int, handler http.Handler) *Server {
addr := fmt.Sprintf("%s:%d", host, port)
return &Server{
srv: &http.Server{Addr: addr, Handler: handler},
wg: &sync.WaitGroup{},
Expand Down
2 changes: 1 addition & 1 deletion internal/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func TestServer(t *testing.T) {
w.WriteHeader(http.StatusOK)
})

srv := NewServer(8585, handler)
srv := NewServer("", 8585, handler)
if srv.srv.Addr != ":8585" {
t.Fatalf("NewServer: expected port :8585 got %s", srv.srv.Addr)
}
Expand Down

0 comments on commit ccea62a

Please sign in to comment.