Documentation
¶
Index ¶
- type ContextKey
- type Handler
- func (h *Handler) AddResult(w http.ResponseWriter, r *http.Request)
- func (h *Handler) ApplyRoutes()
- func (h *Handler) CheckIsAdmin(fn http.HandlerFunc) http.HandlerFunc
- func (h *Handler) ConfirmRegistration(w http.ResponseWriter, r *http.Request)
- func (h *Handler) CreateBet(w http.ResponseWriter, r *http.Request)
- func (h *Handler) CreateEvent(w http.ResponseWriter, r *http.Request)
- func (h *Handler) GetBets(w http.ResponseWriter, r *http.Request)
- func (h *Handler) GetCurrentUser(w http.ResponseWriter, r *http.Request)
- func (h *Handler) GetEvents(w http.ResponseWriter, r *http.Request)
- func (h *Handler) GetFighters(w http.ResponseWriter, r *http.Request)
- func (h *Handler) HealthCheck(w http.ResponseWriter, r *http.Request)
- func (h *Handler) IfLoggedIn(fn http.HandlerFunc) http.HandlerFunc
- func (h *Handler) Login(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Logout(w http.ResponseWriter, r *http.Request)
- func (h *Handler) RecoverPassword(w http.ResponseWriter, r *http.Request)
- func (h *Handler) Register(w http.ResponseWriter, r *http.Request)
- func (h *Handler) ResetPassword(w http.ResponseWriter, r *http.Request)
- func (h *Handler) RunHTTPServer(ctx context.Context) error
- func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContextKey ¶
type ContextKey string
ContextKey represents a custom type for identifying context keys in HTTP requests.
const ( ContextKeyHost ContextKey = "host" ContextKeyPath ContextKey = "path" ContextKeyRemoteAddr ContextKey = "remote_addr" ContextKeyCFConnectingIP ContextKey = "cf_connecting_ip" )
Constants representing various context keys for use in HTTP requests.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler defines a movie handler.
func New ¶
func New(ctrl *pickfighter.Controller) *Handler
func (*Handler) ApplyRoutes ¶
func (h *Handler) ApplyRoutes()
ApplyRoutes sets up the API routes for related services. It associates each route with the corresponding handler method from the service. The routes include user registration, login, logout, password reset, password recovery, and profile retrieval.
func (*Handler) CheckIsAdmin ¶
func (h *Handler) CheckIsAdmin(fn http.HandlerFunc) http.HandlerFunc
CheckIsAdmin is a middleware that verifies if the user is logged in as an administrator. It checks the "admin" claim in the JWT (JSON Web Token) stored in the request cookie. If the user is an administrator, the request continues; otherwise, it responds with an error.
func (*Handler) ConfirmRegistration ¶
func (h *Handler) ConfirmRegistration(w http.ResponseWriter, r *http.Request)
ConfirmRegistration handles the confirmation of user registration by validating the provided token. Users receive a confirmation token upon successful registration, and this endpoint is used to confirm and activate their accounts
func (*Handler) CreateEvent ¶
func (h *Handler) CreateEvent(w http.ResponseWriter, r *http.Request)
func (*Handler) GetCurrentUser ¶
func (h *Handler) GetCurrentUser(w http.ResponseWriter, r *http.Request)
GetCurrentUser retrieves information about the currently authenticated user. It extracts the user ID from the request context, queries the database for the user's details, and responds with a JSON representation of the user.
func (*Handler) GetFighters ¶
func (h *Handler) GetFighters(w http.ResponseWriter, r *http.Request)
GetFighters handles HTTP requests to retrieve fighters based on status.
func (*Handler) HealthCheck ¶
func (h *Handler) HealthCheck(w http.ResponseWriter, r *http.Request)
func (*Handler) IfLoggedIn ¶
func (h *Handler) IfLoggedIn(fn http.HandlerFunc) http.HandlerFunc
IfLoggedIn is a middleware that checks if a user is logged in based on the provided JWT token. If the token is valid, it extracts user information such as user ID and claims, and adds them to the request context. If the token is invalid or missing, it returns an unauthorized response.
func (*Handler) Login ¶
func (h *Handler) Login(w http.ResponseWriter, r *http.Request)
Login handles the user login process, authenticating the user based on the provided credentials. It validates the email or username and password, checks user activation status, generates a JWT token for the authenticated user, and sets an authentication cookie.
func (*Handler) Logout ¶
func (h *Handler) Logout(w http.ResponseWriter, r *http.Request)
Logout handles the user logout process by setting an expired cookie.
func (*Handler) RecoverPassword ¶
func (h *Handler) RecoverPassword(w http.ResponseWriter, r *http.Request)
RecoverPassword handles the process of recovering a user's password based on a provided reset token. It expects a JSON request containing the reset token, new password, and confirmation password. If the token is valid, the password is updated, and the token is marked as used. The response includes a successful result if the password recovery process is completed. In case of errors, appropriate error responses are sent with details in the response body.
func (*Handler) Register ¶
func (h *Handler) Register(w http.ResponseWriter, r *http.Request)
Register handles the registration of a new user. It expects a JSON request with user details, including name, email, password, and terms agreement. Upon successful registration, it initiates a confirmation email and returns the user's ID.
func (*Handler) ResetPassword ¶
func (h *Handler) ResetPassword(w http.ResponseWriter, r *http.Request)
ResetPassword handles the process of resetting a user's password. It expects a JSON request containing the user's email address. If the email is valid and associated with an existing user, a reset token is generated, and an email containing the reset link is sent to the user. The reset token is also stored in the database for verification during the password reset process. A successful response is returned if the email exists, and the reset process is initiated. In case of errors, appropriate error responses are sent with details in the response body.
func (*Handler) RunHTTPServer ¶
RunHTTPServer starts the HTTP server for the API handler with specified routes and services. It sets the health check route and adds routes for each registered service. The server listens on the specified address, and if successful, it prints the server address. It returns an error if the service address is not specified or if there is an issue starting the server.
func (*Handler) ServeHTTP ¶
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP handles the incoming HTTP request by setting CORS headers, processing preflight OPTIONS requests, and forwarding the request to the underlying router with additional context values. It checks for the "Origin" header to set CORS headers and responds to OPTIONS requests appropriately. The function logs details of the incoming request and forwards it to the router for further handling.