Better service/credential validation

This commit is contained in:
Mememan 2024-04-24 19:10:13 +02:00
parent 0fb2b22480
commit f82bf81456
4 changed files with 43 additions and 1 deletions

View file

@ -69,7 +69,6 @@ func handleSubdomain(w http.ResponseWriter, r *http.Request) (input string, err
input = strings.TrimSpace(input)
input = strings.ToLower(input)
// Check if prompt has banned words
if config.state.bannedWordsRegex.MatchString(input) {
w.WriteHeader(http.StatusForbidden)
w.Write([]byte("This incident has been reported. Sick fuck."))

12
main.go
View file

@ -26,12 +26,24 @@ func main() {
}
runpod.Endpoint = config.RunpodApi.Endpoint
runpod.Authorization = config.RunpodApi.Authorization
// Check if Runpod serverless API is up
_, err = runpod.Health()
if err != nil {
log.Fatal(err)
}
// Initialize S3
err = s3.InitMinio(s3.Config(config.S3))
if err != nil {
log.Fatal(err)
}
// Check if we can access the bucket
_, err = s3.GetMetadata("could_be_any_object")
if err != nil {
if s3.ToErrorResponse(err).Code != "NoSuchKey" {
log.Fatal(err)
}
}
// http handler
http.HandleFunc("/img.png", servePicture)

View file

@ -35,6 +35,11 @@ func postReq(data []byte, path string) (resp *http.Response, err error) {
client := &http.Client{}
resp, err = client.Do(req)
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
return resp, fmt.Errorf("runpod '%s' resp HTTP code '%d'", path, resp.StatusCode)
}
return
}
@ -49,6 +54,28 @@ func getReq(path string) (resp *http.Response, err error) {
client := &http.Client{}
resp, err = client.Do(req)
statusOK := resp.StatusCode >= 200 && resp.StatusCode < 300
if !statusOK {
return resp, fmt.Errorf("runpod '%s' resp HTTP code '%d'", path, resp.StatusCode)
}
return
}
// Returns the status of the serverless endpoint, can be used to check if it's operational
// returns a map because I'm too lazy to make the proper struct
func Health() (res map[string]interface{}, err error) {
resp, err := getReq("health")
if err != nil {
return
}
err = json.NewDecoder(resp.Body).Decode(&res)
if err != nil {
return
}
defer resp.Body.Close()
return
}

View file

@ -35,6 +35,10 @@ func InitMinio(cfg Config) (err error) {
return
}
func CreateBucket(name string) (err error) {
return minioClient.MakeBucket(ctx, name, minio.MakeBucketOptions{})
}
// wrap Minio error response
func ToErrorResponse(e error) minio.ErrorResponse {
return minio.ToErrorResponse(e)