Documentation
¶
Index ¶
- Constants
- Variables
- func BuildAppOpts() []fx.Option
- func BuildServerOpts() []fx.Option
- func ErrInvalidRequest(err error) render.Renderer
- func ErrRender(err error) render.Renderer
- func ErrUnauthorized(err error) render.Renderer
- func ErrUnknown(err error) render.Renderer
- func NewFxLogger(logger LoggerService) fxevent.Logger
- func NewRouter() *chi.Mux
- func NewServer(lc fx.Lifecycle, router *chi.Mux, logger LoggerService) *http.Server
- type AuthService
- type AuthServiceParams
- type AuthServiceResult
- type Claims
- func (c *Claims) GetAudience() (jwt.ClaimStrings, error)
- func (c *Claims) GetExpirationTime() (*jwt.NumericDate, error)
- func (c *Claims) GetIssuedAt() (*jwt.NumericDate, error)
- func (c *Claims) GetIssuer() (string, error)
- func (c *Claims) GetNotBefore() (*jwt.NumericDate, error)
- func (c *Claims) GetSubject() (string, error)
- type Controller
- type ControllerOption
- type DBService
- type DBServiceParams
- type DbServiceResult
- type ErrResponse
- type LoggerService
- type LoggerServiceParams
- type LoggerServiceResult
- type Model
- type Repository
- type Resource
- type ResourceContextKey
- type ResourceRepositoryOption
- type ResourceRequestConstructor
- type Route
- type Service
- type ServiceOption
- type ServiceQuery
- type User
- type UserService
Constants ¶
View Source
const (
AuthHeaderName = "Authorization"
)
View Source
const (
QueryTimeout = time.Second
)
View Source
const (
TokenExpirationTime = time.Hour * 24
)
Variables ¶
View Source
var ErrNotFound = &ErrResponse{HTTPStatusCode: 404, StatusText: "Resource not found."}
View Source
var ErrRecordNotFound = errors.New("record not found")
Functions ¶
func BuildAppOpts ¶
func BuildServerOpts ¶
func ErrInvalidRequest ¶
func ErrUnauthorized ¶
func ErrUnknown ¶
func NewFxLogger ¶
func NewFxLogger(logger LoggerService) fxevent.Logger
Types ¶
type AuthService ¶
type AuthServiceParams ¶
type AuthServiceParams struct {
fx.In
Logger LoggerService
UserService UserService
}
type AuthServiceResult ¶
type AuthServiceResult struct {
fx.Out
AuthService AuthService
}
func NewAuthService ¶
func NewAuthService(params AuthServiceParams) (AuthServiceResult, error)
type Claims ¶ added in v0.0.2
type Claims struct {
Sub uint `json:"sub"`
Exp time.Time `json:"exp"`
Iat time.Time `json:"iat"`
Nbf time.Time `json:"nbf"`
Aud string `json:"aud"`
Iss string `json:"iss"`
}
func (*Claims) GetAudience ¶ added in v0.0.2
func (c *Claims) GetAudience() (jwt.ClaimStrings, error)
func (*Claims) GetExpirationTime ¶ added in v0.0.2
func (c *Claims) GetExpirationTime() (*jwt.NumericDate, error)
func (*Claims) GetIssuedAt ¶ added in v0.0.2
func (c *Claims) GetIssuedAt() (*jwt.NumericDate, error)
func (*Claims) GetNotBefore ¶ added in v0.0.2
func (c *Claims) GetNotBefore() (*jwt.NumericDate, error)
func (*Claims) GetSubject ¶ added in v0.0.2
type Controller ¶
type Controller[M Resource] interface { List(w http.ResponseWriter, r *http.Request) Create(w http.ResponseWriter, r *http.Request) Get(w http.ResponseWriter, r *http.Request) Update(w http.ResponseWriter, r *http.Request) Delete(w http.ResponseWriter, r *http.Request) ItemFromContext(ctx context.Context) (M, error) ItemContextMiddleware(next http.Handler) http.Handler UserAccessMiddleware(next http.Handler) http.Handler GetRouter() *chi.Mux }
func NewController ¶
func NewController[M Resource]( svc Service[M], logger LoggerService, authSvc AuthService, createRequestConstructor ResourceRequestConstructor[M], updateRequestConstructor ResourceRequestConstructor[M], opts ...ControllerOption[M], ) Controller[M]
type ControllerOption ¶
type ControllerOption[M Resource] func(*controller[M])
func WithContextKey ¶
func WithContextKey[M Resource](key ResourceContextKey) ControllerOption[M]
func WithDetailRoute ¶
func WithDetailRoute[M Resource](method, path string, handler http.HandlerFunc) ControllerOption[M]
type DBService ¶ added in v0.0.3
type DBService interface {
CreateOne(ctx context.Context, record interface{}) error
UpdateOne(ctx context.Context, recordID uint, record interface{}) error
DEPUpdateOne(ctx context.Context, record interface{}) error
DeleteOne(ctx context.Context, recordID uint, record interface{}) error
FindOne(
ctx context.Context,
result interface{},
joins []string,
preloads []string,
query interface{},
args ...interface{},
) error
FindMany(
ctx context.Context,
result interface{},
joins []string,
preloads []string,
query interface{},
args ...interface{},
) error
GetSession(ctx context.Context) (*gorm.DB, context.CancelFunc)
Migrate(ctx context.Context) error
DropAll(ctx context.Context) error
}
type DBServiceParams ¶ added in v0.0.3
type DbServiceResult ¶ added in v0.0.3
func NewDBService ¶ added in v0.0.3
func NewDBService(params DBServiceParams) (DbServiceResult, error)
type ErrResponse ¶
type ErrResponse struct {
Err error `json:"-"` // low-level runtime error
HTTPStatusCode int `json:"-"` // http response status code
StatusText string `json:"status"` // user-level status message
AppCode int64 `json:"code,omitempty"` // application-specific error code
ErrorText string `json:"error,omitempty"` // application-level error message, for debugging
}
func (*ErrResponse) Render ¶
func (e *ErrResponse) Render(w http.ResponseWriter, r *http.Request) error
type LoggerService ¶
type LoggerServiceParams ¶
type LoggerServiceResult ¶
type LoggerServiceResult struct {
fx.Out
LoggerService LoggerService
}
func NewLoggerService ¶
func NewLoggerService(params LoggerServiceParams) (LoggerServiceResult, error)
type Repository ¶ added in v0.0.3
type Repository[M Model] interface { FindOne(ctx context.Context, query string, args ...interface{}) (M, error) FindOneByID(ctx context.Context, itemID uint, query string, args ...interface{}) (M, error) FindOneByUser(ctx context.Context, userID uint, query string, args ...interface{}) (M, error) FindManyByUser(ctx context.Context, userID uint, query string, args ...interface{}) ([]M, error) CreateOne(ctx context.Context, item M) error UpdateOne(ctx context.Context, itemID uint, item M) error DeleteOne(ctx context.Context, itemID uint) error }
func NewResourceRepository ¶ added in v0.0.3
func NewResourceRepository[M Model]( db DBService, logger LoggerService, opts ...ResourceRepositoryOption[M], ) Repository[M]
type ResourceContextKey ¶
type ResourceContextKey int
type ResourceRepositoryOption ¶ added in v0.0.3
type ResourceRepositoryOption[M Model] func(*repository[M])
func WithJoinTables ¶ added in v0.0.3
func WithJoinTables[M Model](joinTables ...string) ResourceRepositoryOption[M]
func WithPreloadTables ¶ added in v0.0.3
func WithPreloadTables[M Model](preloadTables ...string) ResourceRepositoryOption[M]
func WithTableName ¶ added in v0.0.3
func WithTableName[M Model](tableName string) ResourceRepositoryOption[M]
type Service ¶ added in v0.0.3
type Service[M Resource] interface { ListByUser(ctx context.Context, userID uint) ([]M, error) CreateOne(ctx context.Context, userID uint, item M) (M, error) GetOne(ctx context.Context, itemID uint) (M, error) UpdateOne(ctx context.Context, itemID uint, item M) (M, error) DeleteOne(ctx context.Context, itemID uint) error }
func NewService ¶ added in v0.0.3
func NewService[M Resource]( repo Repository[M], opts ...ServiceOption[M], ) Service[M]
type ServiceOption ¶ added in v0.0.3
type ServiceOption[M Resource] func(*service[M])
func WithGetQuery ¶ added in v0.0.3
func WithGetQuery[M Resource](query string, args ...interface{}) ServiceOption[M]
func WithListQuery ¶ added in v0.0.3
func WithListQuery[M Resource](query string, args ...interface{}) ServiceOption[M]
type ServiceQuery ¶ added in v0.0.3
type ServiceQuery struct {
Filter string
Args []interface{}
}
type UserService ¶ added in v0.0.3
type UserService interface {
ListUsers(ctx context.Context) ([]User, error)
CreateUser(ctx context.Context, user User) (User, error)
GetUserByID(ctx context.Context, userID uint) (User, error)
GetUserByCredentials(ctx context.Context, username, passwordHash string) (User, error)
UpdateUserPassword(ctx context.Context, userID uint, password string) error
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.