github.com/tickoalcantara12/micro/v3@v3.0.0-20221007104245-9d75b9bcbab9/service/api/handler/handler.go (about)

     1  // Copyright 2020 Asim Aslam
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // Original source: github.com/micro/go-micro/v3/api/handler/handler.go
    16  
    17  // Package handler provides http handlers
    18  package handler
    19  
    20  import (
    21  	"context"
    22  	"net/http"
    23  
    24  	"github.com/tickoalcantara12/micro/v3/proto/api"
    25  	"github.com/tickoalcantara12/micro/v3/service/api/auth"
    26  	"github.com/tickoalcantara12/micro/v3/service/errors"
    27  	"github.com/tickoalcantara12/micro/v3/util/auth/namespace"
    28  )
    29  
    30  // Handler represents a HTTP handler that manages a request
    31  type Handler interface {
    32  	// standard http handler
    33  	ServeHTTP(w http.ResponseWriter, r *http.Request)
    34  	// name of handler
    35  	String() string
    36  }
    37  
    38  type APIHandler struct{}
    39  
    40  func (a *APIHandler) ReadBlockList(ctx context.Context, request *api.ReadBlockListRequest, response *api.ReadBlockListResponse) error {
    41  	if err := namespace.AuthorizeAdmin(ctx, namespace.DefaultNamespace, "api.API.AddToBlockList"); err != nil {
    42  		return err
    43  	}
    44  	blocked, err := auth.DefaultBlockList.List(ctx)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	response.Ids = blocked
    49  	return nil
    50  }
    51  
    52  func (a *APIHandler) AddToBlockList(ctx context.Context, request *api.AddToBlockListRequest, response *api.AddToBlockListResponse) error {
    53  	// authorize the request
    54  	if err := namespace.AuthorizeAdmin(ctx, namespace.DefaultNamespace, "api.API.AddToBlockList"); err != nil {
    55  		return err
    56  	}
    57  
    58  	if len(request.Id) == 0 {
    59  		return errors.BadRequest("api.AddToBlockList", "Missing ID field")
    60  	}
    61  	if len(request.Namespace) == 0 {
    62  		return errors.BadRequest("api.AddToBlockList", "Missing Namespace field")
    63  	}
    64  
    65  	return auth.DefaultBlockList.Add(ctx, request.Id, request.Namespace)
    66  }
    67  
    68  func (a *APIHandler) RemoveFromBlockList(ctx context.Context, request *api.RemoveFromBlockListRequest, response *api.RemoveFromBlockListResponse) error {
    69  	if err := namespace.AuthorizeAdmin(ctx, namespace.DefaultNamespace, "api.API.AddToBlockList"); err != nil {
    70  		return err
    71  	}
    72  	if len(request.Id) == 0 {
    73  		return errors.BadRequest("api.RemoveFromBlockList", "Missing ID field")
    74  	}
    75  	if len(request.Namespace) == 0 {
    76  		return errors.BadRequest("api.RemoveFromBlockList", "Missing Namespace field")
    77  	}
    78  
    79  	return auth.DefaultBlockList.Remove(ctx, request.Id, request.Namespace)
    80  }