mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-26 18:17:44 +00:00
feat(docker): add /healthz endpoint and -healthcheck flag (#254)
Adds a Docker healthcheck and the small HTTP/CLI plumbing it needs. - Expose `GET /healthz` on the HTTP-mode server. - Add `-healthcheck` to dial `http://127.0.0.1:<port>/healthz` and exit with status. - Add a Dockerfile `HEALTHCHECK` instruction using `-healthcheck`. - Document HTTP-mode usage and stdio override guidance. Closes https://gitea.com/gitea/gitea-mcp/issues/146 Assisted by Codet. Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/254 Reviewed-by: silverwind <2021+silverwind@noreply.gitea.com>
This commit is contained in:
+13
@@ -5,6 +5,7 @@ import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -23,9 +24,11 @@ var (
|
||||
tools string
|
||||
scopes string
|
||||
version bool
|
||||
healthcheck bool
|
||||
maxInlineAttachmentBytes int
|
||||
maxInlineAttachmentBytesFlagSet bool
|
||||
osExit = os.Exit
|
||||
healthcheckClient = http.DefaultClient
|
||||
)
|
||||
|
||||
func initFlagSet(fs *flag.FlagSet, args []string, getenv func(string) string, readFile func(string) ([]byte, error), stderr io.Writer) {
|
||||
@@ -53,6 +56,7 @@ func initFlagSet(fs *flag.FlagSet, args []string, getenv func(string) string, re
|
||||
fs.BoolVar(&flagPkg.Insecure, "insecure", false, "")
|
||||
fs.BoolVar(&version, "v", false, "")
|
||||
fs.BoolVar(&version, "version", false, "")
|
||||
fs.BoolVar(&healthcheck, "healthcheck", false, "")
|
||||
maxInlineAttachmentBytes = 5 * 1024 * 1024
|
||||
fs.Func("max-inline-attachment-bytes", "", func(val string) error {
|
||||
parsed, err := strconv.Atoi(val)
|
||||
@@ -81,6 +85,7 @@ func initFlagSet(fs *flag.FlagSet, args []string, getenv func(string) string, re
|
||||
fmt.Fprintf(w, " -k, -insecure\tIgnore TLS certificate errors\n")
|
||||
fmt.Fprintf(w, " -max-inline-attachment-bytes <bytes>\tInline image attachments up to this size (default: 5242880)\n")
|
||||
fmt.Fprintf(w, " -v, -version\tPrint version and exit\n")
|
||||
fmt.Fprintf(w, " -healthcheck\tCheck a running HTTP server's /healthz endpoint and exit\n")
|
||||
fmt.Fprintln(w)
|
||||
fmt.Fprintln(w, "Environment variables:")
|
||||
fmt.Fprintf(w, " GITEA_ACCESS_TOKEN\tProvide access token\n")
|
||||
@@ -183,6 +188,14 @@ func Execute() {
|
||||
fmt.Fprintln(os.Stdout, flagPkg.Version)
|
||||
return
|
||||
}
|
||||
if healthcheck {
|
||||
if runHealthcheck(healthcheckClient, flagPkg.Port, os.Stdout, os.Stderr) {
|
||||
osExit(0)
|
||||
} else {
|
||||
osExit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err := operation.Run(); err != nil {
|
||||
if err == context.Canceled {
|
||||
log.Info("Server shutdown due to context cancellation")
|
||||
|
||||
@@ -95,3 +95,12 @@ func TestInitFlagSetScopes(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestInitFlagSetHealthcheck(t *testing.T) {
|
||||
t.Cleanup(func() { healthcheck = false })
|
||||
fs := flag.NewFlagSet("test", flag.ContinueOnError)
|
||||
initFlagSet(fs, []string{"-healthcheck"}, func(string) string { return "" }, func(string) ([]byte, error) { return nil, nil }, &bytes.Buffer{})
|
||||
if !healthcheck {
|
||||
t.Error("healthcheck = false, want true")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// runHealthcheck dials the /healthz endpoint on 127.0.0.1:port and reports
|
||||
// success or failure. It returns true when the server responds with a 2xx
|
||||
// status.
|
||||
func runHealthcheck(client *http.Client, port int, stdout, stderr io.Writer) bool {
|
||||
url := fmt.Sprintf("http://127.0.0.1:%d/healthz", port)
|
||||
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
fmt.Fprintf(stderr, "healthcheck failed: %v\n", err)
|
||||
return false
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusMultipleChoices {
|
||||
fmt.Fprintf(stderr, "healthcheck failed: unexpected status %s\n", resp.Status)
|
||||
return false
|
||||
}
|
||||
|
||||
fmt.Fprintln(stdout, "healthy")
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRunHealthcheck(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
handler http.HandlerFunc
|
||||
port func(server *httptest.Server) int
|
||||
wantOK bool
|
||||
wantStdout string
|
||||
}{
|
||||
{
|
||||
name: "server responds 200 OK",
|
||||
handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
},
|
||||
port: serverPort,
|
||||
wantOK: true,
|
||||
wantStdout: "healthy",
|
||||
},
|
||||
{
|
||||
name: "server responds with an error status",
|
||||
handler: func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
},
|
||||
port: serverPort,
|
||||
wantOK: false,
|
||||
},
|
||||
{
|
||||
name: "nothing listening on the port",
|
||||
port: func(*httptest.Server) int { return 1 },
|
||||
wantOK: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var server *httptest.Server
|
||||
if tt.handler != nil {
|
||||
server = httptest.NewServer(tt.handler)
|
||||
defer server.Close()
|
||||
}
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
ok := runHealthcheck(http.DefaultClient, tt.port(server), &stdout, &stderr)
|
||||
|
||||
if ok != tt.wantOK {
|
||||
t.Errorf("runHealthcheck() = %v, want %v", ok, tt.wantOK)
|
||||
}
|
||||
if tt.wantStdout != "" && !strings.Contains(stdout.String(), tt.wantStdout) {
|
||||
t.Errorf("stdout = %q, want it to contain %q", stdout.String(), tt.wantStdout)
|
||||
}
|
||||
if tt.wantOK && stderr.Len() != 0 {
|
||||
t.Errorf("stderr = %q, want empty on success", stderr.String())
|
||||
}
|
||||
if !tt.wantOK && stderr.Len() == 0 {
|
||||
t.Error("stderr is empty, want a failure message")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func serverPort(server *httptest.Server) int {
|
||||
_, portStr, err := net.SplitHostPort(server.Listener.Addr().String())
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
port, err := strconv.Atoi(portStr)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return port
|
||||
}
|
||||
Reference in New Issue
Block a user