-
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.
- Loading branch information
Showing
6 changed files
with
86 additions
and
7 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
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 current 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 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