30 lines
997 B
Go
30 lines
997 B
Go
package resend
|
|
|
|
import (
|
|
"errors"
|
|
"net"
|
|
"net/url"
|
|
"time"
|
|
)
|
|
|
|
// NewLoopbackSender builds the transport against a Resend API fixture that
|
|
// runs on this machine, for tests of the delivery layer. It accepts only an
|
|
// http URL whose host is a loopback address, so the API key and the messages
|
|
// of a deployment can never be pointed at another host through it; the
|
|
// bootstrap code uses NewSender and its fixed endpoint.
|
|
func NewLoopbackSender(apiKey, from string, timeout time.Duration, endpoint string, now func() time.Time) (*Sender, error) {
|
|
target, err := url.Parse(endpoint)
|
|
if err != nil || target.Scheme != "http" || !isLoopbackHost(target.Hostname()) {
|
|
return nil, errors.New("resend.NewLoopbackSender - endpoint must be an http URL on a loopback address")
|
|
}
|
|
return newSender(apiKey, from, timeout, endpoint, now)
|
|
}
|
|
|
|
func isLoopbackHost(host string) bool {
|
|
if host == "localhost" {
|
|
return true
|
|
}
|
|
address := net.ParseIP(host)
|
|
return address != nil && address.IsLoopback()
|
|
}
|