gex/gex_httpd/src/gex_httpd.erl
2025-09-23 16:23:35 -07:00

83 lines
1.9 KiB
Erlang

%%% @doc
%%% Gajumaru DEX HTTP Daemon
%%% @end
-module(gex_httpd).
-vsn("0.1.0").
-behavior(application).
-author("Peter Harpending <peterharpending@qpq.swiss>").
-copyright("Peter Harpending <peterharpending@qpq.swiss>").
%% for our edification
-export([listen/1, ignore/0]).
-export([start/0]).
%% erlang expects us to export these functions
-export([start/2, stop/1]).
-spec listen(PortNum) -> Result
when PortNum :: inet:port_num(),
Result :: ok
| {error, {listening, inet:port_num()}}.
%% @doc
%% Make the server start listening on a port.
%% Returns an {error, Reason} tuple if it is already listening.
listen(PortNum) ->
gh_client_man:listen(PortNum).
-spec ignore() -> ok.
%% @doc
%% Make the server stop listening if it is, or continue to do nothing if it isn't.
ignore() ->
gh_client_man:ignore().
-spec start() -> ok.
%% @doc
%% Start the server in an "ignore" state.
start() ->
ok = application:ensure_started(sasl),
ok = application:start(gex_httpd),
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(normal, term()) -> {ok, pid()}.
%% @private
%% Called by OTP to kick things off. This is for the use of the "application" part of
%% OTP, not to be called by user code.
%% See: http://erlang.org/doc/apps/kernel/application.html
start(normal, _Args) ->
Result = gh_sup:start_link(),
% auto-listen to port 8000
ok = listen(8000),
Result.
-spec stop(term()) -> ok.
%% @private
%% Similar to start/2 above, this is to be called by the "application" part of OTP,
%% not client code. Causes a (hopefully graceful) shutdown of the application.
stop(_State) ->
ok.