mirror of
https://github.com/fnproject/fn.git
synced 2022-10-28 21:29:17 +03:00
we had this _almost_ right, in that we were trying, but we weren't masking the error from the user response for any error we don't intend to show. this also adds a stack trace from any internal server errors, so that we might be able to track them down in the future (looking at you, 'context deadline exceeded'). in addition, this adds a new `models.APIError` interface which all of the errors in `models` now implement, and can be caught easily / added to easily. the front end now does no status rewriting based on api errors, now when we get a non-nil error we can call `handleResponse(c, err)` with it and if it's a proper error, return it to the user with the right status code, otherwise log a stack trace and return `internal server error`. this cleans up a lot of the front end code. also rewrites start task ctx deadline exceeded as timeout. with iw we had async tasks so we could start the clock later and it didn't matter, but now with sync tasks time out sometimes just making docker calls, and we want the task status to show up as timed out. we may want to just catch all this above in addition to this, but this seems like the right thing to do. remove squishing together errors. this was weird, now we return the first error for the purposes of using the new err interface. removed a lot of 5xx errors that really should have been 4xx errors. changed some of the 400 errors to 409 errors, since they are from sending in conflicting info and not a malformed request. removed unused errors / useless errors (many were used for logging, and didn't provide any context. now with stack traces we don't need context as much in the logs).
179 lines
3.3 KiB
Go
179 lines
3.3 KiB
Go
package models
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"path"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
defaultRouteTimeout = 30 // seconds
|
|
htfnScaleDownTimeout = 30 // seconds
|
|
)
|
|
|
|
type Routes []*Route
|
|
|
|
type Route struct {
|
|
AppName string `json:"app_name"`
|
|
Path string `json:"path"`
|
|
Image string `json:"image"`
|
|
Memory uint64 `json:"memory"`
|
|
Headers http.Header `json:"headers"`
|
|
Type string `json:"type"`
|
|
Format string `json:"format"`
|
|
Timeout int32 `json:"timeout"`
|
|
IdleTimeout int32 `json:"idle_timeout"`
|
|
Config `json:"config"`
|
|
}
|
|
|
|
// SetDefaults sets zeroed field to defaults.
|
|
func (r *Route) SetDefaults() {
|
|
if r.Memory == 0 {
|
|
r.Memory = 128
|
|
}
|
|
|
|
if r.Type == TypeNone {
|
|
r.Type = TypeSync
|
|
}
|
|
|
|
if r.Format == "" {
|
|
r.Format = FormatDefault
|
|
}
|
|
|
|
if r.Headers == nil {
|
|
r.Headers = http.Header{}
|
|
}
|
|
|
|
if r.Config == nil {
|
|
r.Config = map[string]string{}
|
|
}
|
|
|
|
if r.Timeout == 0 {
|
|
r.Timeout = defaultRouteTimeout
|
|
}
|
|
|
|
if r.IdleTimeout == 0 {
|
|
r.IdleTimeout = htfnScaleDownTimeout
|
|
}
|
|
}
|
|
|
|
// Validate validates field values, skipping zeroed fields if skipZero is true.
|
|
// it returns the first error, if any.
|
|
func (r *Route) Validate(skipZero bool) error {
|
|
if !skipZero {
|
|
if r.AppName == "" {
|
|
return ErrRoutesValidationMissingAppName
|
|
}
|
|
|
|
if r.Path == "" {
|
|
return ErrRoutesValidationMissingPath
|
|
}
|
|
|
|
if r.Image == "" {
|
|
return ErrRoutesValidationMissingImage
|
|
}
|
|
}
|
|
|
|
if !skipZero || r.Path != "" {
|
|
u, err := url.Parse(r.Path)
|
|
if err != nil {
|
|
return ErrRoutesValidationPathMalformed
|
|
}
|
|
|
|
if strings.Contains(u.Path, ":") {
|
|
return ErrRoutesValidationFoundDynamicURL
|
|
}
|
|
|
|
if !path.IsAbs(u.Path) {
|
|
return ErrRoutesValidationInvalidPath
|
|
}
|
|
}
|
|
|
|
if !skipZero || r.Type != "" {
|
|
if r.Type != TypeAsync && r.Type != TypeSync {
|
|
return ErrRoutesValidationInvalidType
|
|
}
|
|
}
|
|
|
|
if !skipZero || r.Format != "" {
|
|
if r.Format != FormatDefault && r.Format != FormatHTTP {
|
|
return ErrRoutesValidationInvalidFormat
|
|
}
|
|
}
|
|
|
|
if r.Timeout < 0 {
|
|
return ErrRoutesValidationNegativeTimeout
|
|
}
|
|
|
|
if r.IdleTimeout < 0 {
|
|
return ErrRoutesValidationNegativeIdleTimeout
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (r *Route) Clone() *Route {
|
|
var clone Route
|
|
clone.AppName = r.AppName
|
|
clone.Path = r.Path
|
|
clone.Update(r)
|
|
return &clone
|
|
}
|
|
|
|
// Update updates fields in r with non-zero field values from new.
|
|
// 0-length slice Header values, and empty-string Config values trigger removal of map entry.
|
|
func (r *Route) Update(new *Route) {
|
|
if new.Image != "" {
|
|
r.Image = new.Image
|
|
}
|
|
if new.Memory != 0 {
|
|
r.Memory = new.Memory
|
|
}
|
|
if new.Type != "" {
|
|
r.Type = new.Type
|
|
}
|
|
if new.Timeout != 0 {
|
|
r.Timeout = new.Timeout
|
|
}
|
|
if new.IdleTimeout != 0 {
|
|
r.IdleTimeout = new.IdleTimeout
|
|
}
|
|
if new.Format != "" {
|
|
r.Format = new.Format
|
|
}
|
|
if new.Headers != nil {
|
|
if r.Headers == nil {
|
|
r.Headers = make(http.Header)
|
|
}
|
|
for k, v := range new.Headers {
|
|
if len(v) == 0 {
|
|
r.Headers.Del(k)
|
|
} else {
|
|
for _, val := range v {
|
|
r.Headers.Add(k, val)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if new.Config != nil {
|
|
if r.Config == nil {
|
|
r.Config = make(Config)
|
|
}
|
|
for k, v := range new.Config {
|
|
if v == "" {
|
|
delete(r.Config, k)
|
|
} else {
|
|
r.Config[k] = v
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//TODO are these sql LIKE queries? or strict matches?
|
|
type RouteFilter struct {
|
|
Path string
|
|
AppName string
|
|
Image string
|
|
}
|