Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[+] add `Scheduler.status` property and `RunningStatus` constant [+] add `Scheduler.IsReady()` method to be used in REST API [+] add `RestApi` section and `--rest-port` command-line option
- Loading branch information
1 parent
4cdee9c
commit 83ac6e9
Showing
6 changed files
with
78 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/cybertec-postgresql/pg_timetable/internal/config" | ||
"github.com/cybertec-postgresql/pg_timetable/internal/log" | ||
) | ||
|
||
// StatusIface is a common interface describing the cirrent status of a connection | ||
type StatusIface interface { | ||
IsReady() bool | ||
} | ||
|
||
type RestApiServer struct { | ||
StatusReporter StatusIface | ||
l log.LoggerIface | ||
*http.Server | ||
} | ||
|
||
func Init(opts config.RestApiOpts, logger log.LoggerIface) *RestApiServer { | ||
s := &RestApiServer{ | ||
nil, | ||
logger, | ||
&http.Server{ | ||
Addr: fmt.Sprintf(":%d", opts.Port), | ||
ReadTimeout: 10 * time.Second, | ||
WriteTimeout: 10 * time.Second, | ||
MaxHeaderBytes: 1 << 20, | ||
}, | ||
} | ||
http.HandleFunc("/liveness", func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) // i'm serving hence I'm alive | ||
}) | ||
http.HandleFunc("/readiness", s.readinessHandler) | ||
if opts.Port != 0 { | ||
logger.WithField("port", opts.Port).Info("Starting REST API server...") | ||
go func() { logger.Error(s.ListenAndServe()) }() | ||
} | ||
return s | ||
} | ||
|
||
func (Server *RestApiServer) readinessHandler(w http.ResponseWriter, r *http.Request) { | ||
Server.l.Debug("Received /readiness REST API request") | ||
if Server.StatusReporter == nil || !Server.StatusReporter.IsReady() { | ||
w.WriteHeader(http.StatusServiceUnavailable) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters