Skip to content

Commit 11ae206

Browse files
committed
feat(api): separate internal api function
1 parent 057bd4e commit 11ae206

File tree

6 files changed

+78
-28
lines changed

6 files changed

+78
-28
lines changed

cmd/server/main.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,21 @@ func main() {
145145
panic(err)
146146
}
147147

148+
if err := container.Provide(server.NewAPI); err != nil {
149+
panic(err)
150+
}
151+
148152
if err := container.Provide(ProvideOksusuHandler); err != nil {
149153
panic(err)
150154
}
151155

152-
if err := container.Invoke(func(cfg *config.Config, router server.Router, handler app.OksusuHandler) error {
156+
if err := container.Invoke(func(cfg *config.Config, router server.Router, handler app.OksusuHandler, api *server.API) error {
157+
go func() {
158+
if err := api.ListenAndServe(cfg.Server.Host + ":" + cfg.API.Port); err != nil {
159+
panic(err)
160+
}
161+
}()
162+
153163
if cfg.Oksusu.Enabled {
154164
// Oksu Connect Mode
155165
if cfg.Oksusu.Token == "" {

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ require (
1515
github.com/btcsuite/btcd/btcec/v2 v2.3.5 // indirect
1616
github.com/btcsuite/btcd/btcutil v1.1.6 // indirect
1717
github.com/btcsuite/btcd/chaincfg/chainhash v1.1.0 // indirect
18-
github.com/bytedance/sonic v1.13.1 // indirect
19-
github.com/bytedance/sonic/loader v0.2.4 // indirect
20-
github.com/cloudwego/base64x v0.1.5 // indirect
21-
github.com/coder/websocket v1.8.12 // indirect
18+
github.com/bytedance/sonic v1.14.0 // indirect
19+
github.com/bytedance/sonic/loader v0.3.0 // indirect
20+
github.com/cloudwego/base64x v0.1.6 // indirect
21+
github.com/coder/websocket v1.8.13 // indirect
2222
github.com/davecgh/go-spew v1.1.1 // indirect
2323
github.com/decred/dcrd/crypto/blake256 v1.1.0 // indirect
2424
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.4.0 // indirect
2525
github.com/josharian/intern v1.0.0 // indirect
2626
github.com/json-iterator/go v1.1.12 // indirect
27-
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
27+
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
2828
github.com/kr/pretty v0.3.1 // indirect
2929
github.com/mailru/easyjson v0.9.0 // indirect
3030
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
@@ -36,10 +36,10 @@ require (
3636
github.com/tidwall/match v1.1.1 // indirect
3737
github.com/tidwall/pretty v1.2.1 // indirect
3838
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
39-
golang.org/x/arch v0.15.0 // indirect
40-
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
39+
golang.org/x/arch v0.20.0 // indirect
40+
golang.org/x/exp v0.0.0-20250813145105-42675adae3e6 // indirect
4141
golang.org/x/net v0.41.0 // indirect
42-
golang.org/x/sys v0.33.0 // indirect
42+
golang.org/x/sys v0.35.0 // indirect
4343
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
4444
gopkg.in/yaml.v3 v3.0.1 // indirect
4545
)

go.sum

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBA
44
github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ=
55
github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M=
66
github.com/btcsuite/btcd v0.23.5-0.20231215221805-96c9fd8078fd/go.mod h1:nm3Bko6zh6bWP60UxwoT5LzdGJsQJaPo6HjduXq9p6A=
7+
github.com/btcsuite/btcd v0.24.2 h1:aLmxPguqxza+4ag8R1I2nnJjSu2iFn/kqtHTIImswcY=
78
github.com/btcsuite/btcd v0.24.2/go.mod h1:5C8ChTkl5ejr3WHj8tkQSCmydiMEPB0ZhQhehpq7Dgg=
89
github.com/btcsuite/btcd/btcec/v2 v2.1.0/go.mod h1:2VzYrv4Gm4apmbVVsSq5bqf1Ec8v56E48Vt0Y/umPgA=
910
github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE=
@@ -29,14 +30,22 @@ github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtE
2930
github.com/btcsuite/winsvc v1.0.0/go.mod h1:jsenWakMcC0zFBFurPLEAyrnc/teJEM1O46fmI40EZs=
3031
github.com/bytedance/sonic v1.13.1 h1:Jyd5CIvdFnkOWuKXr+wm4Nyk2h0yAFsr8ucJgEasO3g=
3132
github.com/bytedance/sonic v1.13.1/go.mod h1:o68xyaF9u2gvVBuGHPlUVCy+ZfmNNO5ETf1+KgkJhz4=
33+
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=
34+
github.com/bytedance/sonic v1.14.0/go.mod h1:WoEbx8WTcFJfzCe0hbmyTGrfjt8PzNEBdxlNUO24NhA=
3235
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
3336
github.com/bytedance/sonic/loader v0.2.4 h1:ZWCw4stuXUsn1/+zQDqeE7JKP+QO47tz7QCNan80NzY=
3437
github.com/bytedance/sonic/loader v0.2.4/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
38+
github.com/bytedance/sonic/loader v0.3.0 h1:dskwH8edlzNMctoruo8FPTJDF3vLtDT0sXZwvZJyqeA=
39+
github.com/bytedance/sonic/loader v0.3.0/go.mod h1:N8A3vUdtUebEY2/VQC0MyhYeKUFosQU6FxH2JmUe6VI=
3540
github.com/cloudwego/base64x v0.1.5 h1:XPciSp1xaq2VCSt6lF0phncD4koWyULpl5bUxbfCyP4=
3641
github.com/cloudwego/base64x v0.1.5/go.mod h1:0zlkT4Wn5C6NdauXdJRhSKRlJvmclQ1hhJgA0rcu/8w=
42+
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
43+
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
3744
github.com/cloudwego/iasm v0.2.0/go.mod h1:8rXZaNYT2n95jn+zTI1sDr+IgcD2GVs0nlbbQPiEFhY=
3845
github.com/coder/websocket v1.8.12 h1:5bUXkEPPIbewrnkU8LTCLVaxi4N4J8ahufH2vlo4NAo=
3946
github.com/coder/websocket v1.8.12/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
47+
github.com/coder/websocket v1.8.13 h1:f3QZdXy7uGVz+4uCJy2nTZyM0yTBj8yANEHhqlXZ9FE=
48+
github.com/coder/websocket v1.8.13/go.mod h1:LNVeNrXQZfe5qhS9ALED3uA+l5pPqvwXg3CKoDBB2gs=
4049
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
4150
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4251
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -81,6 +90,8 @@ github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23/go.mod h1:J+Gs4SYgM6
8190
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
8291
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
8392
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
93+
github.com/klauspost/cpuid/v2 v2.3.0 h1:S4CRMLnYUhGeDFDqkGriYKdfoFlDnMtqTiI/sFzhA9Y=
94+
github.com/klauspost/cpuid/v2 v2.3.0/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
8495
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
8596
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
8697
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -141,11 +152,15 @@ go.uber.org/dig v1.19.0 h1:BACLhebsYdpQ7IROQ1AGPjrXcP5dF80U3gKoFzbaq/4=
141152
go.uber.org/dig v1.19.0/go.mod h1:Us0rSJiThwCv2GteUN0Q7OKvU7n5J4dxZ9JKUXozFdE=
142153
golang.org/x/arch v0.15.0 h1:QtOrQd0bTUnhNVNndMpLHNWrDmYzZ2KDqSrEymqInZw=
143154
golang.org/x/arch v0.15.0/go.mod h1:JmwW7aLIoRUKgaTzhkiEFxvcEiQGyOg9BMonBJUS7EE=
155+
golang.org/x/arch v0.20.0 h1:dx1zTU0MAE98U+TQ8BLl7XsJbgze2WnNKF/8tGp/Q6c=
156+
golang.org/x/arch v0.20.0/go.mod h1:bdwinDaKcfZUGpH09BB7ZmOfhalA8lQdzl62l8gGWsk=
144157
golang.org/x/crypto v0.0.0-20170930174604-9419663f5a44/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
145158
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
146159
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
147160
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
148161
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
162+
golang.org/x/exp v0.0.0-20250813145105-42675adae3e6 h1:SbTAbRFnd5kjQXbczszQ0hdk3ctwYf3qBNH9jIsGclE=
163+
golang.org/x/exp v0.0.0-20250813145105-42675adae3e6/go.mod h1:4QTo5u+SEIbbKW1RacMZq1YEfOBqeXa19JeshGi+zc4=
149164
golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
150165
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
151166
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -165,6 +180,8 @@ golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7w
165180
golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
166181
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
167182
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
183+
golang.org/x/sys v0.35.0 h1:vz1N37gP5bs89s7He8XuIYXpyY0+QlsKmzipCbUtyxI=
184+
golang.org/x/sys v0.35.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
168185
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
169186
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
170187
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=

internal/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ type Config struct {
5858
ConfigFile string `short:"c" long:"config" description:"Path to config file" default:"lmt.conf" env:"LMT_CONFIG_FILE"`
5959
General GeneralConfig `group:"General" namespace:"general"`
6060
Server ServerConfig `group:"Server" namespace:"server"`
61+
API APIConfig `group:"API" namespace:"api"`
6162
LND LNDConfig `group:"LND" namespace:"lnd"`
6263
Nostr NostrConfig `group:"Nostr" namespace:"nostr"`
6364
LNURL LNURLConfig `group:"LNURL" namespace:"lnurl"`
@@ -73,6 +74,10 @@ type ServerConfig struct {
7374
Port string `long:"port" env:"SERVER_PORT" description:"Server port"`
7475
}
7576

77+
type APIConfig struct {
78+
Port string `long:"api_port" env:"API_PORT" description:"API port" default:"5051"`
79+
}
80+
7681
type LNURLConfig struct {
7782
Domain string `long:"domain" env:"DOMAIN" description:"Domain for the LNURL" required:"true"`
7883
MinSendableMsat int64 `long:"min-sendable" env:"MIN_SENDABLE_MSAT" description:"Minimum sendable amount in msats" default:"1000"`

internal/server/api.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package server
2+
3+
import (
4+
"log/slog"
5+
"net/http"
6+
"os"
7+
"time"
8+
)
9+
10+
// API provides an HTTP server for administrative tasks, like stopping the application.
11+
type API struct{}
12+
13+
// NewAPI creates a new API server instance.
14+
func NewAPI() *API {
15+
return &API{}
16+
}
17+
18+
// ListenAndServe starts the API server on the given address.
19+
func (a *API) ListenAndServe(addr string) error {
20+
mux := http.NewServeMux()
21+
mux.HandleFunc("/api/stop", a.stop)
22+
slog.Info("Starting API server", "addr", addr)
23+
return http.ListenAndServe(addr, mux)
24+
}
25+
26+
// stop handles the /api/stop request, shutting down the application.
27+
func (a *API) stop(w http.ResponseWriter, req *http.Request) {
28+
slog.Info("Received stop request from API")
29+
w.WriteHeader(http.StatusOK)
30+
_, _ = w.Write([]byte("Application is shutting down..."))
31+
32+
go func() {
33+
time.Sleep(1 * time.Second) // Give response time to send
34+
os.Exit(0)
35+
}()
36+
}

internal/server/router.go

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@ import (
44
"lmt/internal/app"
55
"log/slog"
66
"net/http"
7-
"os"
8-
"time"
97
)
108

119
type Router struct {
1210
lnurlInvoiceHandler app.LNURLInvoiceHandler
1311
lnurlHandler app.LNURLHandler
1412
nostrHandler app.NostrHandler
15-
server *http.Server
1613
}
1714

1815
func NewRouter(lnurlInvoiceHandler app.LNURLInvoiceHandler, lnurlHandler app.LNURLHandler, nostrHandler app.NostrHandler) Router {
@@ -28,25 +25,10 @@ func (r Router) ServeMux() *http.ServeMux {
2825
mux.HandleFunc("/.well-known/lnurlp/{user}", r.lnurlHandler.Handle)
2926
mux.HandleFunc("/.well-known/nostr.json", r.nostrHandler.Handle)
3027
mux.HandleFunc("/.well-known/lnurlp/{user}/callback", r.lnurlInvoiceHandler.Handle)
31-
mux.HandleFunc("/api/stop", r.Stop)
3228
return mux
3329
}
3430

3531
func (r Router) ListenAndServe(addr string) error {
3632
slog.Info("Listening on", "addr", addr)
37-
r.server = &http.Server{
38-
Addr: addr,
39-
Handler: r.ServeMux(),
40-
}
41-
return r.server.ListenAndServe()
42-
}
43-
44-
func (r Router) Stop(w http.ResponseWriter, req *http.Request) {
45-
slog.Info("Received stop request, shutting down server...")
46-
w.WriteHeader(http.StatusOK)
47-
go func() {
48-
time.Sleep(2 * time.Second)
49-
slog.Info("Shutting down server...")
50-
os.Exit(0)
51-
}()
33+
return http.ListenAndServe(addr, r.ServeMux())
5234
}

0 commit comments

Comments
 (0)