rest

package
v0.1.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2021 License: MIT Imports: 28 Imported by: 1

README

REST package

Rest package handles all the rest request, eg. query content, add/edit/delete content. Use subroute to run it.

List of apis:

Note: the real url can depend on routering. The default one is /api. eg. /api/content/get/2

Fetch content

content/get/{id}

content/children/{id}

content/list/{id}

Operate content

i18n library needed

Besides standand transation, the i18n library can be used for customized message also. use key or message? The library should be

  • easy to use. eg. one function should be enough
  • easy to edit and can be changed online. eg. json/toml
  • better support count
  • support override in project so many standard message(eg. validation, error) can be overridden based on special need(not only translation, but also english). Customzied template message(eg. error template) should be done in template override, not here.
  • message should be written inline to improve readability.
  • message supports non-english in code so you can actually write Chinese in source and translate to english. Example: i18n.T( "File format is not supported" ) //message with global context i18n.T( "File format is not supported", "rest.error" ) // message+context note: the message can be treated as a key also. so you can actually customize message in ch.json: { "context": "rest.error", "text": "File format is not supported", "translation": "不支持的文件类型." } also in eng.json in project{ { "context": "rest.error", "text": "File format is not supported", "translation": "File is not supported, check (http://xxxf)[this] link?." } }

so translation file can be a kind of template override. text/locale/eng.json text/locale/chi.json text/customize/eng.json text/customize/chi.json

Documentation

Overview

Author xc, Created on 2020-10-1 12:50 {COPYRIGHTS}

Author xc, Created on 2019-08-11 16:49 {COPYRIGHTS}

Author xc, Created on 2019-08-13 17:25 {COPYRIGHTS}

Index

Constants

View Source
const StatusExpired = 440
View Source
const StatusNotFound = 404
View Source
const StatusServer = 500
View Source
const StatusUnauthed = 403
View Source
const StatusWrongParams = 400

Variables

View Source
var (
	TokenErrorExpired = errors.New("Expired token")
	TokenErrorRevoked = errors.New("Expired revoked")
)

Functions

func AssignUser

func AssignUser(w http.ResponseWriter, r *http.Request)

func AssignedUsers

func AssignedUsers(w http.ResponseWriter, r *http.Request)

func AuthAuthenticate

func AuthAuthenticate(w http.ResponseWriter, r *http.Request)

AuthAuthenticate generate refresh toke and access token based on username and password

func AuthRenewAccessToken

func AuthRenewAccessToken(w http.ResponseWriter, r *http.Request)

Renew access token

func AuthRenewRefreshToken

func AuthRenewRefreshToken(w http.ResponseWriter, r *http.Request)

Renew refresh token

func AuthRevokeRefreshToken

func AuthRevokeRefreshToken(w http.ResponseWriter, r *http.Request)

func AuthVerifyAccessToken

func AuthVerifyAccessToken(w http.ResponseWriter, r *http.Request)

func BuildLimit

func BuildLimit(r *http.Request) ([]int, error)

func BuildSortby

func BuildSortby(r *http.Request) []string

func CheckUserID

func CheckUserID(context context.Context, w http.ResponseWriter) int

Check if there is user id, if not output error and return 0

func CurrentUser

func CurrentUser(w http.ResponseWriter, r *http.Request)

Get current user

func CurrentUserEditField

func CurrentUserEditField(w http.ResponseWriter, r *http.Request)

Get current user's updatefields on him/herself

func Delete

func Delete(w http.ResponseWriter, r *http.Request)

func EnableUser

func EnableUser(w http.ResponseWriter, r *http.Request)

func ExportPDF

func ExportPDF(w http.ResponseWriter, r *http.Request)

func GetAllDefinitions

func GetAllDefinitions(w http.ResponseWriter, r *http.Request)

func GetAllowedLimitations

func GetAllowedLimitations(w http.ResponseWriter, r *http.Request)

func GetContent

func GetContent(w http.ResponseWriter, r *http.Request)

func GetDefinition

func GetDefinition(w http.ResponseWriter, r *http.Request)

todo: check permission

func GetVersion

func GetVersion(w http.ResponseWriter, r *http.Request)

func HandleError

func HandleError(err error, w http.ResponseWriter, httpCode ...int)

func HandleRoute

func HandleRoute(router *mux.Router)

Handle route with context. eg. user Loop registered route and listen handle function

func HandleUploadFile

func HandleUploadFile(r *http.Request, filetype string) (string, error)

Handler uploaded file, return filename & error

func HtmlToPDF

func HtmlToPDF(html string, name string) (string, error)

func InitRequest

func InitRequest(next http.Handler) http.Handler

Initialize request, including set context. todo: support callback

func List

func List(w http.ResponseWriter, r *http.Request)

List

func Move

func Move(w http.ResponseWriter, r *http.Request)

func New

func New(w http.ResponseWriter, r *http.Request)

func RegisterRoute

func RegisterRoute(path string, funcHandler func(http.ResponseWriter, *http.Request))

func RelationOptionList

func RelationOptionList(w http.ResponseWriter, r *http.Request)

Get content list from relation definition

func ResetPassword

func ResetPassword(w http.ResponseWriter, r *http.Request)

todo: move this into user logic under core/user or handler/user.go folder

func ResetPasswordDone

func ResetPasswordDone(w http.ResponseWriter, r *http.Request)

todo: move this to logic

func SaveDraft

func SaveDraft(w http.ResponseWriter, r *http.Request)

func SetPriority

func SetPriority(w http.ResponseWriter, r *http.Request)

func TreeMenu

func TreeMenu(w http.ResponseWriter, r *http.Request)

Get tree menu under a node

func UnassignUser

func UnassignUser(w http.ResponseWriter, r *http.Request)

unassign user from role

func Update

func Update(w http.ResponseWriter, r *http.Request)

func UploadFile

func UploadFile(w http.ResponseWriter, r *http.Request)

func UploadImage

func UploadImage(w http.ResponseWriter, r *http.Request)

Upload image, return path or error

func UserRoles

func UserRoles(w http.ResponseWriter, r *http.Request)

func Validate

func Validate(w http.ResponseWriter, r *http.Request)

func VerifyAccessToken

func VerifyAccessToken(r *http.Request) (error, auth.UserClaims)

Types

type Activiation

type Activiation struct {
	ID      int    `boil:"id" json:"id" toml:"id" yaml:"id"`
	Created int    `boil:"created" json:"created" toml:"created" yaml:"created"`
	Hash    string `boil:"hash" json:"hash" toml:"hash" yaml:"hash"`

	//type. eg. resetpassword
	Type string `boil:"type" json:"type" toml:"type" yaml:"type"`
	//reference. eg. userid
	Ref string `boil:"ref" json:"ref" toml:"ref" yaml:"ref"`
}

todo: move this into entity folder

type ResultItem

type ResultItem map[string]interface{}

Result item in the list

type ResultList

type ResultList []ResultItem

Result list

Jump to

Keyboard shortcuts

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