Skip to content

Commit

Permalink
bugfixes, site creation works end to end now
Browse files Browse the repository at this point in the history
  • Loading branch information
pickledish committed Dec 14, 2023
1 parent 1879eef commit 6fbb713
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
7 changes: 4 additions & 3 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ func (s Server) CreateSite(out http.ResponseWriter, req *http.Request) {
return
}

userIdUntyped, found := claims["user_id"]
// what is "sub"? If nothing else, seems to be the JWT's slang for user ID
userIdUntyped, found := claims["sub"]
if !found {
log.Printf("[ERROR] No 'user_id' field found in JWT claims: %v", claims)
http.Error(out, "Unable to parse claims from JWT", http.StatusUnauthorized)
Expand All @@ -53,7 +54,7 @@ func (s Server) CreateSite(out http.ResponseWriter, req *http.Request) {

err = json.NewDecoder(req.Body).Decode(&body)
if err != nil {
log.Printf("[ERROR] Request body did not parse as expected: %w, body %v", err, req.Body)
log.Printf("[ERROR] Request body did not parse as expected: %v, body %v", err, req.Body)
http.Error(out, "Malformed input, just send JSON with a nickname field", http.StatusBadRequest)
return
}
Expand All @@ -63,7 +64,7 @@ func (s Server) CreateSite(out http.ResponseWriter, req *http.Request) {
log.Printf("[INFO] Creating a new site with generated ID %v...", siteId)

storage := s.bunny.CreateStorageZone(req.Context(), siteId)
if storage != nil {
if storage == nil {
http.Error(out, "Unable to create new storage zone", http.StatusInternalServerError)
return
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/api/bunny.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (b BunnyClient) CreateStorageZone(ctx context.Context, siteId string) *Crea
Fetch(ctx)

if err != nil {
log.Printf("[ERROR] Unable to create storage zone in BunnyCDN: %w, response: %+v", err, errorJson)
log.Printf("[ERROR] Unable to create storage zone in BunnyCDN: %v, response: %+v", err, errorJson)
return nil
}

Expand Down Expand Up @@ -167,7 +167,7 @@ func (b BunnyClient) CreatePullZone(ctx context.Context, siteId string, storage
Fetch(ctx)

if err != nil {
log.Printf("[ERROR] Unable to create pull zone in BunnyCDN: %w, response: %+v", err, errorJson)
log.Printf("[ERROR] Unable to create pull zone in BunnyCDN: %v, response: %+v", err, errorJson)
return ""
}

Expand Down
8 changes: 4 additions & 4 deletions cmd/api/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ var ApiCmd = &cobra.Command{
r.Use(util.CheckJwtMiddleware(permissive, false))

sup := SupabaseClient{
SupabaseUrl: "",
SupabaseAdminToken: "",
SupabaseUrl: "https://ewwccbgjnulfgcvfrsvj.supabase.co",
SupabaseAdminToken: "aaaa",
}

bun := BunnyClient{
BunnyUrl: "https://api.bunny.net",
BunnyAccessKey: "aaaaa",
BunnyUrl: "https://api.bunny.net",
BunnyAccessKey: "aaaa",
}

s := Server{sup, bun}
Expand Down
11 changes: 7 additions & 4 deletions cmd/api/supabase.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ func (s SupabaseClient) CreateSiteRow(ctx context.Context, jwt, userId, siteId,
err := requests.
URL(s.SupabaseUrl).
Path("/rest/v1/site").
Header("apikey", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV3d2NjYmdqbnVsZmdjdmZyc3ZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTM1ODE2ODUsImV4cCI6MjAwOTE1NzY4NX0.gI3YdNSC5GMkda2D2QPRMvnBdaMOS2ynfFKxis5-WKs").
Header("Authorization", jwt).
ContentType("application/json").
BodyJSON(&body).
ErrorJSON(&errorJson).
Fetch(ctx)

if err != nil {
log.Printf("[ERROR] Unable to create new SITE row: %w, response: %+v", err, errorJson)
log.Printf("[ERROR] Unable to create new SITE row: %v, response: %+v", err, errorJson)
return false
}

Expand All @@ -83,14 +84,15 @@ func (s SupabaseClient) CreateAliasRow(ctx context.Context, jwt, userId, siteId,
err := requests.
URL(s.SupabaseUrl).
Path("/rest/v1/alias").
Header("apikey", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV3d2NjYmdqbnVsZmdjdmZyc3ZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTM1ODE2ODUsImV4cCI6MjAwOTE1NzY4NX0.gI3YdNSC5GMkda2D2QPRMvnBdaMOS2ynfFKxis5-WKs").
Header("Authorization", jwt).
ContentType("application/json").
BodyJSON(&body).
ErrorJSON(&errorJson).
Fetch(ctx)

if err != nil {
log.Printf("[ERROR] Unable to create new ALIAS row: %w, response: %+v", err, errorJson)
log.Printf("[ERROR] Unable to create new ALIAS row: %v, response: %+v", err, errorJson)
return false
}

Expand All @@ -112,18 +114,19 @@ func (s SupabaseClient) AuthorizeHostname(ctx context.Context, userId, hostname

var errorJson map[string]interface{}

// needs to be PUT I think?
err := requests.
URL(s.SupabaseUrl).
Pathf("/auth/v1/admin/users/%s", userId).
Header("apikey", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV3d2NjYmdqbnVsZmdjdmZyc3ZqIiwicm9sZSI6ImFub24iLCJpYXQiOjE2OTM1ODE2ODUsImV4cCI6MjAwOTE1NzY4NX0.gI3YdNSC5GMkda2D2QPRMvnBdaMOS2ynfFKxis5-WKs").
Header("Authorization", fmt.Sprintf("Bearer %v", s.SupabaseAdminToken)).
ContentType("application/json").
BodyJSON(&body).
ErrorJSON(&errorJson).
Put().
Fetch(ctx)

if err != nil {
log.Printf("[ERROR] Unable to create pull zone in BunnyCDN: %w, response: %+v", err, errorJson)
log.Printf("[ERROR] Unable to create pull zone in BunnyCDN: %v, response: %+v", err, errorJson)
return false
}

Expand Down

0 comments on commit 6fbb713

Please sign in to comment.