feat: add auto route healthcheck
This commit is contained in:
@@ -10,11 +10,13 @@ You may get a copy of the License at
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
*/
|
||||
import (
|
||||
"github.com/robfig/cron/v3"
|
||||
"net/url"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// FileExists checks if the file does exist
|
||||
@@ -122,3 +124,21 @@ func UrlParsePath(uri string) string {
|
||||
func HasWhitespace(s string) bool {
|
||||
return regexp.MustCompile(`\s`).MatchString(s)
|
||||
}
|
||||
|
||||
// IsValidCronExpression verify cronExpression and returns boolean
|
||||
func IsValidCronExpression(cronExpr string) bool {
|
||||
// Parse the cron expression
|
||||
_, err := cron.ParseStandard(cronExpr)
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func ParseDuration(durationStr string) (time.Duration, error) {
|
||||
if durationStr == "" {
|
||||
return 0, nil
|
||||
}
|
||||
duration, err := time.ParseDuration(durationStr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return duration, nil
|
||||
}
|
||||
|
||||
@@ -1 +1,35 @@
|
||||
package util
|
||||
|
||||
import (
|
||||
"log"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestConExpression(t *testing.T) {
|
||||
cronExpression := "@every 30s"
|
||||
if !IsValidCronExpression(cronExpression) {
|
||||
t.Fatal("Cron expression should be valid")
|
||||
}
|
||||
log.Println(" Cron is valid")
|
||||
|
||||
}
|
||||
|
||||
func TestParseDuration(t *testing.T) {
|
||||
d1, err1 := ParseDuration("20s")
|
||||
if err1 != nil {
|
||||
t.Error("Error:", err1)
|
||||
} else {
|
||||
log.Printf("Parsed duration: %d", d1)
|
||||
log.Printf("Time out: %s\n", time.Now().Add(d1))
|
||||
|
||||
}
|
||||
d2, err2 := ParseDuration("10m")
|
||||
if err2 != nil {
|
||||
t.Errorf("Error: %v", err2)
|
||||
} else {
|
||||
log.Printf("Parsed duration: %d\n", d2)
|
||||
log.Printf("Time out: %s\n", time.Now().Add(d2))
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user