pomme/internal/api/types.go
2023-05-28 01:36:23 -04:00

79 lines
2 KiB
Go

package api
import (
"net/http"
"gorm.io/gorm"
)
const (
keyPrincipalContextID key = iota
keyLoggerContextID
)
type key int
// ZoneRequest represents a new zone file request.
//
// Inside it is a pointer to the zone struct, which contains zone file information, and a User field to keep track of whom owns the file/request.
type ZoneRequest struct {
*Zone
User string `json:"user,omitempty" gorm:"foreignKey:username;references:User"`
}
// Zone struct represents a zone file.
type Zone struct {
gorm.Model
// FileName is the file name for an uploaded zone file how it is expected to show up on the filesystem
FileName string `json:"name"`
// Body is the bytes array of a zone files body for copying and moving it around
Body []byte `json:"body,omitempty"`
}
// ndr is an interface for new DNS requests. It's methods can be used with a ZoneRequest object.
type ndr interface {
// parse() is a wrapper around miekg's NewZoneParser, which is used to validate uploaded zone files
//
// if no error is raised the zone file can be saved.
parse() error
// save() is a wrapper around internal.makeLocal() which will save a valid non-empty zone file to the filesystem
//
// the file is saved to a location the user sets up in their config.yaml file,
// once saved it can be consumed by something like NSD.
save() error
}
var _ ndr = (*ZoneRequest)(nil)
// HTTPLogger interface handles logging and sending responses to the user.
type HTTPLogger[T Response] interface {
newLogEntry() *Responder
apiError(T, http.ResponseWriter, *http.Request)
panicLogger(T)
infoLogger(T)
errorLogger(T)
}
// Response is a generic response struct containing the necessary keys for a log and response action.
type Response struct {
Message string
Err string
Realm string
Status int
}
type Responder struct {
Response Response
}
var _ HTTPLogger[Response] = (*Responder)(nil)
// newResponder instantiates a new HTTPLogger object.
func newResponder() *Responder {
return &Responder{}
}