github.com/Redstoneguy129/cli@v0.0.0-20230211220159-15dca4e91917/test/mocks/supabase/server.go (about)

     1  package supabase
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/gin-gonic/gin"
     7  	gonanoid "github.com/matoous/go-nanoid/v2"
     8  )
     9  
    10  const (
    11  	IDAlphabet  = "abcdefghijklmnopqrstuvwxyz"
    12  	IDLength    = 20
    13  	KeyAlphabet = "abcdef0123456789"
    14  	KeyLength   = 40
    15  )
    16  
    17  var AccessToken = "sbp_" + gonanoid.MustGenerate(KeyAlphabet, KeyLength)
    18  
    19  // Server struct with route handlers
    20  type Server struct {
    21  	FunctionsHandler func(c *gin.Context)
    22  	SecretsHandler   func(c *gin.Context)
    23  }
    24  
    25  var defaultHandler = func(c *gin.Context) {
    26  	c.JSON(http.StatusNotImplemented, gin.H{
    27  		"message": "Not implemented",
    28  	})
    29  }
    30  
    31  // NewServer creates a new server with default handlers
    32  func NewServer() *Server {
    33  	s := Server{
    34  		FunctionsHandler: defaultHandler,
    35  		SecretsHandler:   defaultHandler,
    36  	}
    37  	return &s
    38  }
    39  
    40  // NewRouter creating a new router and setting the routes for the server.
    41  func (s *Server) NewRouter() *gin.Engine {
    42  	root := gin.Default()
    43  	router := root.Group("/v1")
    44  
    45  	projects := router.Group("/projects")
    46  	projects.GET("/:id/functions", s.functions)
    47  	projects.GET("/:id/secrets", s.secrets)
    48  
    49  	return root
    50  }
    51  
    52  // project routes
    53  func (s *Server) functions(c *gin.Context) {
    54  	if s.FunctionsHandler == nil {
    55  		c.JSON(http.StatusInternalServerError, gin.H{
    56  			"message": "handler is nil",
    57  		})
    58  	} else {
    59  		s.FunctionsHandler(c)
    60  	}
    61  }
    62  
    63  func (s *Server) secrets(c *gin.Context) {
    64  	if s.SecretsHandler == nil {
    65  		c.JSON(http.StatusInternalServerError, gin.H{
    66  			"message": "handler is nil",
    67  		})
    68  	} else {
    69  		s.SecretsHandler(c)
    70  	}
    71  }