Merge pull request 'Refactor models/response.go -> apiresponse/, webresponse/' (#167) from refactor/response into main
Reviewed-on: #167
This commit is contained in:
commit
b0db9ce983
10 changed files with 140 additions and 117 deletions
|
@ -14,6 +14,8 @@ ADD api/ api/
|
|||
|
||||
ADD api_handlers/ api_handlers/
|
||||
|
||||
ADD apiresponse/ apiresponse/
|
||||
|
||||
ADD config/ config/
|
||||
|
||||
ADD media/ media/
|
||||
|
@ -24,6 +26,8 @@ ADD templates/ templates/
|
|||
|
||||
ADD web_handlers/ web_handlers/
|
||||
|
||||
Add webresponse/ webresponse/
|
||||
|
||||
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o agecem-org .
|
||||
|
||||
# Alpine
|
||||
|
|
|
@ -10,8 +10,8 @@ import (
|
|||
"net/url"
|
||||
|
||||
"codeberg.org/vlbeaudoin/voki"
|
||||
"git.agecem.com/agecem/agecem-org/apiresponse"
|
||||
"git.agecem.com/agecem/agecem-org/config"
|
||||
"git.agecem.com/agecem/agecem-org/models"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
|
@ -35,8 +35,8 @@ func New(client *http.Client, host, key string, port int, protocol string) (*API
|
|||
return &API{Voki: voki.New(client, host, key, port, protocol)}, nil
|
||||
}
|
||||
|
||||
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (models.V1DocumentCreateResponse, error) {
|
||||
var response models.V1DocumentCreateResponse
|
||||
func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (apiresponse.V1DocumentCreateResponse, error) {
|
||||
var response apiresponse.V1DocumentCreateResponse
|
||||
endpoint := fmt.Sprintf("%s://%s:%d",
|
||||
a.Voki.Protocol,
|
||||
a.Voki.Host,
|
||||
|
@ -100,6 +100,6 @@ func (a *API) UploadDocument(bucket string, file_header *multipart.FileHeader) (
|
|||
return response, err
|
||||
}
|
||||
|
||||
func (a *API) ListBuckets() (response models.V1BucketListResponse, err error) {
|
||||
func (a *API) ListBuckets() (response apiresponse.V1BucketListResponse, err error) {
|
||||
return response, a.Voki.Unmarshal(http.MethodGet, "/v1/bucket", nil, true, &response)
|
||||
}
|
||||
|
|
|
@ -5,9 +5,9 @@ import (
|
|||
"net/http"
|
||||
"sort"
|
||||
|
||||
"git.agecem.com/agecem/agecem-org/apiresponse"
|
||||
"git.agecem.com/agecem/agecem-org/config"
|
||||
"git.agecem.com/agecem/agecem-org/media"
|
||||
"git.agecem.com/agecem/agecem-org/models"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/minio/minio-go/v7"
|
||||
)
|
||||
|
@ -30,7 +30,7 @@ func (h *V1Handler) HandleV1(c echo.Context) error {
|
|||
// HandleV1Seed créé des buckets dans minio selon la liste de buckets dans server.documents.buckets
|
||||
// Les buckets sont créés avec paramètres par défaut, et sont ensuite visible dans /v1/bucket.
|
||||
func (h *V1Handler) HandleV1Seed(c echo.Context) error {
|
||||
var response models.V1SeedResponse
|
||||
var response apiresponse.V1SeedResponse
|
||||
|
||||
new_buckets, err := h.MediaClient.Seed()
|
||||
response.Data.Buckets = new_buckets
|
||||
|
@ -56,7 +56,7 @@ func (h *V1Handler) HandleV1Seed(c echo.Context) error {
|
|||
|
||||
// HandleV1BucketList affiche les buckets permis par server.documents.buckets, qui existent.
|
||||
func (h *V1Handler) HandleV1BucketList(c echo.Context) error {
|
||||
var response models.V1BucketListResponse
|
||||
var response apiresponse.V1BucketListResponse
|
||||
|
||||
var buckets = make(map[string]string)
|
||||
|
||||
|
@ -83,7 +83,7 @@ func (h *V1Handler) HandleV1BucketList(c echo.Context) error {
|
|||
}
|
||||
|
||||
func (h *V1Handler) HandleV1BucketRead(c echo.Context) error {
|
||||
var response models.V1BucketReadResponse
|
||||
var response apiresponse.V1BucketReadResponse
|
||||
|
||||
bucket := c.Param("bucket")
|
||||
|
||||
|
@ -95,7 +95,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !allowed {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -112,7 +112,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !exists {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
objectCh := h.MediaClient.MinioClient.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
|
||||
|
@ -137,7 +137,7 @@ func (h *V1Handler) HandleV1BucketRead(c echo.Context) error {
|
|||
|
||||
// HandleV1DocumentCreate permet d'ajouter un object dans un bucket, par multipart/form-data
|
||||
func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error {
|
||||
var response models.V1DocumentCreateResponse
|
||||
var response apiresponse.V1DocumentCreateResponse
|
||||
|
||||
bucket := c.Param("bucket")
|
||||
|
||||
|
@ -158,7 +158,7 @@ func (h *V1Handler) HandleV1DocumentCreate(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !allowed {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -208,7 +208,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !allowed {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -221,7 +221,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !bucket_exists {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
|
||||
|
@ -229,7 +229,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error {
|
|||
if err != nil {
|
||||
if err.Error() == "The specified key does not exist." {
|
||||
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
|
@ -253,7 +253,7 @@ func (h *V1Handler) HandleV1DocumentRead(c echo.Context) error {
|
|||
|
||||
// HandleV1DocumentUpdate permet de mettre à jour certains champs d'un object, comme le Content-Type ou le Filename
|
||||
func (h *V1Handler) HandleV1DocumentUpdate(c echo.Context) error {
|
||||
return c.JSON(models.NotImplementedResponse())
|
||||
return c.JSON(apiresponse.NotImplementedResponse())
|
||||
}
|
||||
|
||||
// HandleV1DocumentDelete permet de supprimer un object
|
||||
|
@ -269,7 +269,7 @@ func (h *V1Handler) HandleV1DocumentDelete(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !allowed {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
@ -282,14 +282,14 @@ func (h *V1Handler) HandleV1DocumentDelete(c echo.Context) error {
|
|||
}
|
||||
|
||||
if !bucket_exists {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
document_info, err := h.MediaClient.MinioClient.StatObject(ctx, bucket, document, minio.StatObjectOptions{})
|
||||
if err != nil {
|
||||
if err.Error() == "The specified key does not exist." {
|
||||
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusInternalServerError, map[string]interface{}{
|
||||
|
|
45
apiresponse/apiresponse.go
Normal file
45
apiresponse/apiresponse.go
Normal file
|
@ -0,0 +1,45 @@
|
|||
package apiresponse
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Responder interface {
|
||||
Respond() Responder
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
Message string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (r Response) Respond() Responder {
|
||||
return r
|
||||
}
|
||||
|
||||
type SimpleResponse struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (r SimpleResponse) Respond() Responder {
|
||||
return r
|
||||
}
|
||||
|
||||
func NotFoundResponse() (int, SimpleResponse) {
|
||||
return http.StatusNotFound, SimpleResponse{
|
||||
Message: "Not Found",
|
||||
}
|
||||
}
|
||||
|
||||
func NotImplementedResponse() (int, SimpleResponse) {
|
||||
return http.StatusNotImplemented, SimpleResponse{
|
||||
Message: "Not Implemented",
|
||||
}
|
||||
}
|
||||
|
||||
func InternalServerErrorResponse() (int, SimpleResponse) {
|
||||
return http.StatusInternalServerError, SimpleResponse{
|
||||
Message: "Internal Server Error",
|
||||
}
|
||||
}
|
15
apiresponse/bucket.go
Normal file
15
apiresponse/bucket.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
package apiresponse
|
||||
|
||||
type V1BucketListResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Buckets map[string]string
|
||||
}
|
||||
}
|
||||
|
||||
type V1BucketReadResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Keys []string
|
||||
}
|
||||
}
|
10
apiresponse/document.go
Normal file
10
apiresponse/document.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package apiresponse
|
||||
|
||||
type V1DocumentCreateResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Bucket string
|
||||
Key string
|
||||
Size int64
|
||||
}
|
||||
}
|
8
apiresponse/seed.go
Normal file
8
apiresponse/seed.go
Normal file
|
@ -0,0 +1,8 @@
|
|||
package apiresponse
|
||||
|
||||
type V1SeedResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Buckets []string
|
||||
}
|
||||
}
|
|
@ -1,92 +0,0 @@
|
|||
package models
|
||||
|
||||
import "net/http"
|
||||
|
||||
type Responder interface {
|
||||
Respond() Responder
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
StatusCode int `json:"status_code"`
|
||||
Message string
|
||||
Error string
|
||||
}
|
||||
|
||||
func (r Response) Respond() Responder {
|
||||
return r
|
||||
}
|
||||
|
||||
type SimpleResponse struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (r SimpleResponse) Respond() Responder {
|
||||
return r
|
||||
}
|
||||
|
||||
func NotFoundResponse() (int, SimpleResponse) {
|
||||
return http.StatusNotFound, SimpleResponse{
|
||||
Message: "Not Found",
|
||||
}
|
||||
}
|
||||
|
||||
func NotImplementedResponse() (int, SimpleResponse) {
|
||||
return http.StatusNotImplemented, SimpleResponse{
|
||||
Message: "Not Implemented",
|
||||
}
|
||||
}
|
||||
|
||||
type HandleAdminDocumentsUploadResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Buckets []Bucket
|
||||
}
|
||||
}
|
||||
|
||||
type HandleDocumentationResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Buckets []Bucket
|
||||
}
|
||||
}
|
||||
|
||||
type UploadDocumentResponse struct {
|
||||
Response
|
||||
Data UploadDocumentResponseData
|
||||
}
|
||||
|
||||
type UploadDocumentResponseData struct {
|
||||
Bucket string
|
||||
Object string
|
||||
Size float64
|
||||
}
|
||||
|
||||
type V1SeedResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Buckets []string
|
||||
}
|
||||
}
|
||||
|
||||
type V1BucketListResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Buckets map[string]string
|
||||
}
|
||||
}
|
||||
|
||||
type V1BucketReadResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Keys []string
|
||||
}
|
||||
}
|
||||
|
||||
type V1DocumentCreateResponse struct {
|
||||
Response
|
||||
Data struct {
|
||||
Bucket string
|
||||
Key string
|
||||
Size int64
|
||||
}
|
||||
}
|
|
@ -8,7 +8,9 @@ import (
|
|||
"sort"
|
||||
|
||||
"git.agecem.com/agecem/agecem-org/api"
|
||||
"git.agecem.com/agecem/agecem-org/apiresponse"
|
||||
"git.agecem.com/agecem/agecem-org/models"
|
||||
"git.agecem.com/agecem/agecem-org/webresponse"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
|
@ -49,7 +51,7 @@ func HandleVieEtudianteOrganisme(c echo.Context) error {
|
|||
}
|
||||
|
||||
func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
||||
var response models.HandleDocumentationResponse
|
||||
var response webresponse.HandleDocumentationResponse
|
||||
|
||||
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
||||
if err != nil {
|
||||
|
@ -64,7 +66,7 @@ func (h *WebHandler) HandleDocumentation(c echo.Context) error {
|
|||
|
||||
for bucket, displayName := range v1BucketListResponse.Data.Buckets {
|
||||
// TODO move call to dedicated API client method
|
||||
var v1BucketReadResponse models.V1BucketReadResponse
|
||||
var v1BucketReadResponse apiresponse.V1BucketReadResponse
|
||||
|
||||
if err = h.ApiClient.Voki.Unmarshal(http.MethodGet, fmt.Sprintf("/v1/bucket/%s", bucket), nil, true, &v1BucketReadResponse); err != nil {
|
||||
response.Error = err.Error()
|
||||
|
@ -106,13 +108,13 @@ func (h *WebHandler) HandlePublicDocumentation(c echo.Context) error {
|
|||
|
||||
response, err := h.ApiClient.Voki.Call(http.MethodGet, unescaped, nil, true)
|
||||
if err != nil {
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
switch response.StatusCode {
|
||||
case http.StatusNotFound:
|
||||
return c.JSON(models.NotFoundResponse())
|
||||
return c.JSON(apiresponse.NotFoundResponse())
|
||||
case http.StatusInternalServerError:
|
||||
return c.JSON(http.StatusInternalServerError, map[string]string{"message": "Internal Server Error"})
|
||||
}
|
||||
|
@ -130,7 +132,7 @@ func HandleAdmin(c echo.Context) error {
|
|||
}
|
||||
|
||||
func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
|
||||
var response models.HandleAdminDocumentsUploadResponse
|
||||
var response webresponse.HandleAdminDocumentsUploadResponse
|
||||
|
||||
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
||||
if err != nil {
|
||||
|
@ -153,7 +155,7 @@ func (h *WebHandler) HandleAdminDocumentsUpload(c echo.Context) error {
|
|||
}
|
||||
|
||||
func (h *WebHandler) HandleAdminDocumentsUploadPOST(c echo.Context) error {
|
||||
var response models.HandleAdminDocumentsUploadResponse
|
||||
var response webresponse.HandleAdminDocumentsUploadResponse
|
||||
|
||||
v1BucketListResponse, err := h.ApiClient.ListBuckets()
|
||||
if err != nil {
|
||||
|
|
31
webresponse/webresponse.go
Normal file
31
webresponse/webresponse.go
Normal file
|
@ -0,0 +1,31 @@
|
|||
package webresponse
|
||||
|
||||
import (
|
||||
"git.agecem.com/agecem/agecem-org/apiresponse"
|
||||
"git.agecem.com/agecem/agecem-org/models"
|
||||
)
|
||||
|
||||
type HandleAdminDocumentsUploadResponse struct {
|
||||
apiresponse.Response
|
||||
Data struct {
|
||||
Buckets []models.Bucket
|
||||
}
|
||||
}
|
||||
|
||||
type HandleDocumentationResponse struct {
|
||||
apiresponse.Response
|
||||
Data struct {
|
||||
Buckets []models.Bucket
|
||||
}
|
||||
}
|
||||
|
||||
type UploadDocumentResponse struct {
|
||||
apiresponse.Response
|
||||
Data UploadDocumentResponseData
|
||||
}
|
||||
|
||||
type UploadDocumentResponseData struct {
|
||||
Bucket string
|
||||
Object string
|
||||
Size float64
|
||||
}
|
Loading…
Reference in a new issue