Fix domain presence check.

Before this commit, domains which had a project deployed but not
an index would be incorrectly considered nonexistent when queried
via the Caddy endpoint.
This commit is contained in:
Catherine
2025-09-17 12:11:24 +00:00
parent 8d9c64410e
commit 3b80cb4144
2 changed files with 38 additions and 2 deletions

View File

@@ -49,6 +49,9 @@ type Backend interface {
// Delete a manifest.
DeleteManifest(name string) error
// Check whether a domain has any deployments.
CheckDomain(domain string) (bool, error)
}
func splitBlobName(name string) []string {
@@ -208,6 +211,17 @@ func (fs *FSBackend) DeleteManifest(name string) error {
return fs.siteRoot.Remove(name)
}
func (fs *FSBackend) CheckDomain(domain string) (bool, error) {
_, err := fs.siteRoot.Stat(domain)
if errors.Is(err, os.ErrNotExist) {
return false, nil
} else if err == nil {
return true, nil
} else {
return false, err
}
}
type CachedBlob struct {
blob []byte
mtime time.Time
@@ -462,3 +476,20 @@ func (s3 *S3Backend) DeleteManifest(name string) error {
return s3.client.RemoveObject(s3.ctx, s3.bucket, manifestObjectName(name),
minio.RemoveObjectOptions{})
}
func (s3 *S3Backend) CheckDomain(domain string) (bool, error) {
log.Printf("s3: check domain %s\n", domain)
ctx, cancel := context.WithCancel(s3.ctx)
defer cancel()
for object := range s3.client.ListObjectsIter(ctx, s3.bucket, minio.ListObjectsOptions{
Prefix: manifestObjectName(fmt.Sprintf("%s/", domain)),
}) {
if object.Err != nil {
return false, object.Err
}
return true, nil
}
return false, nil
}

View File

@@ -13,11 +13,16 @@ func ServeCaddy(w http.ResponseWriter, r *http.Request) {
return
}
if manifest, _ := backend.GetManifest(fmt.Sprintf("%s/.index", domain)); manifest != nil {
found, err := backend.CheckDomain(domain)
if found {
log.Println("caddy:", domain, 200)
w.WriteHeader(http.StatusOK)
} else {
} else if err == nil {
log.Println("caddy:", domain, 404)
w.WriteHeader(http.StatusNotFound)
} else {
log.Println("caddy:", domain, 500)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintln(w, err)
}
}