hades

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 21 Imported by: 0

README

Hades

Project overview

This repository is a small Go library providing utilities and helper functions for website or backend api development (Gin framework is my favorite). It includes features:

  • send email by SMTP: mail.go
  • html template functions: html.go
  • token generation and validation: token.go
  • price formatting: price.go
  • spam detection: spam.go

Install

Clone or fetch the package in module mode (modules are recommended for Go 1.17+):

go get github.com/sunzhongwei/hades@latest

Upgrade to latest version

To upgrade to the latest version of the library, run:

go get -u github.com/sunzhongwei/hades

Usage

Import and use in your project:

import "github.com/sunzhongwei/hades"

// Example: call an exported function from the library (adjust to actual exported symbols)
// result := hades.SomeFunction(args)

License

MIT License. See the LICENSE file for details.

Documentation

Index

Constants

View Source
const Version = "v1.0.4"

Variables

View Source
var (
	// ErrInvalidToken token format invalid
	ErrInvalidToken = errors.New("invalid token format")
	// ErrSignatureMismatch token signature verification failed
	ErrSignatureMismatch = errors.New("token signature mismatch")
	// ErrExpired token expired
	ErrExpired = errors.New("token expired")
)
View Source
var TemplateFuncMap = template.FuncMap{
	"sub":            Sub,
	"add":            Add,
	"until":          Until,
	"split":          Split,
	"formatPrice":    FormatPrice,
	"SafeHTML":       SafeHTML,
	"SafeJS":         SafeJS,
	"FormatDate":     FormatDate,
	"FormatDateEn":   FormatDateEn,
	"FormatDateCn":   FormatDateCn,
	"GenDescription": GenDescription,
	"CurrentYear":    CurrentYear,
}

Functions

func Add

func Add(a, b int) int

Add returns a + b

func CountURLs

func CountURLs(text string) int

文本中包含 URL 的数量。包含 http/https 链接

func CurrentYear added in v1.0.3

func CurrentYear() int

Get Current Year

func FirstNChars

func FirstNChars(s string, n int) string

FirstNChars 字符串的前 N 个字符 fmt.Println(firstN2("世界 Hello", 1)) // 世

func FormatDate

func FormatDate(t time.Time) string

Datetime to string, format: 2006-01-02

func FormatDateCn added in v1.0.2

func FormatDateCn(t time.Time) string

Format Datetime As Chinese Locale, E.g: 2006年01月02日

func FormatDateEn

func FormatDateEn(t time.Time) string

Format Datetime As English Locale, E.g: January 02, 2006

func FormatPrice

func FormatPrice(price float64) string

将价格格式化为 xx,xx.yy 的形式

func GenDescription

func GenDescription(html string) string

GenDescription 文章概要

func GenerateToken

func GenerateToken(resource string, ttl time.Duration) (string, error)

GenerateToken creates a signed token for the given resource that expires after ttl.

func GetDomain

func GetDomain(debug bool) string

func HasRussianChars

func HasRussianChars(text string) bool

判断是否包含俄语字符

func IsValidEmail added in v1.0.1

func IsValidEmail(email string) bool

email address validation regex

func MarkdownToHTML

func MarkdownToHTML(markdown string) template.HTML

MarkdownToHTML converts markdown to html

func SafeHTML

func SafeHTML(s string) template.HTML

func SafeJS

func SafeJS(s string) template.JS

func SendMail

func SendMail(to, cc, subject, body string, config SMTPConfig) error

SendMail 发送邮件 配置参数示例:

{
	"Host": "smtp.163.com",
	"Port": 465,
	"User": "user@163.com",
	"Pass": "password",
	"UserName": "发件人名称"
}

func SetSecret

func SetSecret(secret []byte)

SetSecret sets the secret used to sign/verify tokens. Call this during application startup if you want a stable secret.

func Slugify

func Slugify(value string) string

将字符串转换为 URL 友好的 slug 例如:Some Product -> some-product

func Split

func Split(s, sep string) []string

Split splits a string by a separator

func Sub

func Sub(a, b int) int

Sub returns a - b

func TotalWords

func TotalWords(s string) int

TotalWords counts total words of article.

func UnescapeHTML

func UnescapeHTML(s string) template.HTML

UnescapeHTML

func Until

func Until(n int) []int

Until returns a slice of ints from 0 to n-1

Types

type AntdCurrentUserResult

type AntdCurrentUserResult struct {
	Name   string `json:"name"`
	Access string `json:"access" enums:"admin,agent"`
	Avatar string `json:"avatar"`
}

AntdCurrentUserResult .

type AntdLoginResult

type AntdLoginResult struct {
	Status string `json:"status" enums:"ok,not ok"`
	Token  string `json:"token"`
}

AntdLoginResult .

type AntdResult

type AntdResult struct {
	Success bool        `json:"success"`
	Data    interface{} `json:"data"`
	Total   int64       `json:"total" example:"100"`
}

AntdResult Antd Pro table list api

type CommonResponse

type CommonResponse struct {
	ErrCode int    `json:"err_code" example:"0"`
	ErrMsg  string `json:"err_msg" example:"OK"`
	Data    any    `json:"data"`
}

type NullTime

type NullTime struct {
	sql.NullTime
}

func (NullTime) MarshalJSON

func (v NullTime) MarshalJSON() ([]byte, error)

func (*NullTime) UnmarshalJSON

func (v *NullTime) UnmarshalJSON(data []byte) error

type SMTPConfig

type SMTPConfig struct {
	Host     string
	Port     int
	User     string
	Pass     string
	UserName string // 发件人名称
}

type TokenPayload

type TokenPayload struct {
	Resource string `json:"res"`
	Iat      int64  `json:"iat"`
	Exp      int64  `json:"exp"`
	Nonce    string `json:"nonce"`
}

TokenPayload is the JSON payload embedded in the token.

func ParseToken

func ParseToken(token string) (TokenPayload, error)

ParseToken verifies the token signature and returns the decoded payload. It does NOT check expiration; use ValidateToken to also enforce expiry.

func ValidateToken

func ValidateToken(token string) (TokenPayload, error)

ValidateToken parses the token, verifies signature and expiration. Returns the payload if valid.

Jump to

Keyboard shortcuts

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