-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
have monitor use an interface for storage
- Loading branch information
1 parent
e14330b
commit 092e5f3
Showing
4 changed files
with
52 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ctmonitor | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type Storage interface { | ||
Get(ctx context.Context, key string) (data []byte, found bool, err error) | ||
} | ||
|
||
// ------------------------------------------------------------ | ||
|
||
type UrlStorage struct { | ||
urlPrefix string | ||
} | ||
|
||
func (f *UrlStorage) Get(ctx context.Context, key string) (data []byte, notfounderr bool, err error) { | ||
req, err := http.NewRequestWithContext(ctx, "GET", f.urlPrefix+key, nil) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
if resp.StatusCode != 200 { | ||
if resp.StatusCode == 404 { | ||
return nil, true, errors.New(resp.Status) | ||
} else { | ||
return nil, false, errors.New(resp.Status) | ||
} | ||
} | ||
body, err := io.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, false, err | ||
} | ||
return body, false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters