remote

package
v0.0.0-...-ae5ab06 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// model管理,比如:/model/api
	APIGetAllModels = "/get-all-models"
	APIAddModel     = "/add"
	APIDeleteModel  = "/delete"

	// 针对指定model的访问,比如:/admin/model-name/api
	APIGetModelInfo         = "/get-model-info"
	APIAddArticle           = "/add-article"
	APIDeleteArticle        = "/delete-article"
	APIGetArticle           = "/get-article"
	APIGetNextArticles      = "/get-next-articles"
	APIGetPrevArticles      = "/get-prev-articles"
	APIGetNextArticlesByTag = "/get-next-articles-by-tag"
	APIGetPrevArticlesByTag = "/get-prev-articles-by-tag"
	APIUpdateArticle        = "/update-article"
	APIGetTagById           = "/get-tag-by-id"
	APIGetTagByName         = "/get-tag-by-name"
	APIGetNextTags          = "/get-next-tags"
	APIGetPrevTags          = "/get-prev-tags"
	APIRenameTag            = "/rename-tag"
	APIGetArticleCountByTag = "/get-article-count-by-tag"
)
View Source
const (
	ErrCodeSuccess        = 0
	ErrCodeFailed         = -1
	ErrCodeArticleDeleted = -2
	ErrMsgSuccess         = "success"
	ErrModeExist          = "model exist"
	ErrModelNotExist      = "model not exist"
	ErrModelNameIsNull    = "model name is null"
	ErrMsgArticleDeleted  = "article is deleted"
)
View Source
const (
	// 数据库名称
	ArticleDBPath = "article.db"
	TagDBPath     = "tag.db"
	IndexDBPath   = "index.db"
	TTLDBPath     = "ttl.db"

	// model中的文章ID都是uint64类型,需要增加字符串ID的扩展功能
	// 保存字符串ID和整型ID的映射,也可以用于自定义ID(字符串)
	// 如果不使用自定义ID,则随机产生一个字符串ID
	// 这样做的原因避免ID为整数,然后导致网站被遍历抓取
	// 用自定义ID还有一个好处,比如将文章的标题作为文章的自定义ID,自带去重效果
	IdDBPath = "id.db"

	// 额外扩展一个自定义ID,业务层设置,不自动生成
	CustomIdDBPath = "custom_id.db"
)

Variables

View Source
var (
	// 注意:错误分为两类,获取文章时需要区分
	// 1. 文章不存在,这里有两种情况:
	//    1.1 如果文章是被删除的(包括过期自动删除),返回 ErrArticleDeleted
	//    1.2 如果文章从未存在过,返回 ErrNotFound
	// 2. 服务器连接失败或者错误,返回500
	ErrServerFailed   = errors.New("500 server error")
	ErrNotFound       = errors.New("404 not found")
	ErrArticleDeleted = errors.New("article is deleted")
)

Functions

This section is empty.

Types

type APIClient

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

func (*APIClient) AddArticle

func (c *APIClient) AddArticle(tags []string, data string, ttl int64, customArticleId, customArticleIdEx string) (uint64, string, string, error)

customArticleId 可以为空,如果为空,则服务器自动生成 customArticleIdEx 可以为空,服务器不会自动生成 返回:文章ID、自定义文章ID、扩展自定义文章ID

func (*APIClient) AddModel

func (c *APIClient) AddModel(modelName, modelDescription string) error

func (*APIClient) DeleteArticle

func (c *APIClient) DeleteArticle(articleId uint64, customArticleId, customArticleIdEx string) error

文章ID随便填一个,customArticleId 优先

func (*APIClient) DeleteModel

func (c *APIClient) DeleteModel(modelName string) error

func (*APIClient) GetAllModels

func (c *APIClient) GetAllModels() (map[string]string, error)

func (*APIClient) GetArticle

func (c *APIClient) GetArticle(articleId uint64, customArticleId, customArticleIdEx string) (*RemoteArticle, error)

func (*APIClient) GetArticleCount

func (c *APIClient) GetArticleCount() uint64

func (*APIClient) GetArticleCountByTag

func (c *APIClient) GetArticleCountByTag(tagName string) uint64

func (*APIClient) GetMaxArticleId

func (c *APIClient) GetMaxArticleId() uint64

func (*APIClient) GetModelInfo

func (c *APIClient) GetModelInfo() (uint64, uint64, uint64, error)

返回文章数量、分类数量、最大的文章ID

func (*APIClient) GetNextArticles

func (c *APIClient) GetNextArticles(articleId uint64, customArticleId, customArticleIdEx string, n int) []*RemoteArticle

func (*APIClient) GetNextArticlesByTag

func (c *APIClient) GetNextArticlesByTag(tagName string, articleId uint64, customArticleId, customArticleIdEx string, n int) []*RemoteArticle

func (*APIClient) GetNextTags

func (c *APIClient) GetNextTags(tagName string, n int) []*RemoteTag

func (*APIClient) GetPrevArticles

func (c *APIClient) GetPrevArticles(articleId uint64, customArticleId, customArticleIdEx string, n int) []*RemoteArticle

func (*APIClient) GetPrevArticlesByTag

func (c *APIClient) GetPrevArticlesByTag(tagName string, articleId uint64, customArticleId, customArticleIdEx string, n int) []*RemoteArticle

func (*APIClient) GetPrevTags

func (c *APIClient) GetPrevTags(tagName string, n int) []*RemoteTag

func (*APIClient) GetTagById

func (c *APIClient) GetTagById(id uint64) (*RemoteTag, error)

func (*APIClient) GetTagByName

func (c *APIClient) GetTagByName(name string) (*RemoteTag, error)

func (*APIClient) GetTagCount

func (c *APIClient) GetTagCount() uint64

func (*APIClient) RenameTag

func (c *APIClient) RenameTag(oldName, newName string) error

func (*APIClient) Start

func (c *APIClient) Start(remoteAddr, modelName string)

func (*APIClient) UpdateArticle

func (c *APIClient) UpdateArticle(articleId uint64, customArticleId, customArticleIdEx string, newTags []string, newData string, newTTL int64) error

type APIServer

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

func (*APIServer) Start

func (s *APIServer) Start(config *APIServerConfig)

type APIServerConfig

type APIServerConfig struct {
	// 配置文件的路径
	ConfPath string

	// 数据存储目录
	DataDir string

	// 监听地址
	ListeningAddr string

	// 是否开启Gzip
	// 建议:如果服务器之间走内网(内网带宽非常大),则无需开启
	UseGzip bool
}

type AddArticleReq

type AddArticleReq struct {
	Tags []string `json:"tags"`
	Data string   `json:"data"`
	TTL  int64    `json:"ttl"`

	// CustomArticleId 如果没有填,系统会自动生成一个随机的唯一字符串
	CustomArticleId string `json:"custom_article_id"`

	// CustomArticleIdEx 如果没有填,系统会忽略掉这个
	CustomArticleIdEx string `json:"custom_article_id_ex"`
}

type AddArticleResp

type AddArticleResp struct {
	BaseResp
	ArticleId         uint64 `json:"article_id"`
	CustomArticleId   string `json:"custom_article_id"`
	CustomArticleIdEx string `json:"custom_article_id_ex"`
}

type AddModelResp

type AddModelResp = BaseResp

type BaseResp

type BaseResp struct {
	ErrCode int    `json:"errcode"`
	ErrMsg  string `json:"errmsg"`
}

type DeleteArticleReq

type DeleteArticleReq struct {
	ArticleId         uint64 `json:"article_id"`
	CustomArticleId   string `json:"custom_article_id"`
	CustomArticleIdEx string `json:"custom_article_id_ex"`
}

type DeleteArticleResp

type DeleteArticleResp = BaseResp

type DeleteModelResp

type DeleteModelResp = BaseResp

type GetAllModelsResp

type GetAllModelsResp struct {
	BaseResp
	Models map[string]string `json:"models"` // modelName -> modelDescription
}

type GetArticleCountByTagReq

type GetArticleCountByTagReq struct {
	TagName string `json:"tag_name"`
}

type GetArticleCountByTagResp

type GetArticleCountByTagResp struct {
	BaseResp
	ArticleCount uint64 `json:"article_count"`
}

type GetArticleReq

type GetArticleReq = DeleteArticleReq

type GetArticleResp

type GetArticleResp struct {
	BaseResp
	*RemoteArticle
}

type GetModelInfoResp

type GetModelInfoResp struct {
	BaseResp
	ArticleCount uint64 `json:"article_count"`
	TagCount     uint64 `json:"tag_count"`
	MaxArticleId uint64 `json:"max_article_id"`
}

type GetNextArticlesByTagReq

type GetNextArticlesByTagReq struct {
	GetNextArticlesReq
	Tag string `json:"tag"`
}

type GetNextArticlesByTagResp

type GetNextArticlesByTagResp = GetNextArticlesResp

type GetNextArticlesReq

type GetNextArticlesReq struct {
	ArticleId         uint64 `json:"article_id"`
	CustomArticleId   string `json:"custom_article_id"`
	CustomArticleIdEx string `json:"custom_article_id_ex"`
	N                 int    `json:"n"`
}

type GetNextArticlesResp

type GetNextArticlesResp struct {
	BaseResp
	RemoteArticles []*RemoteArticle `json:"remote_articles"`
}

type GetNextTagsReq

type GetNextTagsReq struct {
	TagName string `json:"tag_name"`
	N       int    `json:"n"`
}

type GetNextTagsResp

type GetNextTagsResp struct {
	BaseResp
	RemoteTags []*RemoteTag `json:"remote_tags"`
}

type GetPrevArticlesByTagReq

type GetPrevArticlesByTagReq = GetNextArticlesByTagReq

type GetPrevArticlesByTagResp

type GetPrevArticlesByTagResp = GetNextArticlesByTagResp

type GetPrevArticlesReq

type GetPrevArticlesReq = GetNextArticlesReq

type GetPrevArticlesResp

type GetPrevArticlesResp = GetNextArticlesResp

type GetPrevTagsReq

type GetPrevTagsReq = GetNextTagsReq

type GetPrevTagsResp

type GetPrevTagsResp = GetNextTagsResp

type GetTagByIdReq

type GetTagByIdReq struct {
	TagId uint64 `json:"tag_id"`
}

type GetTagByIdResp

type GetTagByIdResp struct {
	BaseResp
	*RemoteTag
}

type GetTagByNameReq

type GetTagByNameReq struct {
	TagName string `json:"tag_name"`
}

type GetTagByNameResp

type GetTagByNameResp = GetTagByIdResp

type ModelConfig

type ModelConfig struct {
	Models map[string]string `json:"models"` // model_name -> model_description
}

type ModelMgr

type ModelMgr struct {
	// 数据存储目录
	DataDir string

	// 配置文件路径
	ConfPath string

	// 配置文件解析后的数据
	Conf *ModelConfig
}

func NewModelMgr

func NewModelMgr(confPath string, dataDir string) *ModelMgr

confPath: 配置文件路径,比如:./model.conf dataDir: 数据存储目录,末尾带有 /

func (*ModelMgr) AddModel

func (m *ModelMgr) AddModel(name, description string) error

func (*ModelMgr) CheckExist

func (m *ModelMgr) CheckExist(name string) bool

func (*ModelMgr) GetAllModels

func (m *ModelMgr) GetAllModels() map[string]string

func (*ModelMgr) GetModelDir

func (m *ModelMgr) GetModelDir(modelName string) string

func (*ModelMgr) RemoveModel

func (m *ModelMgr) RemoveModel(name string) error

func (*ModelMgr) WriteToFile

func (m *ModelMgr) WriteToFile() error

type RemoteArticle

type RemoteArticle struct {
	*gmodel.Article
	CustomArticleId   string   `json:"custom_article_id"`    // 自定义文章ID,优先级高于 Article.Id
	CustomArticleIdEx string   `json:"custom_article_id_ex"` // 自定义文章ID,优先级高于 Article.Id,但低于 CustomArticleId
	TagNameArray      []string `json:"tag_name_array"`       // 分类数组
}

CustomArticleId 优先,CustomArticleId为空时才使用 Article.Id,下同 注意:CustomArticleId 和 CustomArticleIdEx(如果存在)永不删除,且永远映射到 Article.Id, 即使 Article 被删除,这样可以避免生成重复的字符串ID,以及避免旧的字符串ID(对应文章已经删除)可以访问新的文章

type RemoteTag

type RemoteTag struct {
	*gmodel.Tag
}

type RenameTagReq

type RenameTagReq struct {
	OldName string `json:"old_name"`
	NewName string `json:"new_name"`
}

type RenameTagResp

type RenameTagResp = BaseResp

type UpdateArticleReq

type UpdateArticleReq struct {
	ArticleId         uint64   `json:"article_id"`
	CustomArticleId   string   `json:"custom_article_id"`
	CustomArticleIdEx string   `json:"custom_article_id_ex"`
	NewTags           []string `json:"new_tags"`
	NewData           string   `json:"new_data"`
	NewTTL            int64    `json:"new_ttl"`
}

type UpdateArticleResp

type UpdateArticleResp = BaseResp

Jump to

Keyboard shortcuts

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