CoreGo

API Reference

Edit on GitHub

Every exported identifier of every CoreGo package, generated from the package documentation (go doc -all): signatures are exact, each entry carries the first sentence of its doc comment. Run go doc on any identifier for the full text, and see the linked guide of each package for explanations and examples.

Packages:

corego

import "github.com/berkkaradalan/CoreGo"

Package corego assembles the CoreGo building blocks - environment configuration, database connections, the authentication service, its HTTP routes and the web server - behind one facade. Guide: getting-started.md.

Constants

NameDefinitionDescription
StoreMemory= "memory"StoreMemory keeps auth records in process memory.
StoreMongo= "mongo"StoreMongo persists auth records in the connected MongoDB.
StorePostgres= "postgres"StorePostgres persists auth records in the connected PostgreSQL.
Version= "0.2.0"Version is the CoreGo release this code belongs to.

Types

AuthOptions

AuthOptions enables and configures the authentication service and its HTTP routes.

FieldTypeDescription
Serviceauth.ConfigService configures the auth service.
MailSendermail.SenderMailSender delivers the auth emails through the facade's auth.DefaultMailer, with the links derived as described for Service.
StorestringStore selects where users, sessions and tokens are kept.
TablePrefixstringTablePrefix is the prefix of the auth tables (PostgreSQL) or collections (MongoDB) of the store New creates, passed on as pgstore.Options.TablePrefix or mongostore.Options.CollectionPrefix.
CustomStoreauth.StoreCustomStore, when non-nil, is used as the auth store as-is and takes precedence over Store, COREGO_AUTH_STORE and the automatic selection (TablePrefix is ignored too).
HTTPauthhttp.ConfigHTTP configures the auth routes mounted by Core.Server and the middleware returned by Core.RequireAuth and Core.OptionalAuth.

Config

Config configures a Core.

FieldTypeDescription
Env*env.EnvEnv is the environment snapshot New reads its settings from.
Mongo*database.MongoConfigMongo connects to MongoDB.
Postgres*database.PostgresConfigPostgres connects to PostgreSQL.
Auth*AuthOptionsAuth enables the authentication service and its HTTP routes.
ServerServerOptionsServer configures the web server returned by Core.Server.
Logger*slog.LoggerLogger receives the facade's startup log (warnings and the "corego: auth ready" summary), the request log, the web server's logs, the emails of COREGO_MAIL=log and, unless AuthOptions.Service.Logger is set, the auth service's logs; nil selects a colored console logger in a terminal and slog.Default() otherwise.

Core

Core is the root handle of a CoreGo application.

FieldTypeDescription
Env*env.EnvEnv is the environment snapshot New read its settings from.
Mongo*database.MongoDBMongo is the MongoDB connection; nil when MongoDB is not configured.
Postgres*database.PostgresDBPostgres is the PostgreSQL connection; nil when PostgreSQL is not configured.
Auth*auth.AuthAuth is the authentication service; nil when Config.Auth is nil.
Function / methodDescription
func New(cfg *Config) (*Core, error)New loads the environment (unless Config.Env provides it), connects the configured databases and, when Config.Auth is set, starts the authentication service on the selected store and logs one "corego: auth ready" line at Info level that summarizes the resolved auth setup.
func (c *Core) AuthHandlers() *authhttp.HandlersAuthHandlers returns the HTTP layer of the auth service, the same instance whose routes Server mounts, for advanced use such as protecting plain net/http handlers with its Middleware or reading the session token with Token.
func (c *Core) Close() errorClose releases everything the Core owns.
func (c *Core) OptionalAuth() web.MiddlewareFuncOptionalAuth returns the middleware for routes that serve signed-in and anonymous visitors alike.
func (c *Core) RequireAuth() web.MiddlewareFuncRequireAuth returns the middleware that only lets requests with a valid session through and stores the signed-in user in the request context, where handlers read it with auth.MustUser(c).
func (c *Core) Run() errorRun serves Server on ":" + PORT (Env.Port) until the process receives SIGINT or SIGTERM, shuts the server down gracefully and then calls Close.
func (c *Core) Server() *web.ServerServer returns the application's web server, building it on the first call; later calls return the same *web.Server, so it is safe to call Server from anywhere.

ServerOptions

ServerOptions configures the web server returned by Core.Server.

FieldTypeDescription
CORS*web.CORSConfigCORS enables Cross-Origin Resource Sharing with this configuration.
TrustProxyHeaders*boolTrustProxyHeaders sets web.Server.TrustProxyHeaders.
DisableRequestLogboolDisableRequestLog turns off the one-line-per-request log.
DisableBannerboolDisableBanner turns off the startup summary Run prints: the address, the connected services and every registered route.

web

import "github.com/berkkaradalan/CoreGo/web"

Package web is CoreGo's HTTP framework, built only on the standard library. Guide: web-framework.md.

Constants

NameDefinitionDescription
DefaultReadHeaderTimeout= 10 * time.SecondDefault values New assigns to the Server timeout fields.
DefaultReadTimeout= 30 * time.SecondDefault values New assigns to the Server timeout fields.
DefaultWriteTimeout= 30 * time.SecondDefault values New assigns to the Server timeout fields.
DefaultIdleTimeout= 120 * time.SecondDefault values New assigns to the Server timeout fields.
DefaultShutdownTimeout= 10 * time.SecondDefault values New assigns to the Server timeout fields.
DefaultClientIPHeader= "X-Forwarded-For"DefaultClientIPHeader is the default value of Server.ClientIPHeader.
DefaultMaxBodyBytesint64 = 1 << 20DefaultMaxBodyBytes is the default request body limit enforced by Context.Body, BindJSON and BindJSONStrict (1 MiB).
DefaultRateLimitMaxKeys= 100000DefaultRateLimitMaxKeys is the number of keys a rate limiter tracks individually when RateLimitConfig.MaxKeys is zero.
DefaultRateLimitMessage= "Too many requests. Please try again later."DefaultRateLimitMessage is the 429 error message used when RateLimitConfig.Message is empty.

Variables

NameDefinitionDescription
ErrBodyTooLarge= errors.New("web: request body exceeds the configured limit")Sentinel errors returned by Context methods.
ErrUnsupportedMediaType= errors.New("web: unsupported media type")Sentinel errors returned by Context methods.
ErrInvalidJSON= errors.New("web: invalid JSON body")Sentinel errors returned by Context methods.

Functions

SignatureDescription
func ClientKey(c *Context) stringClientKey returns a key identifying the client of c for per-client limits such as RateLimit (its default key).

Types

CORSConfig

CORSConfig configures the CORS middleware.

FieldTypeDescription
AllowedOrigins[]stringAllowedOrigins lists the origins allowed to make cross-origin requests, exactly as browsers send them ("https://app.example.com", scheme and host compared case-insensitively, a trailing slash ignored).
AllowOriginFuncfunc(origin string) boolAllowOriginFunc, when set, is consulted for origins that AllowedOrigins does not match; returning true allows the origin.
AllowedMethods[]stringAllowedMethods is sent in Access-Control-Allow-Methods.
AllowedHeaders[]stringAllowedHeaders is sent in Access-Control-Allow-Headers.
ExposedHeaders[]stringExposedHeaders lists response headers browsers may expose to the calling script (Access-Control-Expose-Headers).
AllowCredentialsboolAllowCredentials allows cookies and HTTP authentication on cross-origin requests (Access-Control-Allow-Credentials: true).
MaxAgetime.DurationMaxAge is how long browsers may cache a preflight result (Access-Control-Max-Age, whole seconds).

Context

Context carries per-request state through the middleware and handler chain.

Opaque: no exported fields.

Function / methodDescription
func (c *Context) BindJSON(v any) errorBindJSON decodes the request body into v.
func (c *Context) BindJSONStrict(v any) errorBindJSONStrict is BindJSON for clients that must send exactly the expected shape.
func (c *Context) Body() ([]byte, error)Body reads and returns the request body.
func (c *Context) Cookie(name string) (string, error)Cookie returns the value of the named request cookie.
func (c *Context) Data(code int, contentType string, b []byte) errorData writes raw bytes with the given status code and Content-Type.
func (c *Context) Deadline() (time.Time, bool)Deadline implements context.Context by delegating to the request context.
func (c *Context) DefaultQuery(name, def string) stringDefaultQuery returns the first URL query parameter with the given name, or def when the parameter is absent.
func (c *Context) Done() <-chan struct{}Done implements context.Context by delegating to the request context.
func (c *Context) Err() errorErr implements context.Context by delegating to the request context.
func (c *Context) Get(key string) (any, bool)Get returns the per-request value stored under key with Set and whether it was present.
func (c *Context) GetHeader(name string) stringGetHeader returns the first request header with the given (case-insensitive) name, or the empty string when absent.
func (c *Context) HTML(code int, s string) errorHTML writes s with the given status code and a "text/html; charset=utf-8" Content-Type.
func (c *Context) JSON(code int, v any) errorJSON serializes v as JSON and writes it with the given status code and a "application/json; charset=utf-8" Content-Type.
func (c *Context) Method() stringMethod returns the request's HTTP method.
func (c *Context) NoContent() errorNoContent writes a 204 No Content status with no body.
func (c *Context) Param(name string) stringParam returns the value of the named path parameter (":name" or "*name" segments), or the empty string when no such parameter was captured.
func (c *Context) Path() stringPath returns the decoded request path, without the query string.
func (c *Context) Queries() url.ValuesQueries returns all URL query parameters, keyed by name.
func (c *Context) Query(name string) stringQuery returns the first URL query parameter with the given name, or the empty string when absent.
func (c *Context) Redirect(code int, location string) errorRedirect writes a redirection response with the given 3xx status code (commonly 301, 302, 303, 307 or 308) and Location header.
func (c *Context) RemoteIP() stringRemoteIP returns the client IP address.
func (c *Context) Request() *http.RequestRequest returns the current *http.Request.
func (c *Context) Response() http.ResponseWriterResponse returns the http.ResponseWriter for the current request.
func (c *Context) Set(key string, value any)Set stores a per-request value for communication between middleware and handlers.
func (c *Context) SetCookie(cookie *http.Cookie)SetCookie adds a Set-Cookie header to the response.
func (c *Context) SetHeader(name, value string)SetHeader sets a response header, replacing any existing value.
func (c *Context) SetRequest(r *http.Request)SetRequest replaces the current request, typically to attach values to its context so downstream handlers (and Value) see them.
func (c *Context) Status(code int) errorStatus writes the response status code without a body.
func (c *Context) String(code int, s string) errorString writes s with the given status code and a "text/plain; charset=utf-8" Content-Type.
func (c *Context) Value(key any) anyValue implements context.Context by delegating to the request context, so values attached with SetRequest(r.WithContext(...)) are visible.
func (c *Context) Written() boolWritten reports whether the response has been started (a status code or body bytes were written, through the Context writers or Response()).

ErrorHandler

ErrorHandler turns an error returned by a handler or middleware into a response.

Go
type ErrorHandler func(*Context, error)

Group

Group bundles a path prefix with middleware so that routes registered through it share both.

Opaque: no exported fields.

Function / methodDescription
func (g *Group) Delete(path string, h HandlerFunc, mw ...MiddlewareFunc)Delete registers h for DELETE requests under the group's prefix.
func (g *Group) Get(path string, h HandlerFunc, mw ...MiddlewareFunc)Get registers h for GET requests under the group's prefix.
func (g *Group) Group(prefix string, mw ...MiddlewareFunc) *GroupGroup creates a nested group under g.
func (g *Group) Handle(method, path string, h HandlerFunc, mw ...MiddlewareFunc)Handle registers h for method and path under the group's prefix.
func (g *Group) Head(path string, h HandlerFunc, mw ...MiddlewareFunc)Head registers h for HEAD requests under the group's prefix.
func (g *Group) Options(path string, h HandlerFunc, mw ...MiddlewareFunc)Options registers h for OPTIONS requests under the group's prefix.
func (g *Group) Patch(path string, h HandlerFunc, mw ...MiddlewareFunc)Patch registers h for PATCH requests under the group's prefix.
func (g *Group) Post(path string, h HandlerFunc, mw ...MiddlewareFunc)Post registers h for POST requests under the group's prefix.
func (g *Group) Put(path string, h HandlerFunc, mw ...MiddlewareFunc)Put registers h for PUT requests under the group's prefix.
func (g *Group) Use(mw ...MiddlewareFunc)Use appends middleware to the group.

HTTPError

HTTPError is an error carrying an HTTP status code and a user-presentable message.

FieldTypeDescription
Codeint
Messagestring
Errerror
Function / methodDescription
func AsHTTPError(err error) (*HTTPError, bool)AsHTTPError returns the *HTTPError the default error handler renders for err, and reports whether there is one.
func BadRequest(message string) *HTTPErrorBadRequest returns a 400 *HTTPError.
func Conflict(message string) *HTTPErrorConflict returns a 409 *HTTPError.
func Forbidden(message string) *HTTPErrorForbidden returns a 403 *HTTPError.
func NewHTTPError(code int, message string) *HTTPErrorNewHTTPError returns an *HTTPError with the given status code and message.
func NotFound(message string) *HTTPErrorNotFound returns a 404 *HTTPError.
func TooManyRequests(message string) *HTTPErrorTooManyRequests returns a 429 *HTTPError.
func Unauthorized(message string) *HTTPErrorUnauthorized returns a 401 *HTTPError.
func (e *HTTPError) Error() stringError implements the error interface.
func (e *HTTPError) Unwrap() errorUnwrap exposes the wrapped cause, if any.

HandlerFunc

HandlerFunc is a single request handler.

Go
type HandlerFunc func(*Context) error
Function / methodDescription
func WrapHandler(h http.Handler) HandlerFuncWrapHandler adapts a standard http.Handler into a HandlerFunc, for mounting existing net/http handlers (file servers, metrics endpoints, pprof, ...) on CoreGo routes.

MiddlewareFunc

MiddlewareFunc wraps a HandlerFunc with additional behavior.

Go
type MiddlewareFunc func(HandlerFunc) HandlerFunc
Function / methodDescription
func CORS(cfg CORSConfig) MiddlewareFuncCORS returns a middleware implementing Cross-Origin Resource Sharing.
func RateLimit(cfg RateLimitConfig) MiddlewareFuncRateLimit returns an in-memory rate limiting middleware built on a RateLimiter.
func Recovery() MiddlewareFuncRecovery returns a middleware that converts panics raised downstream into a logged 500 response and keeps the server running.
func RequestLogger(logger *slog.Logger) MiddlewareFuncRequestLogger returns a middleware that logs one line per request with the attributes method, path, status, duration and remote_ip.
func WrapMiddleware(mw func(http.Handler) http.Handler) MiddlewareFuncWrapMiddleware adapts a standard net/http middleware into a MiddlewareFunc.

RateLimitConfig

RateLimitConfig configures the RateLimit middleware and NewRateLimiter.

FieldTypeDescription
LimitintLimit is the maximum number of requests per Window for one key.
Windowtime.DurationWindow is the length of the rate window.
Keyfunc(*Context) stringKey derives the bucket a request counts against.
MessagestringMessage is the error message of the 429 response.
MaxKeysintMaxKeys bounds the number of keys tracked individually.

RateLimiter

RateLimiter is an in-memory, per-key rate limiter.

Opaque: no exported fields.

Function / methodDescription
func NewRateLimiter(cfg RateLimitConfig) *RateLimiterNewRateLimiter returns a RateLimiter enforcing cfg.Limit requests per cfg.Window for each key, tracking at most cfg.MaxKeys keys.
func (l *RateLimiter) Allow(key string) (allowed bool, retryAfter time.Duration, remaining int)Allow records one request for key if it fits in the limit.

Route

Route describes one registered route.

FieldTypeDescription
Methodstring
Pathstring

Server

Server is CoreGo's web server.

FieldTypeDescription
MaxBodyBytesint64MaxBodyBytes is the largest request body Context.Body, Context.BindJSON and Context.BindJSONStrict will read.
TrustProxyHeadersboolTrustProxyHeaders makes Context.RemoteIP read the client address from ClientIPHeader.
ClientIPHeaderstringClientIPHeader is the header consulted by Context.RemoteIP when TrustProxyHeaders is true.
ReadHeaderTimeouttime.DurationTimeouts passed to the underlying http.Server when serving starts.
ReadTimeouttime.Durationdefault 30s
WriteTimeouttime.Durationdefault 30s
IdleTimeouttime.Durationdefault 120s
ShutdownTimeouttime.DurationShutdownTimeout bounds how long Run waits for in-flight requests after SIGINT or SIGTERM before closing remaining connections.
Logger*slog.LoggerLogger receives framework logs (recovered panics, unhandled errors, net/http server errors at Error level).
Function / methodDescription
func New() *ServerNew creates an empty Server with every field set to its default.
func (s *Server) Delete(path string, h HandlerFunc, mw ...MiddlewareFunc)Delete registers h for DELETE requests.
func (s *Server) Get(path string, h HandlerFunc, mw ...MiddlewareFunc)Get registers h for GET requests.
func (s *Server) Group(prefix string, mw ...MiddlewareFunc) *GroupGroup creates a route group with the given prefix and middleware.
func (s *Server) Handle(method, path string, h HandlerFunc, mw ...MiddlewareFunc)Handle registers h for requests with the given method and path, with optional per-route middleware mw (first = outermost).
func (s *Server) Head(path string, h HandlerFunc, mw ...MiddlewareFunc)Head registers h for HEAD requests.
func (s *Server) ListenAndServe(addr string) errorListenAndServe listens on the TCP address addr (for example ":8080") and serves requests until Shutdown is called, then returns nil.
func (s *Server) OnError(fn ErrorHandler)OnError installs fn as the error handler invoked when a handler or middleware returns an error.
func (s *Server) Options(path string, h HandlerFunc, mw ...MiddlewareFunc)Options registers h for OPTIONS requests.
func (s *Server) Patch(path string, h HandlerFunc, mw ...MiddlewareFunc)Patch registers h for PATCH requests.
func (s *Server) Post(path string, h HandlerFunc, mw ...MiddlewareFunc)Post registers h for POST requests.
func (s *Server) Put(path string, h HandlerFunc, mw ...MiddlewareFunc)Put registers h for PUT requests.
func (s *Server) Routes() []RouteRoutes returns every registered route, sorted by path and then method.
func (s *Server) Run(addr string) errorRun listens on addr and serves requests until the process receives SIGINT or SIGTERM, then shuts down gracefully.
func (s *Server) Serve(ln net.Listener) errorServe accepts connections on ln and serves requests until Shutdown is called, then returns nil.
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)ServeHTTP implements http.Handler.
func (s *Server) Shutdown(ctx context.Context) errorShutdown gracefully shuts down the running server.
func (s *Server) Use(mw ...MiddlewareFunc)Use appends global middleware.

auth

import "github.com/berkkaradalan/CoreGo/auth"

Package auth provides authentication and session management for applications built with CoreGo. Guide: authentication.md.

Constants

NameDefinitionDescription
DefaultSessionTTL= 7 * 24 * time.HourDefaultSessionTTL is the lifetime of a regular session.
DefaultRememberMeTTL= 30 * 24 * time.HourDefaultRememberMeTTL is the lifetime of a session created with the remember me flag set.
DefaultEmailVerificationTokenTTL= 24 * time.HourDefaultEmailVerificationTokenTTL is the lifetime of an email verification token.
DefaultPasswordResetTokenTTL= time.HourDefaultPasswordResetTokenTTL is the lifetime of a password reset token.
DefaultMinPasswordLength= 8DefaultMinPasswordLength is the minimum password length in bytes enforced by the default password policy.
DefaultMaxPendingEmails= 100DefaultMaxPendingEmails is the maximum number of background email jobs (token creation and delivery) in flight at once.
CredentialProvider= "credential"CredentialProvider is the provider name used for email and password (credential) accounts.

Variables

NameDefinitionDescription
ErrInvalidConfig= errors.New("auth: invalid configuration")ErrInvalidConfig is returned by New when the configuration or the store is not usable.
ErrNameRequired= errors.New("auth: name is required")ErrNameRequired is returned when a signup omits the display name.
ErrNameTooLong= errors.New("auth: name is too long")ErrNameTooLong is returned by SignUp and UpdateUser when the display name is longer than 256 characters (Unicode code points, counted after trimming surrounding whitespace).
ErrInvalidImageURL= errors.New("auth: image must be an absolute http or https URL")ErrInvalidImageURL is returned by SignUp and UpdateUser when the image is neither empty nor an absolute http or https URL (with a host) of at most 2048 bytes, measured after trimming surrounding whitespace.
ErrEmailRequired= errors.New("auth: email is required")ErrEmailRequired is returned when an email address is required but missing.
ErrInvalidEmail= errors.New("auth: invalid email address")ErrInvalidEmail is returned when an email address is malformed.
ErrPasswordRequired= errors.New("auth: password is required")ErrPasswordRequired is returned when a password is required but missing.
ErrWeakPassword= errors.New("auth: password does not meet the requirements")ErrWeakPassword is returned when a password does not satisfy the configured password policy.
ErrUserAlreadyExists= errors.New("auth: user with this email already exists")ErrUserAlreadyExists is returned when a user with the same email already exists.
ErrUserNotFound= errors.New("auth: user not found")ErrUserNotFound is returned when the requested user does not exist.
ErrAccountNotFound= errors.New("auth: account not found")ErrAccountNotFound is returned when the requested account does not exist.
ErrInvalidCredentials= errors.New("auth: invalid email or password")ErrInvalidCredentials is returned when email or password do not match an account.
ErrInvalidCurrentPassword= errors.New("auth: current password is incorrect")ErrInvalidCurrentPassword is returned by ChangePassword, ChangeEmail and CheckPassword when the supplied password does not match the user's credential account.
ErrEmailNotVerified= errors.New("auth: email address is not verified")ErrEmailNotVerified is returned by SignIn and GetSession when Config.RequireVerifiedEmail is enabled and the user's email address has not been verified yet.
ErrEmailAlreadyVerified= errors.New("auth: email address is already verified")ErrEmailAlreadyVerified is returned by SendVerificationEmail when the user's address is already verified.
ErrUserBanned= errors.New("auth: this account has been banned")ErrUserBanned is returned when the account is currently banned.
ErrSessionNotFound= errors.New("auth: session not found")ErrSessionNotFound is returned when the requested session does not exist.
ErrSessionExpired= errors.New("auth: session has expired")ErrSessionExpired is returned when a session token is valid but the session has expired.
ErrInvalidToken= errors.New("auth: invalid token")ErrInvalidToken is returned when a verification or reset token is unknown or malformed.
ErrTokenExpired= errors.New("auth: token has expired")ErrTokenExpired is returned when a verification or reset token is recognized but has expired.
ErrMailerNotConfigured= errors.New("auth: no mailer configured")ErrMailerNotConfigured is returned by SendVerificationEmail when no mailer is configured.
ErrEmailDeliveryFailed= errors.New("auth: email delivery failed")ErrEmailDeliveryFailed wraps mailer errors returned by the synchronous SendVerificationEmail so callers can detect delivery failures with errors.Is while still seeing the cause.
ErrNotFound= errors.New("auth: record not found")ErrNotFound is returned when the requested record does not exist.
ErrDuplicate= errors.New("auth: duplicate record")ErrDuplicate is returned when a create or update violates a unique constraint, for example a second user with the same email.
ErrConflict= errors.New("auth: record was modified concurrently")ErrConflict is returned by UpdateUser and UpdateAccount when the stored record's Version no longer matches the Version of the record passed in.

Functions

SignatureDescription
func ContextWithSession(ctx context.Context, session *Session, user *User) context.ContextContextWithSession returns a copy of ctx that carries the authenticated session and user.
func HashPassword(password string) (string, error)HashPassword hashes password with bcrypt at bcrypt.DefaultCost.
func HashToken(token string) stringHashToken returns the SHA-256 hex digest of token.
func NewToken() (string, error)NewToken returns a new opaque high-entropy token.
func VerifyPassword(hash, password string) boolVerifyPassword reports whether password matches the bcrypt hash.

Types

Account

Account links a user to an authentication provider.

FieldTypeJSONDescription
IDstringid
UserIDstringuser_id
Providerstringprovider"credential", "google", "apple", "facebook", ...
ProviderAccountIDstringprovider_account_idprovider specific user id; the email for credential accounts
Passwordstringneverbcrypt hash; empty for OAuth accounts
CreatedAttime.Timecreated_at
UpdatedAttime.Timeupdated_at
Versionint64neverVersion is the optimistic concurrency counter managed by the store.

Auth

Auth is the CoreGo authentication service.

Opaque: no exported fields.

Function / methodDescription
func New(cfg Config, store Store) (*Auth, error)New creates an Auth service backed by store.
func (a *Auth) BanUser(ctx context.Context, userID, reason string, expires *time.Time) errorBanUser marks the user with the given ID as banned, records the reason and optional expiry, and immediately revokes every session so the user is signed out everywhere.
func (a *Auth) ChangeEmail(ctx context.Context, userID, newEmail, currentPassword string) (*User, error)ChangeEmail moves the user's login address to newEmail after verifying currentPassword, and returns the updated user.
func (a *Auth) ChangePassword(ctx context.Context, userID, currentPassword, newPassword string) errorChangePassword replaces the password of the user's credential account after verifying the current password, then revokes all of the user's sessions so every client must sign in again, and invalidates any outstanding password reset tokens.
func (a *Auth) CheckPassword(ctx context.Context, userID, password string) errorCheckPassword reports whether password is the current password of the user's credential account.
func (a *Auth) CleanupExpired(ctx context.Context) errorCleanupExpired removes every verification and password reset token and every session whose expiry lies before now.
func (a *Auth) CountUsers(ctx context.Context) (int64, error)CountUsers returns the total number of stored users.
func (a *Auth) DeleteUser(ctx context.Context, userID string) errorDeleteUser removes the user with the given ID together with everything that belongs to it.
func (a *Auth) GetSession(ctx context.Context, sessionToken string) (*Session, *User, error)GetSession resolves sessionToken to its session and user.
func (a *Auth) GetUser(ctx context.Context, userID string) (*User, error)GetUser returns the user with the given ID.
func (a *Auth) ListAccounts(ctx context.Context, userID string) ([]*Account, error)ListAccounts returns all provider accounts linked to the user, oldest first.
func (a *Auth) ListSessions(ctx context.Context, userID string) ([]*Session, error)ListSessions returns all sessions of userID, newest first.
func (a *Auth) ListUsers(ctx context.Context, opts ListOptions) ([]*User, error)ListUsers returns users page by page, newest first.
func (a *Auth) RequestEmailVerification(ctx context.Context, email string) errorRequestEmailVerification is the public, unauthenticated "send me a new verification link" flow, keyed by email address.
func (a *Auth) RequestPasswordReset(ctx context.Context, email string) errorRequestPasswordReset starts the password reset flow for the given email address.
func (a *Auth) ResetPassword(ctx context.Context, email, token, newPassword string) errorResetPassword consumes a password reset token and sets a new password for the matching credential account.
func (a *Auth) RevokeAllSessions(ctx context.Context, userID string) errorRevokeAllSessions deletes every session of userID.
func (a *Auth) RevokeOtherSessions(ctx context.Context, userID, keepSessionID string) errorRevokeOtherSessions deletes every session of userID except the one with the given ID.
func (a *Auth) RevokeSession(ctx context.Context, userID, sessionID string) errorRevokeSession deletes the session with the given ID, but only when it belongs to userID.
func (a *Auth) SendVerificationEmail(ctx context.Context, userID string) errorSendVerificationEmail creates a fresh verification token for the user with the given ID and emails it synchronously.
func (a *Auth) Shutdown(ctx context.Context) errorShutdown waits until every email that is being delivered in the background has been handed to the mailer (successfully or not).
func (a *Auth) SignIn(ctx context.Context, in SignInInput) (*SignInOutput, error)SignIn authenticates a user with an email address and a password and starts a new session.
func (a *Auth) SignOut(ctx context.Context, sessionToken string) errorSignOut invalidates the session identified by sessionToken.
func (a *Auth) SignUp(ctx context.Context, in SignUpInput) (*SignUpOutput, error)SignUp registers a user with a credential account and, unless Config.RequireVerifiedEmail is enabled, starts a session.
func (a *Auth) UnbanUser(ctx context.Context, userID string) errorUnbanUser clears every ban field (Banned, BanReason, BanExpires) of the user with the given ID.
func (a *Auth) UpdateUser(ctx context.Context, userID string, in UpdateUserInput) (*User, error)UpdateUser applies the non-nil fields of in to the user with the given ID and returns the updated record.
func (a *Auth) VerifyEmail(ctx context.Context, email, token string) errorVerifyEmail consumes an email verification token.

Config

Config configures an Auth service.

FieldTypeDescription
SessionTTLtime.DurationSessionTTL is how long a session stays valid.
RememberMeTTLtime.DurationRememberMeTTL is how long a session created with RememberMe set stays valid.
EmailVerificationTokenTTLtime.DurationEmailVerificationTokenTTL is how long an email verification token stays valid.
PasswordResetTokenTTLtime.DurationPasswordResetTokenTTL is how long a password reset token stays valid.
RequireVerifiedEmailboolRequireVerifiedEmail makes a verified email address mandatory for being signed in.
MinPasswordLengthintMinPasswordLength is the minimum password length in bytes.
MailerMailerMailer sends verification and password reset emails.
MaxPendingEmailsintMaxPendingEmails bounds the background email jobs in flight at once (verification and password reset emails, including the token creation of the public request flows).
Logger*slog.LoggerLogger receives background email delivery failures and configuration warnings.

DefaultMailer

DefaultMailer is the built-in Mailer.

Opaque: no exported fields.

Function / methodDescription
func NewDefaultMailer(sender mail.Sender, cfg DefaultMailerConfig) *DefaultMailerNewDefaultMailer returns a DefaultMailer that delivers through sender using the links and wording from cfg.
func (m *DefaultMailer) SendPasswordResetEmail(ctx context.Context, to, token string) errorSendPasswordResetEmail implements Mailer.
func (m *DefaultMailer) SendVerificationEmail(ctx context.Context, to, token string) errorSendVerificationEmail implements Mailer.

DefaultMailerConfig

DefaultMailerConfig configures the links and wording of the DefaultMailer emails.

FieldTypeDescription
AppNamestringAppName is shown in subjects and bodies, for example "Verify your email for Acme".
VerifyEmailURLstringVerifyEmailURL is the absolute http(s) URL the verification link points at; "token" and "email" query parameters are added to it and any existing query parameters are kept.
ResetPasswordURLstringResetPasswordURL is the absolute http(s) URL of the application's "choose a new password" page; the link carries the same "token" and "email" query parameters.

ListOptions

ListOptions controls pagination for list operations.

FieldTypeDescription
LimitintLimit is the maximum number of records returned.
OffsetintOffset skips the first Offset records.

Mailer

Mailer sends the transactional emails of the auth flows.

Go
type Mailer interface {
	// SendVerificationEmail delivers an email verification token to the
	// user with the given address.
	SendVerificationEmail(ctx context.Context, to, token string) error

	// SendPasswordResetEmail delivers a password reset token to the
	// user with the given address.
	SendPasswordResetEmail(ctx context.Context, to, token string) error
}

MemoryStore

MemoryStore is a complete in-memory Store implementation intended for development, examples and tests.

Opaque: no exported fields.

Function / methodDescription
func NewMemoryStore() *MemoryStoreNewMemoryStore returns an empty MemoryStore ready for use.
func (s *MemoryStore) CountUsers(_ context.Context) (int64, error)CountUsers implements Store.
func (s *MemoryStore) CreateAccount(_ context.Context, account *Account) errorCreateAccount implements Store.
func (s *MemoryStore) CreateSession(_ context.Context, session *Session) errorCreateSession implements Store.
func (s *MemoryStore) CreateUser(_ context.Context, user *User) errorCreateUser implements Store.
func (s *MemoryStore) CreateVerificationToken(_ context.Context, token *VerificationToken) errorCreateVerificationToken implements Store.
func (s *MemoryStore) DeleteAccount(_ context.Context, id string) errorDeleteAccount implements Store.
func (s *MemoryStore) DeleteExpiredSessions(_ context.Context, before time.Time) errorDeleteExpiredSessions implements Store.
func (s *MemoryStore) DeleteExpiredVerificationTokens(_ context.Context, before time.Time) errorDeleteExpiredVerificationTokens implements Store.
func (s *MemoryStore) DeleteSession(_ context.Context, id string) errorDeleteSession implements Store.
func (s *MemoryStore) DeleteSessionsByUser(_ context.Context, userID string) errorDeleteSessionsByUser implements Store.
func (s *MemoryStore) DeleteUser(_ context.Context, id string) errorDeleteUser implements Store.
func (s *MemoryStore) DeleteVerificationToken(_ context.Context, id string) errorDeleteVerificationToken implements Store.
func (s *MemoryStore) DeleteVerificationTokensByIdentifier(_ context.Context, identifier string) errorDeleteVerificationTokensByIdentifier implements Store.
func (s *MemoryStore) GetAccount(_ context.Context, provider, providerAccountID string) (*Account, error)GetAccount implements Store.
func (s *MemoryStore) GetAccountByUser(_ context.Context, userID, provider string) (*Account, error)GetAccountByUser implements Store.
func (s *MemoryStore) GetSession(_ context.Context, id string) (*Session, error)GetSession implements Store.
func (s *MemoryStore) GetSessionByTokenHash(_ context.Context, tokenHash string) (*Session, error)GetSessionByTokenHash implements Store.
func (s *MemoryStore) GetUser(_ context.Context, id string) (*User, error)GetUser implements Store.
func (s *MemoryStore) GetUserByEmail(_ context.Context, email string) (*User, error)GetUserByEmail implements Store.
func (s *MemoryStore) GetVerificationToken(_ context.Context, identifier, valueHash string) (*VerificationToken, error)GetVerificationToken implements Store.
func (s *MemoryStore) ListAccountsByUser(_ context.Context, userID string) ([]*Account, error)ListAccountsByUser implements Store.
func (s *MemoryStore) ListSessionsByUser(_ context.Context, userID string) ([]*Session, error)ListSessionsByUser implements Store.
func (s *MemoryStore) ListUsers(_ context.Context, opts ListOptions) ([]*User, error)ListUsers implements Store.
func (s *MemoryStore) UpdateAccount(_ context.Context, account *Account) errorUpdateAccount implements Store.
func (s *MemoryStore) UpdateSession(_ context.Context, session *Session) errorUpdateSession implements Store.
func (s *MemoryStore) UpdateUser(_ context.Context, user *User) errorUpdateUser implements Store.

Session

Session represents a server side session.

FieldTypeJSONDescription
IDstringid
UserIDstringuser_id
TokenHashstringnever
ExpiresAttime.Timeexpires_at
IPAddressstringip_address
UserAgentstringuser_agent
CreatedAttime.Timecreated_at
UpdatedAttime.Timeupdated_at
Function / methodDescription
func SessionFromContext(ctx context.Context) (*Session, bool)SessionFromContext returns the authenticated session stored by ContextWithSession and whether one is present.

SignInInput

SignInInput holds the data required to authenticate with an email address and a password.

FieldTypeDescription
EmailstringEmail is the login address; it is normalized before lookup.
PasswordstringPassword is the account password.
RememberMeboolRememberMe extends the created session's lifetime from SessionTTL to RememberMeTTL.
IPAddressstringIPAddress and UserAgent are recorded on the session for auditing.
UserAgentstring

SignInOutput

SignInOutput is the result of a successful sign-in.

FieldTypeDescription
User*UserUser is the authenticated user record.
SessionTokenstringSessionToken is the raw opaque session token, returned exactly once.
Session*SessionSession is the stored session record.

SignUpInput

SignUpInput holds the data required to register a new user with an email address and a password.

FieldTypeDescription
NamestringName is the display name.
EmailstringEmail is the login address.
PasswordstringPassword must satisfy the configured password policy.
ImagestringImage is an optional avatar URL stored on the user.
RememberMeboolRememberMe extends the created session's lifetime from SessionTTL to RememberMeTTL.
IPAddressstringIPAddress and UserAgent are recorded on the session for auditing.
UserAgentstring

SignUpOutput

SignUpOutput is the result of a successful registration.

FieldTypeDescription
User*UserUser is the created user record.
SessionTokenstringSessionToken is the raw opaque session token.
Session*SessionSession is the stored session record; it does not contain the raw token.

Store

Store persists authentication records.

Go
type Store interface {
	// User records.
	CreateUser(ctx context.Context, user *User) error
	GetUser(ctx context.Context, id string) (*User, error)
	GetUserByEmail(ctx context.Context, email string) (*User, error)
	ListUsers(ctx context.Context, opts ListOptions) ([]*User, error)
	CountUsers(ctx context.Context) (int64, error)
	UpdateUser(ctx context.Context, user *User) error
	DeleteUser(ctx context.Context, id string) error

	// Account records.
	CreateAccount(ctx context.Context, account *Account) error
	GetAccount(ctx context.Context, provider, providerAccountID string) (*Account, error)
	GetAccountByUser(ctx context.Context, userID, provider string) (*Account, error)
	ListAccountsByUser(ctx context.Context, userID string) ([]*Account, error)
	UpdateAccount(ctx context.Context, account *Account) error
	DeleteAccount(ctx context.Context, id string) error

	// Session records.
	CreateSession(ctx context.Context, session *Session) error
	GetSession(ctx context.Context, id string) (*Session, error)
	GetSessionByTokenHash(ctx context.Context, tokenHash string) (*Session, error)
	ListSessionsByUser(ctx context.Context, userID string) ([]*Session, error)
	UpdateSession(ctx context.Context, session *Session) error
	DeleteSession(ctx context.Context, id string) error
	DeleteSessionsByUser(ctx context.Context, userID string) error
	DeleteExpiredSessions(ctx context.Context, before time.Time) error

	// VerificationToken records.
	CreateVerificationToken(ctx context.Context, token *VerificationToken) error
	GetVerificationToken(ctx context.Context, identifier, valueHash string) (*VerificationToken, error)
	DeleteVerificationToken(ctx context.Context, id string) error
	DeleteVerificationTokensByIdentifier(ctx context.Context, identifier string) error
	DeleteExpiredVerificationTokens(ctx context.Context, before time.Time) error
}

UpdateUserInput

UpdateUserInput describes changes to a user profile.

FieldTypeDescription
Name*stringName replaces the display name.
Image*stringImage replaces the avatar URL.

User

User is the primary identity record.

FieldTypeJSONDescription
IDstringid
Namestringname
Emailstringemail
EmailVerifiedboolemail_verified
Imagestringimage
Bannedboolbanned
BanReasonstringban_reason
BanExpires*time.Timeban_expires (omitempty)
CreatedAttime.Timecreated_at
UpdatedAttime.Timeupdated_at
Versionint64neverVersion is the optimistic concurrency counter managed by the store.
Function / methodDescription
func MustUser(ctx context.Context) *UserMustUser returns the authenticated user stored in ctx.
func UserFromContext(ctx context.Context) (*User, bool)UserFromContext returns the authenticated user stored by ContextWithSession and whether one is present.
func (u *User) IsBanned() boolIsBanned reports whether the user is banned right now according to the wall clock.
func (u *User) IsBannedAt(t time.Time) boolIsBannedAt reports whether the user is banned at the instant t.

VerificationToken

VerificationToken is a single use token used for email verification and password reset.

FieldTypeJSONDescription
IDstringid
Identifierstringidentifier
ValueHashstringnever
ExpiresAttime.Timeexpires_at
CreatedAttime.Timecreated_at

auth/authhttp

import "github.com/berkkaradalan/CoreGo/auth/authhttp"

Package authhttp exposes the CoreGo authentication service (package auth) as JSON routes on the CoreGo web framework, and provides the middleware that protects an application's own routes. Guide: http-api.md.

Constants

NameDefinitionDescription
DefaultBasePath= "/auth"DefaultBasePath prefixes every mounted route when Config.BasePath is empty.
DefaultCookieName= "corego_session"DefaultCookieName is the session cookie name used when CookieConfig.Name is empty.
DefaultCookiePath= "/"DefaultCookiePath is the session cookie path used when CookieConfig.Path is empty.
DefaultRateLimit= 10DefaultRateLimit is the number of requests one client (and, on the routes limited per email address, one address) may send to one rate limited route per window when RateLimitConfig.Limit is zero.
DefaultRateLimitWindow= time.MinuteDefaultRateLimitWindow is the rate limit window used when RateLimitConfig.Window is zero.

Types

Config

Config configures the HTTP layer.

FieldTypeDescription
BasePathstringBasePath prefixes every mounted route.
CookieCookieConfigCookie, when enabled, additionally transports the session token as an HttpOnly cookie for browser clients.
RateLimitRateLimitConfigRateLimit limits how often one client IP may call each sensitive route (sign-up, sign-in, the email and password flows, the email change and account deletion) and how often one email address may be used on sign-in, forgot-password and verify-email/request.
TrustedOrigins[]stringTrustedOrigins lists the origins of other sites (typically your frontend on another host, such as "https://app.example.com") that may send state-changing requests authenticated with the session cookie.
VerifyEmailRedirectURLstringVerifyEmailRedirectURL is where GET {BasePath}/verify-email (the link in verification emails) sends the browser after verifying, with a "status" query parameter set to "verified", "invalid" or "expired"; existing query parameters and the fragment are kept.
Function / methodDescription
func (cfg Config) ResolvedBasePath() (string, error)ResolvedBasePath returns the prefix Mount registers the routes under for this configuration.
func (cfg Config) Validate() errorValidate reports the first invalid value in cfg as an error starting with "authhttp: ", or nil when New would accept cfg.

CookieConfig

CookieConfig describes the optional session cookie.

FieldTypeDescription
EnabledboolEnabled turns the cookie transport on.
NamestringName is the cookie name.
PathstringPath is the cookie path.
DomainstringDomain is the cookie domain.
SecureboolSecure restricts the cookie to HTTPS.
SameSitehttp.SameSiteSameSite is the cookie's SameSite policy.
OmitTokenFromBodyboolOmitTokenFromBody leaves the "token" field out of the sign-up and sign-in responses (expires_at, user and verification_required stay), so the session token only ever travels in the HttpOnly cookie and scripts in the page, including injected ones, can never read it.

Handlers

Handlers is the HTTP layer around an auth.Auth service.

Opaque: no exported fields.

Function / methodDescription
func New(service *auth.Auth, cfg Config) *HandlersNew returns Handlers that expose service over HTTP with the given configuration.
func (h *Handlers) BasePath() stringBasePath returns the resolved prefix Mount registers the routes under.
func (h *Handlers) Middleware(next http.Handler) http.HandlerMiddleware is RequireAuth for plain net/http handlers and routers other than the CoreGo web framework.
func (h *Handlers) Mount(r Router)Mount registers every auth route on r under Config.BasePath (a group's prefix comes first).
func (h *Handlers) OptionalAuth() web.MiddlewareFuncOptionalAuth returns web middleware for routes that serve signed-in and anonymous visitors alike.
func (h *Handlers) RequireAuth() web.MiddlewareFuncRequireAuth returns web middleware that only lets requests with a valid session through.
func (h *Handlers) Routes() []RouteInfoRoutes returns every auth route in registration order, sign-up first, with its full path under BasePath.
func (h *Handlers) Token(r *http.Request) (string, bool)Token returns the session token carried by r.

RateLimitConfig

RateLimitConfig configures the rate limits of the sensitive routes.

FieldTypeDescription
DisabledboolDisabled turns rate limiting off for every route, for example when a reverse proxy or API gateway already enforces limits.
LimitintLimit is the maximum number of requests per Window from one client IP to one route, and for one email address to one of the routes limited per address.
Windowtime.DurationWindow is the length of the rate window.

RouteInfo

RouteInfo describes one auth route, for listings such as the startup banner of the corego facade.

FieldTypeDescription
MethodstringMethod is the HTTP method, for example "POST".
PathstringPath is the full path under BasePath, for example "/auth/signup".
RequiresSessionboolRequiresSession reports whether the route needs a valid session token (it is wrapped in RequireAuth).

Router

Router is what Mount registers routes on.

Go
type Router interface {
	Handle(method, path string, h web.HandlerFunc, mw ...web.MiddlewareFunc)
}

auth/mongostore

import "github.com/berkkaradalan/CoreGo/auth/mongostore"

Package mongostore implements the auth.Store interface on MongoDB (driver go.mongodb.org/mongo-driver/v2), on top of the CoreGo database.MongoDB wrapper. Guide: auth-storage.md.

Constants

NameDefinitionDescription
DefaultCollectionPrefix= "corego_"DefaultCollectionPrefix is the collection name prefix used when Options.CollectionPrefix is empty.

Types

Options

Options configures a Store.

FieldTypeDescription
CollectionPrefixstringCollectionPrefix is prepended to every collection name the store uses.

Store

Store implements auth.Store on MongoDB.

Opaque: no exported fields.

Function / methodDescription
func NewStore(db *database.MongoDB, opts Options) (*Store, error)NewStore returns an auth.Store that keeps its records in the database db is bound to (see database.MongoConfig.Database).
func (s *Store) CountUsers(ctx context.Context) (int64, error)CountUsers returns the number of stored users.
func (s *Store) CreateAccount(ctx context.Context, account *auth.Account) errorCreateAccount inserts account, filling in ID and timestamps on the passed struct when the caller left them zero and setting Version to 1 (also on the passed struct).
func (s *Store) CreateSession(ctx context.Context, session *auth.Session) errorCreateSession inserts session, filling in ID and timestamps on the passed struct when the caller left them zero.
func (s *Store) CreateUser(ctx context.Context, user *auth.User) errorCreateUser inserts user.
func (s *Store) CreateVerificationToken(ctx context.Context, token *auth.VerificationToken) errorCreateVerificationToken inserts token, filling in ID and CreatedAt on the passed struct when the caller left them zero; a CreatedAt set by the caller (the auth service always sets one) is persisted as is, at millisecond precision.
func (s *Store) DeleteAccount(ctx context.Context, id string) errorDeleteAccount removes an account by ID.
func (s *Store) DeleteExpiredSessions(ctx context.Context, before time.Time) errorDeleteExpiredSessions removes every session whose ExpiresAt is strictly before the given instant and succeeds when none matches.
func (s *Store) DeleteExpiredVerificationTokens(ctx context.Context, before time.Time) errorDeleteExpiredVerificationTokens removes every token whose ExpiresAt is strictly before the given instant and succeeds when none matches.
func (s *Store) DeleteSession(ctx context.Context, id string) errorDeleteSession removes a session by ID.
func (s *Store) DeleteSessionsByUser(ctx context.Context, userID string) errorDeleteSessionsByUser removes every session of a user.
func (s *Store) DeleteUser(ctx context.Context, id string) errorDeleteUser removes the user and cascades to its accounts and sessions.
func (s *Store) DeleteVerificationToken(ctx context.Context, id string) errorDeleteVerificationToken removes a token by ID.
func (s *Store) DeleteVerificationTokensByIdentifier(ctx context.Context, identifier string) errorDeleteVerificationTokensByIdentifier removes every token whose identifier matches exactly (no prefix or case-insensitive matching) and succeeds when none matches.
func (s *Store) EnsureIndexes(ctx context.Context) errorEnsureIndexes creates the indexes the store relies on.
func (s *Store) GetAccount(ctx context.Context, provider, providerAccountID string) (*auth.Account, error)GetAccount fetches an account by provider and provider specific account ID.
func (s *Store) GetAccountByUser(ctx context.Context, userID, provider string) (*auth.Account, error)GetAccountByUser fetches the account of provider owned by userID.
func (s *Store) GetSession(ctx context.Context, id string) (*auth.Session, error)GetSession fetches a session by ID.
func (s *Store) GetSessionByTokenHash(ctx context.Context, tokenHash string) (*auth.Session, error)GetSessionByTokenHash resolves a session token hash.
func (s *Store) GetUser(ctx context.Context, id string) (*auth.User, error)GetUser fetches a user by ID.
func (s *Store) GetUserByEmail(ctx context.Context, email string) (*auth.User, error)GetUserByEmail fetches a user by exact email match.
func (s *Store) GetVerificationToken(ctx context.Context, identifier, valueHash string) (*auth.VerificationToken, error)GetVerificationToken fetches a token by its exact identifier and value hash.
func (s *Store) ListAccountsByUser(ctx context.Context, userID string) ([]*auth.Account, error)ListAccountsByUser returns the user's accounts oldest first (created_at ascending, the ID ascending as tie-breaker).
func (s *Store) ListSessionsByUser(ctx context.Context, userID string) ([]*auth.Session, error)ListSessionsByUser returns the user's sessions newest first (created_at descending, the ID descending as tie-breaker).
func (s *Store) ListUsers(ctx context.Context, opts auth.ListOptions) ([]*auth.User, error)ListUsers returns users newest first (created_at descending, the ID descending as tie-breaker) with pagination; see normalizeListOptions for how Limit and Offset are interpreted.
func (s *Store) UpdateAccount(ctx context.Context, account *auth.Account) errorUpdateAccount replaces the full account record with optimistic concurrency, exactly like UpdateUser.
func (s *Store) UpdateSession(ctx context.Context, session *auth.Session) errorUpdateSession replaces the full session record and sets UpdatedAt to the current time (also on the passed struct).
func (s *Store) UpdateUser(ctx context.Context, user *auth.User) errorUpdateUser replaces the full user record with optimistic concurrency.

auth/pgstore

import "github.com/berkkaradalan/CoreGo/auth/pgstore"

Package pgstore implements auth.Store on PostgreSQL using the pgx driver and its connection pool. Guide: auth-storage.md.

Constants

NameDefinitionDescription
DefaultTablePrefix= "corego_"DefaultTablePrefix is the table name prefix used when Options.TablePrefix is empty.
MaxTablePrefixLength= 24MaxTablePrefixLength is the longest accepted Options.TablePrefix in bytes.

Variables

NameDefinitionDescription
ErrNilPool= errors.New("pgstore: pool must not be nil")ErrNilPool is returned by NewStore when the pool is nil.
ErrInvalidTablePrefix= errors.New("pgstore: invalid table prefix")ErrInvalidTablePrefix is returned by NewStore when Options.TablePrefix contains characters other than lowercase ASCII letters, digits and underscores, or is longer than MaxTablePrefixLength.

Types

Options

Options configures a Store.

FieldTypeDescription
TablePrefixstringTablePrefix is prepended verbatim to every table name, so "myapp_" yields myapp_users, myapp_accounts and so on.

Store

Store implements auth.Store on PostgreSQL.

Opaque: no exported fields.

Function / methodDescription
func NewStore(pool *pgxpool.Pool, opts Options) (*Store, error)NewStore returns a Store that issues its queries through pool, a connected pool such as the one returned by database.PostgresDB.Pool.
func (s *Store) CountUsers(ctx context.Context) (int64, error)CountUsers implements auth.Store.
func (s *Store) CreateAccount(ctx context.Context, account *auth.Account) errorCreateAccount implements auth.Store.
func (s *Store) CreateSession(ctx context.Context, session *auth.Session) errorCreateSession implements auth.Store.
func (s *Store) CreateUser(ctx context.Context, user *auth.User) errorCreateUser implements auth.Store.
func (s *Store) CreateVerificationToken(ctx context.Context, token *auth.VerificationToken) errorCreateVerificationToken implements auth.Store.
func (s *Store) DeleteAccount(ctx context.Context, id string) errorDeleteAccount implements auth.Store.
func (s *Store) DeleteExpiredSessions(ctx context.Context, before time.Time) errorDeleteExpiredSessions implements auth.Store.
func (s *Store) DeleteExpiredVerificationTokens(ctx context.Context, before time.Time) errorDeleteExpiredVerificationTokens implements auth.Store.
func (s *Store) DeleteSession(ctx context.Context, id string) errorDeleteSession implements auth.Store.
func (s *Store) DeleteSessionsByUser(ctx context.Context, userID string) errorDeleteSessionsByUser implements auth.Store.
func (s *Store) DeleteUser(ctx context.Context, id string) errorDeleteUser implements auth.Store.
func (s *Store) DeleteVerificationToken(ctx context.Context, id string) errorDeleteVerificationToken implements auth.Store.
func (s *Store) DeleteVerificationTokensByIdentifier(ctx context.Context, identifier string) errorDeleteVerificationTokensByIdentifier implements auth.Store.
func (s *Store) GetAccount(ctx context.Context, provider, providerAccountID string) (*auth.Account, error)GetAccount implements auth.Store.
func (s *Store) GetAccountByUser(ctx context.Context, userID, provider string) (*auth.Account, error)GetAccountByUser implements auth.Store.
func (s *Store) GetSession(ctx context.Context, id string) (*auth.Session, error)GetSession implements auth.Store.
func (s *Store) GetSessionByTokenHash(ctx context.Context, tokenHash string) (*auth.Session, error)GetSessionByTokenHash implements auth.Store.
func (s *Store) GetUser(ctx context.Context, id string) (*auth.User, error)GetUser implements auth.Store.
func (s *Store) GetUserByEmail(ctx context.Context, email string) (*auth.User, error)GetUserByEmail implements auth.Store.
func (s *Store) GetVerificationToken(ctx context.Context, identifier, valueHash string) (*auth.VerificationToken, error)GetVerificationToken implements auth.Store.
func (s *Store) ListAccountsByUser(ctx context.Context, userID string) ([]*auth.Account, error)ListAccountsByUser implements auth.Store.
func (s *Store) ListSessionsByUser(ctx context.Context, userID string) ([]*auth.Session, error)ListSessionsByUser implements auth.Store.
func (s *Store) ListUsers(ctx context.Context, opts auth.ListOptions) ([]*auth.User, error)ListUsers implements auth.Store.
func (s *Store) Migrate(ctx context.Context) (err error)Migrate brings the store's tables up to date.
func (s *Store) TablePrefix() stringTablePrefix returns the effective table prefix of the store, for example "corego_" when Options.TablePrefix was left empty.
func (s *Store) UpdateAccount(ctx context.Context, account *auth.Account) errorUpdateAccount implements auth.Store with optimistic concurrency, exactly like UpdateUser.
func (s *Store) UpdateSession(ctx context.Context, session *auth.Session) errorUpdateSession implements auth.Store.
func (s *Store) UpdateUser(ctx context.Context, user *auth.User) errorUpdateUser implements auth.Store with optimistic concurrency.

database

import "github.com/berkkaradalan/CoreGo/database"

Package database connects CoreGo applications to MongoDB and PostgreSQL and offers small, context-aware helpers on top of the official drivers (go.mongodb.org/mongo-driver/v2 and github.com/jackc/pgx/v5). Guide: database.md.

Constants

NameDefinitionDescription
DefaultMongoDatabase= "corego"DefaultMongoDatabase is the database name used when neither MongoConfig.Database nor the connection URL names one.

Variables

NameDefinitionDescription
ErrNotFound= errors.New("database: not found")ErrNotFound reports that a lookup, update or delete matched nothing.

Functions

SignatureDescription
func QueryAll[T any](ctx context.Context, q Querier, sql string, args ...any) ([]T, error)QueryAll runs sql on q and scans every row into a T.
func QueryOne[T any](ctx context.Context, q Querier, sql string, args ...any) (T, error)QueryOne runs sql on q and scans the first row into a T, using the same mapping rules as QueryAll.

Types

MongoConfig

MongoConfig configures a MongoDB connection.

FieldTypeDescription
URLstringURL is the MongoDB connection string, for example "mongodb://localhost:27017/myapp" or "mongodb+srv://user:pass@cluster.example.net/myapp?retryWrites=true".
DatabasestringDatabase names the database the helpers operate on.

MongoDB

MongoDB is a connected MongoDB client bound to one database.

Opaque: no exported fields.

Function / methodDescription
func NewMongoDB(cfg MongoConfig) (*MongoDB, error)NewMongoDB connects to MongoDB, pings the server and returns a handle bound to the database resolved from cfg (see MongoConfig.Database).
func (m *MongoDB) Client() *mongo.ClientClient returns the underlying driver client, for example to start a session or a multi-document transaction.
func (m *MongoDB) Close() errorClose disconnects the client, waiting at most 10 seconds for in-flight operations.
func (m *MongoDB) Collection(name string) *mongo.CollectionCollection returns the driver handle of a collection in the configured database.
func (m *MongoDB) Count(ctx context.Context, collection string, filter any) (int64, error)Count returns the number of documents matching filter.
func (m *MongoDB) Database() *mongo.DatabaseDatabase returns the driver handle of the configured database.
func (m *MongoDB) DeleteMany(ctx context.Context, collection string, filter any) (int64, error)DeleteMany deletes every document matching filter and returns how many were deleted.
func (m *MongoDB) DeleteOne(ctx context.Context, collection string, filter any) errorDeleteOne deletes the first document matching filter.
func (m *MongoDB) Find(ctx context.Context, collection string, filter any, out any, opts ...options.Lister[options.FindOptions]) errorFind decodes every document matching filter into out, which must be a non-nil pointer to a slice (for example *[]Task).
func (m *MongoDB) FindOne(ctx context.Context, collection string, filter any, out any) errorFindOne decodes the first document matching filter into out, which must be a non-nil pointer (to a struct, a bson.M, ...).
func (m *MongoDB) InsertOne(ctx context.Context, collection string, document any) (any, error)InsertOne inserts document into collection and returns the inserted _id exactly as the driver reports it.
func (m *MongoDB) Name() stringName returns the resolved database name.
func (m *MongoDB) Ping(ctx context.Context) errorPing checks that the server is reachable.
func (m *MongoDB) UpdateMany(ctx context.Context, collection string, filter, update any) (int64, error)UpdateMany applies update to every document matching filter and returns how many documents matched.
func (m *MongoDB) UpdateOne(ctx context.Context, collection string, filter, update any) errorUpdateOne applies update (for example bson.M{"$set": ...}) to the first document matching filter.

PostgresConfig

PostgresConfig configures a PostgreSQL connection.

FieldTypeDescription
URLstringURL is the PostgreSQL connection string, for example "postgres://user:pass@localhost:5432/myapp?sslmode=disable".

PostgresDB

PostgresDB is a connected PostgreSQL connection pool.

Opaque: no exported fields.

Function / methodDescription
func NewPostgresDB(cfg PostgresConfig) (*PostgresDB, error)NewPostgresDB creates a connection pool, pings the server and returns the handle.
func (p *PostgresDB) Close() errorClose closes every connection in the pool, waiting for acquired connections to be released.
func (p *PostgresDB) Exec(ctx context.Context, sql string, args ...any) (int64, error)Exec runs a statement that returns no rows (INSERT, UPDATE, DELETE, DDL) and returns the number of affected rows.
func (p *PostgresDB) Ping(ctx context.Context) errorPing checks that the server is reachable.
func (p *PostgresDB) Pool() *pgxpool.PoolPool returns the underlying pgx connection pool.
func (p *PostgresDB) Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)Query runs a statement that returns rows and hands back the raw pgx.Rows.
func (p *PostgresDB) QueryMaps(ctx context.Context, sql string, args ...any) ([]map[string]any, error)QueryMaps runs a statement that returns rows and returns them as maps keyed by column name, for ad-hoc queries where declaring a struct is not worth it.
func (p *PostgresDB) WithTx(ctx context.Context, fn func(tx pgx.Tx) error) errorWithTx runs fn inside a transaction.

Querier

Querier is anything that can run a query returning rows.

Go
type Querier interface {
	Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
}

mail

import "github.com/berkkaradalan/CoreGo/mail"

Package mail sends email through pluggable backends. Guide: mail.md.

Types

CaptureSender

CaptureSender is a Sender that records every message it receives in memory.

Opaque: no exported fields.

Function / methodDescription
func NewCaptureSender() *CaptureSenderNewCaptureSender returns an empty CaptureSender.
func (c *CaptureSender) Last() (Message, bool)Last returns a copy of the most recently captured message and whether any message has been captured at all.
func (c *CaptureSender) Messages() []MessageMessages returns a deep copy of all captured messages in the order they were sent.
func (c *CaptureSender) Reset()Reset discards all captured messages.
func (c *CaptureSender) Send(_ context.Context, msg Message) errorSend implements Sender.

LogSender

LogSender is a Sender that logs messages instead of delivering them.

Opaque: no exported fields.

Function / methodDescription
func NewLogSender(logger *slog.Logger) *LogSenderNewLogSender returns a LogSender that logs to logger.
func (l *LogSender) Send(ctx context.Context, msg Message) errorSend implements Sender.

Message

Message represents an outgoing email message.

FieldTypeDescription
Fromstring
To[]string
Cc[]string
Bcc[]string
ReplyTostring
Subjectstring
Textstring
HTMLstring
Function / methodDescription
func (m Message) Validate() errorValidate reports whether the message satisfies the minimum rules for sending.

SMTPConfig

SMTPConfig configures an SMTPSender.

FieldTypeDescription
HoststringHost is the SMTP server hostname or IP address.
PortintPort is the SMTP server port.
UsernamestringUsername and Password enable SMTP authentication (AUTH PLAIN).
Passwordstring
FromstringFrom is the envelope sender (MAIL FROM) and the default value for Message.From when a message does not set its own From.
TLSModeTLSModeTLSMode selects STARTTLS (default), implicit TLS or plaintext.
Timeouttime.DurationTimeout bounds both the connection dial and every individual SMTP command exchange.

SMTPSender

SMTPSender delivers messages over SMTP using net/smtp.

Opaque: no exported fields.

Function / methodDescription
func NewSMTPSender(cfg SMTPConfig) *SMTPSenderNewSMTPSender returns an SMTPSender with defaults applied.
func (s *SMTPSender) Send(ctx context.Context, msg Message) errorSend implements Sender.

Sender

Sender delivers email messages.

Go
type Sender interface {
	Send(ctx context.Context, msg Message) error
}

SenderFunc

SenderFunc adapts a plain function to the Sender interface.

Go
type SenderFunc func(ctx context.Context, msg Message) error
Function / methodDescription
func (f SenderFunc) Send(ctx context.Context, msg Message) errorSend implements Sender.

TLSMode

TLSMode selects how an SMTPSender secures its connection.

Go
type TLSMode int
ConstantDefinitionDescription
TLSStartTLSTLSMode = iotaTLSStartTLS connects over plaintext and upgrades the session with the STARTTLS extension.
TLSImplicitTLSImplicit establishes TLS before the SMTP dialog starts (formerly "SMTPS", typically served on port 465).
TLSDisabledTLSDisabled speaks plaintext SMTP for the whole dialog.
Function / methodDescription
func (m TLSMode) String() stringString returns a human-readable name for the mode.

env

import "github.com/berkkaradalan/CoreGo/env"

Package env loads application configuration from the process environment and an optional .env file. Guide: environment.md.

Constants

NameDefinitionDescription
DefaultFile= ".env"DefaultFile is the file Load reads from the current working directory.
DefaultPort= "8080"DefaultPort is the value of Env.Port when PORT is unset or empty.

Types

Env

Env is an immutable snapshot of the configuration visible to the application.

FieldTypeDescription
AppURLstringAppURL is the public base URL of the application, used to build links such as the ones in verification emails.
PortstringPort is the HTTP port the application listens on (PORT, default "8080").
MongoURLstringMongoURL is the MongoDB connection string from MONGODB_CONNECTION_URL; empty when MongoDB is not configured.
MongoDatabasestringMongoDatabase is the MongoDB database name from MONGODB_DATABASE; empty when not set.
PostgresURLstringPostgresURL is the PostgreSQL connection string from POSTGRES_CONNECTION_URL; empty when PostgreSQL is not configured.
Function / methodDescription
func Load() (*Env, error)Load reads ./.env when it exists, merges it with the process environment and returns the resulting snapshot.
func LoadFile(path string) (*Env, error)LoadFile is like Load but reads the given file instead of ./.env.
func (e *Env) Bool(key string, def bool) boolBool returns the variable parsed with strconv.ParseBool (1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False), or def when it is unset, empty or not a valid boolean.
func (e *Env) Duration(key string, def time.Duration) time.DurationDuration returns the variable parsed with time.ParseDuration (for example "30s", "5m", "1h30m"), or def when it is unset, empty or not a valid duration.
func (e *Env) Get(key string) (string, bool)Get returns the raw value of a variable and whether it is set.
func (e *Env) GetOrDefault(key, def string) stringGetOrDefault returns the value of a variable with surrounding whitespace removed, or def when the variable is unset, empty or only whitespace.
func (e *Env) Int(key string, def int) intInt returns the variable parsed as a base-10 integer, or def when it is unset, empty or not a valid integer.
func (e *Env) Strings(key string) []stringStrings splits a comma separated variable into its elements, trimming whitespace around each one and dropping empty elements.

On this page