2022-05-08 09:00:22 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-09-22 09:46:36 +00:00
|
|
|
"code.icb4dc0.de/prskr/nurse/grammar"
|
2022-05-08 09:00:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Route string
|
|
|
|
|
|
|
|
func (r Route) String() string {
|
|
|
|
val := string(r)
|
|
|
|
val = strings.Trim(val, "/")
|
|
|
|
|
|
|
|
return path.Clean(fmt.Sprintf("/%s", val))
|
|
|
|
}
|
|
|
|
|
|
|
|
type EndpointSpec struct {
|
2022-08-07 10:22:21 +00:00
|
|
|
CheckTimeout time.Duration
|
|
|
|
CheckAttempts uint
|
|
|
|
Checks []grammar.Check
|
2022-05-08 09:00:22 +00:00
|
|
|
}
|
|
|
|
|
2022-05-13 13:38:19 +00:00
|
|
|
func (s EndpointSpec) Timeout(fallback time.Duration) time.Duration {
|
|
|
|
if s.CheckTimeout != 0 {
|
|
|
|
return s.CheckTimeout
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
2022-08-07 10:22:21 +00:00
|
|
|
func (s EndpointSpec) Attempts(fallback uint) uint {
|
|
|
|
if s.CheckAttempts != 0 {
|
|
|
|
return s.CheckAttempts
|
|
|
|
}
|
|
|
|
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:38:19 +00:00
|
|
|
func (s *EndpointSpec) Parse(text string) error {
|
2022-05-08 09:00:22 +00:00
|
|
|
parser, err := grammar.NewParser[grammar.Script]()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:38:19 +00:00
|
|
|
script, err := parser.Parse(text)
|
2022-05-08 09:00:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:38:19 +00:00
|
|
|
s.Checks = script.Checks
|
2022-05-08 09:00:22 +00:00
|
|
|
return nil
|
|
|
|
}
|