usersfx

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

Contributors Forks Stargazers Issues Apache 2.0 License


usersfx

Users domain module for go-core-fx — registration and login by Telegram identity, user lookup, an Uber Fx module and a bun-backed repository.

Report Bug · Request Feature

Table of Contents


About The Project

usersfx is the users domain for the go-core-fx ecosystem. The root package holds the domain (User, Identity), the Service (RegisterOrLogin, Login, GetUser), the Repository port and the sentinel errors; Module() registers the Uber Fx module "users" with a named logger. Persistence ships as the nested bun module:

  • Domain and port — domain.go / errors.go / repository.go; the service never imports a storage driver
  • Fx module — Module() registers module "users" and decorates the injected *zap.Logger with that name
  • bun repository — the github.com/go-core-fx/usersfx/bun submodule maps users / user_identities tables (timestamps via bunfx.TimedModel), registers a user and identity in one transaction and handles the concurrent-registration race
  • Strict lint config — a v2.14.0 golangci-lint configuration with a large strict-but-practical linter set
  • Makefile + CI — formatting, linting, race-enabled tests, coverage, benchmarks, nested-module checks, and GitHub Actions workflows that run them

Features

  • User registration and login by identity — Service.RegisterOrLogin validates the identity, returns an existing user or creates one atomically; duplicate identities from concurrent requests resolve to the existing user
  • User lookup — Service.GetUser by user ID; all failures surface as wrapped sentinel errors (ErrValidationFailed, ErrNotFound, ErrIdentityExists)
  • bun persistence submodule — bun.NewRepository(db) implements the usersfx.Repository port over an injected *bun.DB; the adapter is bound to the port by the consuming application, not by this module
  • Strict golangci-lint — the same golden config used across the go-core-fx modules
  • CI pipeline — lint, race-enabled tests with coverage upload to Codecov, and benchmark tracking with alerting

Built With

  • Go
  • Uber Fx
  • bun
  • zap

(back to top)


Getting Started

Prerequisites

  • Go 1.24.3 or newer for the root module (go.mod requires go 1.24.3); the bun submodule requires go 1.26.1
  • golangci-lint v2 (the config targets v2.14.0) for make lint and make fmt
  • GNU Make

Installation

go get github.com/go-core-fx/usersfx

Add the persistence submodule only if you need it:

go get github.com/go-core-fx/usersfx/bun

(back to top)


Usage

Module Wiring

Module() provides the *Service and decorates the logger; the application must also provide a usersfx.Repository (the port is bound to its adapter in the composition root):

fx.New(
    logger.Module(),
    bunfx.Module(),
    usersfx.Module(),
    // fx.Provide(...): *sql.DB, schema.Dialect, usersfx.Repository
)

Providing the Repository

The bun submodule implements the port over an injected *bun.DB:

import usersfx "github.com/go-core-fx/usersfx"
import usersbun "github.com/go-core-fx/usersfx/bun"

fx.Provide(func(db *bun.DB) usersfx.Repository {
    return usersbun.NewRepository(db)
})

The consuming application owns the driver and dialect: it opens the *sql.DB, supplies the matching schema.Dialect to bunfx.New, and owns schema migrations for the users / user_identities tables.

The schema must enforce UNIQUE (provider, provider_id) on user_identities. RegisterOrLogin relies on the database rejecting a duplicate identity insert to resolve concurrent registrations for the same identity; without that constraint, two concurrent registrations can both commit and the same provider ID will map to two users.

(back to top)


Development

Make Targets

Target Description
make all Run all checks: fmt, lint, coverage, nested
make version Display the current version (from git describe, defaults to 0.0.0)
make fmt Run go generate ./..., then golangci-lint fmt
make lint Run golangci-lint run --timeout=5m
make test Run tests with race detector, shuffle and atomic coverage
make coverage Produce coverage.out and an HTML report (coverage.html)
make nested Build, vet, test and lint every nested module (auto-discovered go.mod directories below the root)
make benchmark Run benchmarks into benchmark.txt
make air Run the development server with air (TZ=UTC DEBUG=1)
make deps Download module dependencies
make gen Run go generate ./...
make release Create a snapshot release with goreleaser
make build Build the project into bin/
make clean Remove coverage output, benchmarks, dist/ and bin/
make help List all available targets

Continuous Integration

The Go workflow runs on pushes to master and on pull requests (path-filtered to Go sources, go.mod/go.sum, the lint config and the workflows themselves):

  • Lint — golangci-lint with the repository config
  • Test — race-enabled, shuffled tests with coverage uploaded to Codecov
  • Benchmark — runs go test -bench, keeps history in the Actions cache, uploads results when benchmarks exist and fails the job on a performance alert
  • E2E Test — present but disabled (if: false)

The Nested modules workflow runs make nested on the same triggers: root patterns like ./... stop at a nested module's boundary, so each module (e.g. bun) gets its own build, vet, test and lint pass. Discovery lives in the Makefile — any directory with its own go.mod is picked up automatically.

A second workflow marks issues and pull requests stale after 7 days of inactivity and closes them 7 days later.

(back to top)


Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

Please run make all before opening the pull request — it formats, lints and tests the change.

(back to top)


License

Distributed under the Apache License 2.0. See LICENSE for more information.

(back to top)


Acknowledgments

(back to top)

Documentation

Index

Constants

View Source
const (
	StatusActive   Status = "active"
	StatusInactive Status = "inactive"

	ProviderTelegram Provider = "telegram"
)

Variables

View Source
var (
	ErrValidationFailed = errors.New("validation failed")
	ErrNotFound         = errors.New("user not found")
	ErrIdentityExists   = errors.New("identity already exists")
)

Functions

func Module

func Module() fx.Option

Types

type Identity

type Identity struct {
	Provider     Provider
	ProviderID   string
	ProviderData string
}

func (Identity) Validate

func (i Identity) Validate() error

type Provider

type Provider string

type Repository

type Repository interface {
	RegisterOrLogin(ctx context.Context, ident Identity) (*User, error)
	Login(ctx context.Context, ident Identity) (*User, error)
	GetUser(ctx context.Context, userID string) (*User, error)
}

type Service

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

func New

func New(
	users Repository,
	logger *zap.Logger,
) *Service

func (*Service) GetUser

func (s *Service) GetUser(ctx context.Context, userID string) (*User, error)

func (*Service) Login

func (s *Service) Login(ctx context.Context, ident Identity) (*User, error)

func (*Service) RegisterOrLogin

func (s *Service) RegisterOrLogin(ctx context.Context, ident Identity) (*User, error)

type Status

type Status string

type User

type User struct {
	ID     string
	Status Status

	CreatedAt time.Time
	UpdatedAt time.Time
}

Jump to

Keyboard shortcuts

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