core

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUserAlreadyExists = errors.New("a user with this name already exists")
	ErrUserNotFound      = errors.New("user not found")
)
View Source
var Config = func() AppConfig {
	config := AppConfig{
		DbPath:             resolvePath(os.Getenv("GENESIS_DB_PATH")),
		BaseUrl:            os.Getenv("GENESIS_BASE_URL"),
		JWTSecret:          []byte(os.Getenv("GENESIS_JWT_SECRET")),
		JWTExpiration:      time.Duration(parseInt(os.Getenv("GENESIS_JWT_TOKEN_EXPIRATION"))) * time.Minute,
		JWTCookieAllowHTTP: os.Getenv("GENESIS_JWT_COOKIE_ALLOW_HTTP") == "true",
		AppBuildVersion:    os.Getenv("GENESIS_BUILD_VERSION"),
		AppBuildDate:       os.Getenv("GENESIS_BUILD_DATE"),
		AppBuildCommit:     os.Getenv("GENESIS_BUILD_COMMIT"),
		AppGinMode:         os.Getenv("GENESIS_GIN_MODE"),
		AppPort:            os.Getenv("GENESIS_PORT"),
		AppUsersToCreate:   parseInitialUserList(os.Getenv("GENESIS_CREATE_USERS")),
		AppUserPattern:     regexp.MustCompile(os.Getenv("GENESIS_USERNAME_PATTERN")),
		AppKeyPattern:      regexp.MustCompile(os.Getenv("GENESIS_KEY_PATTERN")),
		AppDataMaxSize:     parseInt(os.Getenv("GENESIS_DATA_MAX_SIZE")) * 1000,
		AppKeysPerUser:     parseInt(os.Getenv("GENESIS_KEYS_PER_USER")),
	}

	Logger.Debug("build info",
		zap.String("version", config.AppBuildVersion),
		zap.String("date", config.AppBuildDate),
		zap.String("commit", config.AppBuildCommit),
	)

	return config
}()
View Source
var Logger = func() *zap.Logger {
	_, filename, _, _ := runtime.Caller(0)
	var root = path.Join(path.Dir(filename), "..")

	envFile := path.Join(root, ".env")
	if testing.Testing() {
		envFile = path.Join(root, ".env.test")
	}

	envSkipped := godotenv.Load(envFile)

	var cfg zap.Config
	if os.Getenv("GENESIS_LOG_MODE") == "production" {
		cfg = zap.NewProductionConfig()
	} else {
		cfg = zap.NewDevelopmentConfig()
	}

	logger, err := cfg.Build(zap.AddCallerSkip(1))
	if err != nil {
		log.Fatal(err)
	}

	if envSkipped != nil {
		logger.Debug(".env file skipped")
	}

	return logger
}()

Functions

func CreateAuthToken

func CreateAuthToken(user *User) (string, error)

func CreateUser

func CreateUser(user User) error

func DeleteDataFromUser

func DeleteDataFromUser(name string, key string) error

func DeleteUser

func DeleteUser(name string) error

func GetAllDataFromUser

func GetAllDataFromUser(name string) ([]byte, error)

func GetDataCountForUser

func GetDataCountForUser(name, includedKey string) int64

func GetDataFromUser

func GetDataFromUser(name string, key string) ([]byte, error)

func InitializeUsers added in v1.4.0

func InitializeUsers()

func IsTokenBlacklisted

func IsTokenBlacklisted(jti string) (bool, error)

func ResetDatabase

func ResetDatabase()

func SetDataForUser

func SetDataForUser(name string, key string, data []byte) error

func StoreInvalidatedToken

func StoreInvalidatedToken(jti string, expiration time.Duration) error

func UpdateUser

func UpdateUser(name string, user PartialUser) error

Types

type AppConfig

type AppConfig struct {
	DbPath             string
	BaseUrl            string
	JWTSecret          []byte
	JWTExpiration      time.Duration
	JWTCookieAllowHTTP bool
	AppBuildVersion    string
	AppBuildDate       string
	AppBuildCommit     string
	AppGinMode         string
	AppPort            string
	AppUsersToCreate   []User
	AppUserPattern     *regexp.Regexp
	AppKeyPattern      *regexp.Regexp
	AppDataMaxSize     int64
	AppKeysPerUser     int64
}

type JWTClaim

type JWTClaim struct {
	User string `json:"user"`
	jwt.RegisteredClaims
}

func ParseAuthToken

func ParseAuthToken(token string) (*JWTClaim, error)

type PartialUser

type PartialUser struct {
	Admin    *bool   `json:"admin,omitempty"`
	Password *string `json:"password,omitempty" validate:"omitempty,gte=8,lte=64"`
}

type PublicUser

type PublicUser struct {
	Name  string `json:"name"`
	Admin bool   `json:"admin"`
}

func GetAllUsers added in v1.3.0

func GetAllUsers() ([]*PublicUser, error)

func GetUsers

func GetUsers(skip string) ([]*PublicUser, error)

type User

type User struct {
	Name     string `json:"name" validate:"required,gte=3,lte=32"`
	Admin    bool   `json:"admin"`
	Password string `json:"password" validate:"required,gte=8,lte=64"`
}

func AuthenticateUser

func AuthenticateUser(name string, password string) (*User, error)

func GetUser

func GetUser(name string) (*User, error)

Jump to

Keyboard shortcuts

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