mirror of
https://github.com/safing/portbase
synced 2025-04-16 23:49:09 +00:00
Add switch to enable/disable API HTTP server
This commit is contained in:
parent
1ae8c0698e
commit
ca8c784c23
2 changed files with 32 additions and 10 deletions
|
@ -1,7 +1,6 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
|
@ -58,7 +57,7 @@ func prep() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func start() error {
|
func start() error {
|
||||||
go Serve()
|
startServer()
|
||||||
|
|
||||||
_ = updateAPIKeys(module.Ctx, nil)
|
_ = updateAPIKeys(module.Ctx, nil)
|
||||||
err := module.RegisterEventHook("config", "config change", "update API keys", updateAPIKeys)
|
err := module.RegisterEventHook("config", "config change", "update API keys", updateAPIKeys)
|
||||||
|
@ -75,10 +74,7 @@ func start() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func stop() error {
|
func stop() error {
|
||||||
if server != nil {
|
return stopServer()
|
||||||
return server.Shutdown(context.Background())
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func exportEndpointsCmd() error {
|
func exportEndpointsCmd() error {
|
||||||
|
|
|
@ -18,6 +18,9 @@ import (
|
||||||
"github.com/safing/portbase/utils"
|
"github.com/safing/portbase/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// EnableServer defines if the HTTP server should be started.
|
||||||
|
const EnableServer = true
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// mainMux is the main mux router.
|
// mainMux is the main mux router.
|
||||||
mainMux = mux.NewRouter()
|
mainMux = mux.NewRouter()
|
||||||
|
@ -48,15 +51,38 @@ func RegisterHandleFunc(path string, handleFunc func(http.ResponseWriter, *http.
|
||||||
return mainMux.HandleFunc(path, handleFunc)
|
return mainMux.HandleFunc(path, handleFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serve starts serving the API endpoint.
|
func startServer() {
|
||||||
func Serve() {
|
// Check if server is enabled.
|
||||||
// configure server
|
if !EnableServer {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Configure server.
|
||||||
server.Addr = listenAddressConfig()
|
server.Addr = listenAddressConfig()
|
||||||
server.Handler = &mainHandler{
|
server.Handler = &mainHandler{
|
||||||
// TODO: mainMux should not be modified anymore.
|
// TODO: mainMux should not be modified anymore.
|
||||||
mux: mainMux,
|
mux: mainMux,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Start server manager.
|
||||||
|
module.StartServiceWorker("http server manager", 0, serverManager)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stopServer() error {
|
||||||
|
// Check if server is enabled.
|
||||||
|
if !EnableServer {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if server.Addr != "" {
|
||||||
|
return server.Shutdown(context.Background())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve starts serving the API endpoint.
|
||||||
|
func serverManager(_ context.Context) error {
|
||||||
// start serving
|
// start serving
|
||||||
log.Infof("api: starting to listen on %s", server.Addr)
|
log.Infof("api: starting to listen on %s", server.Addr)
|
||||||
backoffDuration := 10 * time.Second
|
backoffDuration := 10 * time.Second
|
||||||
|
@ -67,7 +93,7 @@ func Serve() {
|
||||||
})
|
})
|
||||||
// return on shutdown error
|
// return on shutdown error
|
||||||
if errors.Is(err, http.ErrServerClosed) {
|
if errors.Is(err, http.ErrServerClosed) {
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
// log error and restart
|
// log error and restart
|
||||||
log.Errorf("api: http endpoint failed: %s - restarting in %s", err, backoffDuration)
|
log.Errorf("api: http endpoint failed: %s - restarting in %s", err, backoffDuration)
|
||||||
|
|
Loading…
Add table
Reference in a new issue