package rest import ( "context" "encoding/json" "gorm.io/gorm" "net/http" ) const ( defaultPageSize = 15 defaultDomain = "localhost" ) type ( httpWriter struct { } multiValue struct { Text any `json:"label"` Value any `json:"value"` } stdResponse struct { Code int `json:"code"` Reason string `json:"reason,omitempty"` Data any `json:"data,omitempty"` } tableNamer interface { HttpTableName(req *http.Request) string } //创建后的回调,这个回调不在事物内 afterCreated interface { AfterCreated(ctx context.Context, tx *gorm.DB) } //更新后的回调,这个回调不在事物内 afterUpdated interface { AfterUpdated(ctx context.Context, tx *gorm.DB) } //保存后的回调,这个回调不在事物内 afterSaved interface { AfterSaved(ctx context.Context, tx *gorm.DB) } sqlCountResponse struct { Count int64 `json:"count"` } ) func (h *httpWriter) Success(w http.ResponseWriter, data any) { w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(stdResponse{ Code: 0, Data: data, }); err != nil { w.Write([]byte(err.Error())) } } func (h *httpWriter) Failure(w http.ResponseWriter, reason int, message string, data any) { w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(stdResponse{ Code: reason, Reason: message, Data: data, }); err != nil { w.Write([]byte(err.Error())) } } func defaultValueLookup(field string, w http.ResponseWriter, r *http.Request) string { var ( domainName string ) domainName = r.Header.Get(field) if domainName == "" { return defaultDomain } return domainName }