add internal API & ping

This commit is contained in:
Peter Rounce 2023-02-02 09:16:23 +00:00
parent 53a8e1f581
commit 58c074234c
2 changed files with 54 additions and 10 deletions

44
main.go
View file

@ -7,8 +7,6 @@ import (
"time" "time"
) )
var router = mux.NewRouter()
func write_error(w http.ResponseWriter) { func write_error(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@ -41,26 +39,52 @@ func main() {
DisableHTMLEscape: true, DisableHTMLEscape: true,
}) })
var external_router = mux.NewRouter()
var internal_router = mux.NewRouter()
// external API
// ping
external_router.Path("/ping").Methods("GET").HandlerFunc(external_ping)
// createboltcard // createboltcard
router.Path("/new").Methods("GET").HandlerFunc(new_card_request) external_router.Path("/new").Methods("GET").HandlerFunc(new_card_request)
// lnurlw for pos // lnurlw for pos
router.Path("/ln").Methods("GET").HandlerFunc(lnurlw_response) external_router.Path("/ln").Methods("GET").HandlerFunc(lnurlw_response)
router.Path("/cb").Methods("GET").HandlerFunc(lnurlw_callback) external_router.Path("/cb").Methods("GET").HandlerFunc(lnurlw_callback)
// lnurlp for lightning address // lnurlp for lightning address
router.Path("/.well-known/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_response) external_router.Path("/.well-known/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_response)
router.Path("/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_callback) external_router.Path("/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_callback)
// internal API
// this has no authentication and is not to be exposed publicly
// it exists for use on a private virtual network within a docker container
// ping
internal_router.Path("/ping").Methods("GET").HandlerFunc(internal_ping)
//internal_router.Path("/createboltcard").Methods("POST").HandlerFunc(adminapi_createboltcard)
//internal_router.Path("/wipeboltcard").Methods("POST").HandlerFunc(adminapi_wipeboltcard)
port := db_get_setting("HOST_PORT") port := db_get_setting("HOST_PORT")
if port == "" { if port == "" {
port = "9000" port = "9000"
} }
srv := &http.Server{ external_server := &http.Server{
Handler: router, Handler: external_router,
Addr: ":" + port, // consider adding host Addr: ":" + port, // consider adding host
WriteTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second,
ReadTimeout: 30 * time.Second, ReadTimeout: 30 * time.Second,
} }
srv.ListenAndServe() internal_server := &http.Server{
Handler: internal_router,
Addr: ":9001",
WriteTimeout: 5 * time.Second,
ReadTimeout: 5 * time.Second,
}
go external_server.ListenAndServe()
go internal_server.ListenAndServe()
select {}
} }

20
ping.go Normal file
View file

@ -0,0 +1,20 @@
package main
import (
"net/http"
)
func external_ping(w http.ResponseWriter, req *http.Request) {
ping(w, "external API")
}
func internal_ping(w http.ResponseWriter, req *http.Request) {
ping(w, "internal API")
}
func ping(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK","pong":"` + message + `"}`)
w.Write(jsonData)
}