capsule

package module
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2024 License: MIT Imports: 20 Imported by: 0

README

Go Capsule!

  • Getting Started
func main() {
    app := capsule.NewApp()

    app.AddHandler("Health", app.DefaultHealthHandler())
    app.RegisterRoutes(capsule.Router{
		{
			Path:    "/",
			Method:  app.GET,
			Handler: app.GetHandler("Health"),
		},
	})

    if err := app.Run(); err != nil {
	log.Fatal(err)
    }
}
  • On your browser, enter the URL: http://localhost:8080/ and check the response. Enjoy it!

Documentation

Index

Constants

View Source
const PORT = 8080

Variables

This section is empty.

Functions

func RandomString

func RandomString(n int) string

RandomString returns a string of random characters of length n, using randomStringSource as the source for the string

Types

type AES_GCM

type AES_GCM struct{}

================================================================================================= AES GCM: Advanced Encryption Standard (AES) is a symmetric encryption algorithm. =================================================================================================

func (*AES_GCM) DecryptGCM

func (a *AES_GCM) DecryptGCM(ciphertext string, secretKey []byte) string

func (*AES_GCM) EncryptGCM

func (a *AES_GCM) EncryptGCM(plaintext string, secretKey []byte) string

func (*AES_GCM) Generate32BytesBase64Key

func (a *AES_GCM) Generate32BytesBase64Key() string

func (*AES_GCM) Generate32BytesKey

func (a *AES_GCM) Generate32BytesKey() []byte

type CORSCapsule

type CORSCapsule struct {
	AllowedOrigins   []string
	AllowedMethods   []string
	AllowedHeaders   []string
	AllowCredentials bool
}

type CapsuleApp

type CapsuleApp struct {
	PORT     string
	Security SecurityService
	CORS     CORSCapsule
	Handlers []CapsuleHandler
	Version  string
}

func NewApp

func NewApp(props ...Props) *CapsuleApp

func (*CapsuleApp) AddHandler

func (h *CapsuleApp) AddHandler(name string, handler func(w http.ResponseWriter, r *http.Request))

func (*CapsuleApp) AuthRequired

func (c *CapsuleApp) AuthRequired(h http.Handler) http.Handler

AUTH

func (*CapsuleApp) CORSRequired

func (c *CapsuleApp) CORSRequired(h http.Handler) http.Handler

func (*CapsuleApp) DELETE

func (c *CapsuleApp) DELETE(h http.Handler) http.Handler

DELETE

func (*CapsuleApp) DefaultAuthenticationHandler

func (h *CapsuleApp) DefaultAuthenticationHandler(db Repository) func(w http.ResponseWriter, r *http.Request)

func (*CapsuleApp) DefaultHealthHandler

func (h *CapsuleApp) DefaultHealthHandler(healthMessage ...string) func(w http.ResponseWriter, r *http.Request)

func (*CapsuleApp) GET

func (c *CapsuleApp) GET(h http.Handler) http.Handler

GET

func (*CapsuleApp) GetHandler

func (h *CapsuleApp) GetHandler(name string) func(w http.ResponseWriter, r *http.Request)

func (*CapsuleApp) OPTIONS

func (c *CapsuleApp) OPTIONS(h http.Handler) http.Handler

OPTIONS

func (*CapsuleApp) PATCH

func (c *CapsuleApp) PATCH(h http.Handler) http.Handler

PATCH

func (*CapsuleApp) POST

func (c *CapsuleApp) POST(h http.Handler) http.Handler

POST

func (*CapsuleApp) PUT

func (c *CapsuleApp) PUT(h http.Handler) http.Handler

PUT

func (*CapsuleApp) RegisterProtectedRoutes

func (c *CapsuleApp) RegisterProtectedRoutes(routes []Route)

func (*CapsuleApp) RegisterRoutes

func (c *CapsuleApp) RegisterRoutes(routes []Route)

func (*CapsuleApp) Run

func (c *CapsuleApp) Run() error

type CapsuleHandler

type CapsuleHandler struct {
	Name    string
	Handler func(w http.ResponseWriter, r *http.Request)
}

type Claims

type Claims struct {
	jwt.RegisteredClaims
}

type Handler

type Handler struct {
	Handlers []CapsuleHandler
}

type JSONResponse

type JSONResponse struct {
	Error   bool        `json:"error"`
	Message string      `json:"message"`
	Data    interface{} `json:"data,omitempty"`
}

func (JSONResponse) ErrorJson

func (JSONResponse) ErrorJson(w http.ResponseWriter, err error, status ...int) error

func (JSONResponse) ReadJson

func (JSONResponse) ReadJson(w http.ResponseWriter, r *http.Request, data interface{}) error

func (JSONResponse) WriteJSON

func (JSONResponse) WriteJSON(w http.ResponseWriter, status int, data interface{}, headers ...http.Header) error

type JwtUser

type JwtUser struct {
	ID        string `json:"id"`
	Username  string `json:"username"`
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
	Email     string `json:"email"`
	Role      string `json:"role"`
}

type Props

type Props struct {
	PORT     uint
	Security SecurityService
	CORS     CORSCapsule
	Version  string
}

type RSA_OAEP

type RSA_OAEP struct{}

================================================================================================= RSA OAEP: RSA (Rivest-Shamir-Adleman) is an asymmetric encryption algorithm. =================================================================================================

func (*RSA_OAEP) DecryptOAEP

func (r *RSA_OAEP) DecryptOAEP(cipherText string, privKey *rsa.PrivateKey) (string, error)

func (*RSA_OAEP) EncryptOAEP

func (r *RSA_OAEP) EncryptOAEP(secretMessage string, key *rsa.PublicKey) (string, error)

OAEP is an asymmetric encryption algorithm that combines the RSA algorithm with the OAEP padding scheme. The RSA algorithm is used to encrypt the data, and the OAEP padding scheme is used to add randomness to the data.

func (*RSA_OAEP) GenerateRSAKeyPair

func (r *RSA_OAEP) GenerateRSAKeyPair(bits int) (*rsa.PrivateKey, *rsa.PublicKey)

func (*RSA_OAEP) GenerateRSAKeyPairAndSaveOnPath

func (r *RSA_OAEP) GenerateRSAKeyPairAndSaveOnPath(bits int, path string)

func (*RSA_OAEP) LoadPrivateKey

func (r *RSA_OAEP) LoadPrivateKey(path string) (*rsa.PrivateKey, error)

func (*RSA_OAEP) LoadPrivateKeyFromBytes

func (r *RSA_OAEP) LoadPrivateKeyFromBytes(pemBytes []byte) (*rsa.PrivateKey, error)

func (*RSA_OAEP) LoadPublicKey

func (r *RSA_OAEP) LoadPublicKey(path string) (*rsa.PublicKey, error)

func (*RSA_OAEP) LoadPublicKeyFromBytes

func (r *RSA_OAEP) LoadPublicKeyFromBytes(pemBytes []byte) (*rsa.PublicKey, error)

type Repository

type Repository interface {
	GetUserByEmail(email string) (User, error)
}

type Route

type Route struct {
	Path    string
	Method  func(http.Handler) http.Handler
	Handler http.HandlerFunc
}

type Router

type Router []Route

type SecurityCapsule

type SecurityCapsule struct {
	Issuer        string
	Audience      string
	Secret        string
	TokenExpiry   time.Duration
	RefreshExpiry time.Duration
	CookieDomain  string
	CookiePath    string
	CookieName    string
}

func (*SecurityCapsule) GenerateTokenPair

func (s *SecurityCapsule) GenerateTokenPair(u *JwtUser) (*TokenPairs, error)

func (*SecurityCapsule) GetTokenFromHeaderAndVerify

func (s *SecurityCapsule) GetTokenFromHeaderAndVerify(w http.ResponseWriter, r *http.Request) (string, *Claims, error)

type SecurityService

type SecurityService interface {
	GenerateTokenPair(u *JwtUser) (*TokenPairs, error)
	GetTokenFromHeaderAndVerify(w http.ResponseWriter, r *http.Request) (string, *Claims, error)
}

type TokenPairs

type TokenPairs struct {
	AccessToken  string `json:"access_token"`
	RefreshToken string `json:"refresh_token"`
}

type User

type User interface {
	GetUserId() string
	GetEmail() string
	GetRole() string
	GetFirstName() string
	GetLastName() string
	GetIsDisabled() bool
	CheckPassword(password string) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL