api

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2025 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// HealthCheckPath constant is the path used to check the health of the API
	// server. It is a string with a value of "/health".
	HealthCheckPath = "/ping"
	// AppsPath constant is the path used to create the apps in the API server.
	AppsPath = "/apps"
	// TokensPath constant is the path used to generate and verify the tokens
	// in the API server.
	TokensPath = "/tokens"
	// DemoInboxPath constant is the path used to get the demo email inbox
	// in the API server when it runs in demo mode.
	DemoInboxPath = "/demo/inbox"
)

routes paths constants

View Source
const (
	// AppIDHeader constant is the header of the app ID in the request. It is
	// used to authenticate the app making the request.
	AppIDHeader = "AppID"
	// AppSecretHeader constant is the header of the app secret in the request
	// It is used to authenticate the app making the request.
	AppSecretHeader = "AppSecret"
)

other api related constants

Variables

View Source
var (
	// Decode data errors
	DecodeAppIDRequestErr       = io.NewAPIError(1001, http.StatusBadRequest).With("could not decode app id request")
	DecodeTokenRequestErr       = io.NewAPIError(1002, http.StatusBadRequest).With("could not decode token request")
	DecodeTokenStatusRequestErr = io.NewAPIError(1003, http.StatusBadRequest).With("could not decode token status request")
	// Encode data errors
	EncodeAppIDResponseErr       = io.NewAPIError(1010, http.StatusInternalServerError).With("could not encode app id response")
	EncodeTokenStatusResponseErr = io.NewAPIError(1011, http.StatusInternalServerError).With("could not encode token status response")
	// Bad request errors
	InvalidAppHeadersErr     = io.NewAPIError(1020, http.StatusBadRequest).With("invalid app headers")
	InvalidAppIDErr          = io.NewAPIError(1021, http.StatusBadRequest).With("invalid app id")
	InvalidAppSecretErr      = io.NewAPIError(1022, http.StatusBadRequest).With("invalid app secret")
	InvalidDemoEmailInboxErr = io.NewAPIError(1023, http.StatusBadRequest).With("invalid demo email inbox")
	// Internal errors
	GenerateTokenErr = io.NewAPIError(1030, http.StatusInternalServerError).With("could not generate token")
	GenerateEmailErr = io.NewAPIError(1031, http.StatusInternalServerError).With("could not generate email")
	SendEmailErr     = io.NewAPIError(1032, http.StatusInternalServerError).With("could not send email")
	InternalErr      = io.NewAPIError(1033, http.StatusInternalServerError).With("internal server error")
)

Functions

This section is empty.

Types

type AppIDRequest

type AppIDRequest struct {
	Name        string `json:"name"`
	Duration    string `json:"session_duration"`
	RedirectURL string `json:"redirect_url"`
	Secret      string `json:"secret"`
}

type AppIDResponse

type AppIDResponse struct {
	ID string `json:"id"`
}

type Config

type Config struct {
	Server     string
	ServerPort int
	Secret     string
	// demo stuff
	DemoMode     bool
	DemoSMTPAddr string
	DemoSMTPPort int
}

Config struct represents the configuration for the API service. It contains the server address, server port, and secret key for the service. The server address is the address where the service will listen for incoming requests, and the server port is the port number where the service will listen for incoming requests. The secret key is used to sign and verify tokens. The demo mode is used to enable or disable the demo functionality of the service. The demo SMTP address and port are used to configure the demo mail server. The demo mode is used to enable or disable the demo functionality of the service.

type Service

type Service struct {
	// contains filtered or unexported fields
}

Service struct represents the API service. It contains the context, cancel function, wait group, configuration, notification queue, API handler, HTTP server, and demo mail server. The context is used to manage the lifecycle of the service, while the wait group is used to wait for background processes to finish. The notification queue is used to send notifications, and the API handler is used to handle incoming requests. The HTTP server is used to serve the API endpoints, and the demo mail server is used to simulate sending emails in demo mode. The demo mail server is a fake SMTP server that captures emails sent to it for testing purposes. The demo mail inbox is a channel that receives the captured emails.

func New

func New(ctx context.Context, cfg *Config, nq notification.Queue) (*Service, error)

New function creates a new service instance. It takes a context, a config struct, and a notification queue as parameters. It returns a pointer to the service instance and an error if something goes wrong during the process. The function is responsible for setting up the service and its dependencies. It handles the configuration, rate limiting, and HTTP server setup. It also manages the demo mode functionality, including the demo mail server and inbox. The function is designed to be used as a constructor for the service and is responsible for initializing all the necessary components for the service to function properly.

func (*Service) Ping

func (s *Service) Ping() bool

Ping method checks if the service is up and running. It sends a GET request to the health check endpoint and returns true if the response status code is 200 OK, otherwise it returns false. If something goes wrong during the process, it returns false.

func (*Service) Start

func (s *Service) Start() error

Start method starts the service by starting the http server.

func (*Service) Stop

func (s *Service) Stop()

Stop method stops the service. It cancels the context and waits for the background processes to finish. It also closes the http server and the demo mail server if it is running.

func (*Service) WaitToShutdown

func (s *Service) WaitToShutdown() error

WaitToShutdown method waits for the service to shutdown. It listens for the interrupt signal and shutdown the http server and the service. If something goes wrong during the process, it returns an error.

type TokenRequest

type TokenRequest struct {
	Email string `json:"email"`
}

type TokenStatusRequest

type TokenStatusRequest struct {
	Token string `json:"token"`
	Email string `json:"email"`
}

type TokenStatusResponse

type TokenStatusResponse struct {
	Valid      bool      `json:"valid"`
	Expiration time.Time `json:"expiration"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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