-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #379 from cybertec-postgresql/378_rest_api
[!] add REST API, resolves #378
- Loading branch information
Showing
12 changed files
with
154 additions
and
11 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
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 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,19 @@ | ||
REST API | ||
================================================ | ||
|
||
**pg_timetable** has a rich REST API, which can be used by external tools in order to perform start/stop/reinitialize/restarts/reloads, | ||
by any kind of tools to perform HTTP health checks, and of course, could also be used for monitoring. | ||
|
||
Below you will find the list of **pg_timetable** REST API endpoints. | ||
|
||
Health check endpoints | ||
------------------------------------------------ | ||
|
||
Currently, there are two health check endpoints available: | ||
|
||
``GET /liveness`` | ||
Always returns HTTP status code ``200`` what only indicates that **pg_timetable** is running. | ||
|
||
``GET /readiness`` | ||
Returns HTTP status code ``200`` when the **pg_timetable** is running and the scheduler is in the main loop processing chains. | ||
If the scheduler connects to the database, creates the database schema, or upgrades it, it will return HTTP status code ``503``. |
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
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" | ||
) | ||
|
||
// StatusReporter is a common interface describing the current status of a connection | ||
type StatusReporter interface { | ||
IsReady() bool | ||
} | ||
|
||
type RestApiServer struct { | ||
Reporter StatusReporter | ||
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.Reporter == nil || !Server.Reporter.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package api_test | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/cybertec-postgresql/pg_timetable/internal/api" | ||
"github.com/cybertec-postgresql/pg_timetable/internal/config" | ||
"github.com/cybertec-postgresql/pg_timetable/internal/log" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type reporter struct { | ||
} | ||
|
||
func (r *reporter) IsReady() bool { | ||
return true | ||
} | ||
|
||
func TestStatus(t *testing.T) { | ||
restsrv := api.Init(config.RestApiOpts{Port: 8080}, log.Init(config.LoggingOpts{LogLevel: "error"})) | ||
r, err := http.Get("http://localhost:8080/liveness") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, r.StatusCode) | ||
|
||
r, err = http.Get("http://localhost:8080/readiness") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusServiceUnavailable, r.StatusCode) | ||
|
||
restsrv.Reporter = &reporter{} | ||
r, err = http.Get("http://localhost:8080/readiness") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, r.StatusCode) | ||
} |
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 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 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