Documentation
¶
Index ¶
Constants ¶
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
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 ¶
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 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 ¶
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 ¶
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) 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 ¶
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"`
}