From 2037444f54d8f95a959c7e67673d55782fad5ad3 Mon Sep 17 00:00:00 2001 From: Peter Harpending Date: Tue, 23 Sep 2025 16:23:35 -0700 Subject: [PATCH] auto-listen to port 8000 --- README.md | 66 +++++++++++++++++++++++++++++++++++++ gex_httpd/src/gex_httpd.erl | 32 ++++++++++-------- 2 files changed, 85 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0e9c195..a7b132a 100644 --- a/README.md +++ b/README.md @@ -563,3 +563,69 @@ start_acceptor(ListenSocket) -> - Everything else is boilerplate - So our task is to remove the relay-messages logic, and replace it with http parse/respond logic. + + +### Auto-listening to port `8000` + +- Start commit: `9c5b332e009b88a9400a4e7008f760ce872b810a` + +```diff +diff --git a/gex_httpd/src/gex_httpd.erl b/gex_httpd/src/gex_httpd.erl +index 242c82f..3a724b0 100644 +--- a/gex_httpd/src/gex_httpd.erl ++++ b/gex_httpd/src/gex_httpd.erl +@@ -9,8 +9,11 @@ + -copyright("Peter Harpending "). + + ++%% for our edification + -export([listen/1, ignore/0]). +--export([start/0, start/1]). ++-export([start/0]). ++ ++%% erlang expects us to export these functions + -export([start/2, stop/1]). + + +@@ -45,17 +48,17 @@ start() -> + io:format("Starting..."). + + +--spec start(PortNum) -> ok +- when PortNum :: inet:port_number(). +-%% @doc +-%% Start the server and begin listening immediately. Slightly more convenient when +-%% playing around in the shell. +- +-start(PortNum) -> +- ok = start(), +- ok = gh_client_man:listen(PortNum), +- io:format("Startup complete, listening on ~w~n", [PortNum]). +- ++%-spec start(PortNum) -> ok ++% when PortNum :: inet:port_number(). ++%%% @doc ++%%% Start the server and begin listening immediately. Slightly more convenient when ++%%% playing around in the shell. ++% ++%start(PortNum) -> ++% ok = start(), ++% ok = gh_client_man:listen(PortNum), ++% io:format("Startup complete, listening on ~w~n", [PortNum]). ++% + + -spec start(normal, term()) -> {ok, pid()}. + %% @private +@@ -64,7 +67,10 @@ start(PortNum) -> + %% See: http://erlang.org/doc/apps/kernel/application.html + + start(normal, _Args) -> +- gh_sup:start_link(). ++ Result = gh_sup:start_link(), ++ % auto-listen to port 8000 ++ ok = listen(8000), ++ Result. + + + -spec stop(term()) -> ok. +``` diff --git a/gex_httpd/src/gex_httpd.erl b/gex_httpd/src/gex_httpd.erl index 242c82f..3a724b0 100644 --- a/gex_httpd/src/gex_httpd.erl +++ b/gex_httpd/src/gex_httpd.erl @@ -9,8 +9,11 @@ -copyright("Peter Harpending "). +%% for our edification -export([listen/1, ignore/0]). --export([start/0, start/1]). +-export([start/0]). + +%% erlang expects us to export these functions -export([start/2, stop/1]). @@ -45,17 +48,17 @@ start() -> io:format("Starting..."). --spec start(PortNum) -> ok - when PortNum :: inet:port_number(). -%% @doc -%% Start the server and begin listening immediately. Slightly more convenient when -%% playing around in the shell. - -start(PortNum) -> - ok = start(), - ok = gh_client_man:listen(PortNum), - io:format("Startup complete, listening on ~w~n", [PortNum]). - +%-spec start(PortNum) -> ok +% when PortNum :: inet:port_number(). +%%% @doc +%%% Start the server and begin listening immediately. Slightly more convenient when +%%% playing around in the shell. +% +%start(PortNum) -> +% ok = start(), +% ok = gh_client_man:listen(PortNum), +% io:format("Startup complete, listening on ~w~n", [PortNum]). +% -spec start(normal, term()) -> {ok, pid()}. %% @private @@ -64,7 +67,10 @@ start(PortNum) -> %% See: http://erlang.org/doc/apps/kernel/application.html start(normal, _Args) -> - gh_sup:start_link(). + Result = gh_sup:start_link(), + % auto-listen to port 8000 + ok = listen(8000), + Result. -spec stop(term()) -> ok.