-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
34 changed files
with
915 additions
and
46 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
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,18 @@ | ||
CREATE TABLE buckets | ||
( | ||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(), | ||
"endpoint_url" VARCHAR(255) NOT NULL, | ||
"name" VARCHAR(255) NOT NULL, | ||
"active" BOOLEAN NOT NULL DEFAULT FALSE | ||
); | ||
|
||
CREATE TABLE objects | ||
( | ||
"guild_id" int8 NOT NULL, | ||
"ticket_id" int4 NOT NULL, | ||
"bucket_id" uuid NOT NULL, | ||
PRIMARY KEY ("guild_id", "ticket_id"), | ||
FOREIGN KEY ("bucket_id") REFERENCES "buckets" ("id") | ||
); | ||
|
||
CREATE INDEX objects_guid_id_idx ON objects ("guild_id"); |
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,112 @@ | ||
package http | ||
|
||
import ( | ||
"errors" | ||
"github.com/TicketsBot/logarchiver/pkg/repository" | ||
"github.com/TicketsBot/logarchiver/pkg/repository/model" | ||
"github.com/gin-gonic/gin" | ||
"github.com/google/uuid" | ||
"go.uber.org/zap" | ||
"net/http" | ||
) | ||
|
||
func (s *Server) adminListBuckets(ctx *gin.Context) { | ||
var buckets []model.Bucket | ||
if err := s.store.Tx(ctx, func(r repository.Repositories) (err error) { | ||
buckets, err = r.Buckets().ListBuckets(ctx) | ||
return | ||
}); err != nil { | ||
s.Logger.Error("Error fetching buckets", zap.Error(err)) | ||
ctx.JSON(500, gin.H{ | ||
"message": "Error fetching buckets", | ||
}) | ||
return | ||
} | ||
|
||
ctx.JSON(200, buckets) | ||
} | ||
|
||
func (s *Server) adminCreateBucket(ctx *gin.Context) { | ||
type body struct { | ||
EndpointUrl string `json:"endpoint_url"` | ||
Name string `json:"name"` | ||
} | ||
|
||
var b body | ||
if err := ctx.BindJSON(&b); err != nil { | ||
ctx.JSON(400, gin.H{ | ||
"message": "missing endpoint_url or name", | ||
}) | ||
return | ||
} | ||
|
||
var bucketId uuid.UUID | ||
if err := s.store.Tx(ctx, func(r repository.Repositories) (err error) { | ||
bucketId, err = r.Buckets().CreateBucket(ctx, b.EndpointUrl, b.Name) | ||
return | ||
}); err != nil { | ||
s.Logger.Error("Error creating bucket", zap.Error(err), zap.Any("request_data", b)) | ||
ctx.JSON(500, gin.H{ | ||
"message": "Error creating bucket", | ||
}) | ||
return | ||
} | ||
|
||
ctx.JSON(http.StatusCreated, model.Bucket{ | ||
Id: bucketId, | ||
EndpointUrl: b.EndpointUrl, | ||
Name: b.Name, | ||
Active: false, | ||
}) | ||
} | ||
|
||
func (s *Server) adminSetActiveBucket(ctx *gin.Context) { | ||
type body struct { | ||
BucketId uuid.UUID `json:"bucket_id"` | ||
} | ||
|
||
var b body | ||
if err := ctx.BindJSON(&b); err != nil { | ||
ctx.JSON(400, gin.H{ | ||
"message": "missing bucket_id", | ||
}) | ||
return | ||
} | ||
|
||
var ErrBucketNotFound = errors.New("bucket not found") | ||
if err := s.store.Tx(ctx, func(r repository.Repositories) error { | ||
buckets, err := r.Buckets().ListBuckets(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
found := false | ||
for _, bucket := range buckets { | ||
if bucket.Id == b.BucketId { | ||
found = true | ||
break | ||
} | ||
} | ||
|
||
if !found { | ||
return ErrBucketNotFound | ||
} | ||
|
||
return r.Buckets().SetActiveBucket(ctx, b.BucketId) | ||
}); err != nil { | ||
if errors.Is(err, ErrBucketNotFound) { | ||
ctx.JSON(404, gin.H{ | ||
"message": "bucket not found", | ||
}) | ||
} else { | ||
s.Logger.Error("Error setting active bucket", zap.Error(err), zap.Any("request_data", b)) | ||
ctx.JSON(500, gin.H{ | ||
"message": "Error setting active bucket", | ||
}) | ||
} | ||
|
||
return | ||
} | ||
|
||
ctx.Status(204) | ||
} |
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,24 @@ | ||
package http | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
func (s *Server) middlewareAuthAdmin(ctx *gin.Context) { | ||
if len(s.Config.AdminAuthToken) == 0 { | ||
s.Logger.Error("Admin authentication token not set") | ||
ctx.JSON(500, gin.H{ | ||
"message": "misconfigured server", | ||
}) | ||
ctx.Abort() | ||
return | ||
} | ||
|
||
if ctx.GetHeader("Authorization") != s.Config.AdminAuthToken { | ||
ctx.JSON(401, gin.H{ | ||
"message": "unauthorized", | ||
}) | ||
ctx.Abort() | ||
return | ||
} | ||
|
||
ctx.Next() | ||
} |
Oops, something went wrong.