-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,4 +25,6 @@ RUN apk add --no-cache tzdata | |
|
||
EXPOSE 1025/tcp 1110/tcp 8025/tcp | ||
|
||
HEALTHCHECK --interval=15s CMD /mailpit readyz | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
axllent
Author
Owner
|
||
|
||
ENTRYPOINT ["/mailpit"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"path" | ||
"strings" | ||
"time" | ||
|
||
"github.com/axllent/mailpit/config" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
useHTTPS bool | ||
) | ||
|
||
// readyzCmd represents the healthcheck command | ||
var readyzCmd = &cobra.Command{ | ||
Use: "readyz", | ||
Short: "Run a healthcheck to test if Mailpit is running", | ||
Long: `This command connects to the /readyz endpoint of a running Mailpit server | ||
and exits with a status of 0 if the connection is successful, else with a | ||
status 1 if unhealthy. | ||
If running within Docker, it should automatically detect environment | ||
settings to determine the HTTP bind interface & port. | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
webroot := strings.TrimRight(path.Join("/", config.Webroot, "/"), "/") + "/" | ||
proto := "http" | ||
if useHTTPS { | ||
proto = "https" | ||
} | ||
|
||
uri := fmt.Sprintf("%s://%s%sreadyz", proto, config.HTTPListen, webroot) | ||
|
||
conf := &http.Transport{ | ||
IdleConnTimeout: time.Second * 5, | ||
ExpectContinueTimeout: time.Second * 5, | ||
TLSHandshakeTimeout: time.Second * 5, | ||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, | ||
} | ||
client := &http.Client{Transport: conf} | ||
|
||
res, err := client.Get(uri) | ||
if err != nil || res.StatusCode != 200 { | ||
os.Exit(1) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(readyzCmd) | ||
|
||
if len(os.Getenv("MP_UI_BIND_ADDR")) > 0 { | ||
config.HTTPListen = os.Getenv("MP_UI_BIND_ADDR") | ||
} | ||
|
||
if len(os.Getenv("MP_WEBROOT")) > 0 { | ||
config.Webroot = os.Getenv("MP_WEBROOT") | ||
} | ||
|
||
config.UITLSCert = os.Getenv("MP_UI_TLS_CERT") | ||
|
||
if config.UITLSCert != "" { | ||
useHTTPS = true | ||
} | ||
|
||
readyzCmd.Flags().StringVarP(&config.HTTPListen, "listen", "l", config.HTTPListen, "Set the HTTP bind interface & port") | ||
readyzCmd.Flags().StringVar(&config.Webroot, "webroot", config.Webroot, "Set the webroot for web UI & API") | ||
readyzCmd.Flags().BoolVar(&useHTTPS, "https", useHTTPS, "Connect via HTTPS (ignores HTTPS validation)") | ||
} |
Maybe this could be reduced a bit? Currently it will take 15 seconds until this service is marked
healthy
, thus a service whichdepends_on
(Compose) this, will need to wait at least 15 seconds.Also consider adding
--start-period=Xs
(default:0s
) if you know roughly how long Mailpit needs to start. It may also make sense to adjust--start-interval=Ys
(default:5s
) if the startup should be quick but may take 1-2 seconds longer.In any case thanks a lot for adding this!