作者 唐旭辉
package common
import "fmt"
import (
"bytes"
"fmt"
"encoding/json"
"math/rand"
)
// Must panics if err is not nil.
func Must(err error) {
... ... @@ -22,4 +27,32 @@ func Error2(v interface{}, err error) error {
func LogF(format string,args interface{})string{
return fmt.Sprintf(format,args)
}
func AssertJson(object interface{})string{
json,err :=json.Marshal(object)
if err!=nil{
return ""
}
return string(json)
}
var randomChars = "ABCDEFGHJKMNPQRSTWXYZabcdefhjkmnprstwxyz2345678" /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
func RandomString(l int)string{
return RandomStringWithChars(l,randomChars)
}
func RandomStringWithChars(l int,chars string)string{
if l<=0{
return ""
}
if len(chars)==0{
return ""
}
lenChars :=len(chars) -1
rsp :=bytes.NewBuffer(nil)
for i:=0;i<l;i++{
rsp.WriteByte(chars[rand.Intn(lenChars)])
}
return rsp.String()
}
\ No newline at end of file
... ...
package common
import (
"testing"
)
func Test_RandomString(t *testing.T){
input :=[]int{6,10,20,16,32}
for i:=range input{
l :=input[i]
out := RandomString(l)
if len(out)!=l{
t.Fatal("length not equal want :",l," out:",out)
}
}
}
func Benchmark_RandomString(b *testing.B) {
input :=[]int{10,20,16,32}
l :=0
out :=""
for i:=0;i<b.N;i++{
l=i%4
l=input[l]
out = RandomString(l)
if len(out)!=l{
b.Fatal("length not equal want :",l," out:",out)
}
}
}
\ No newline at end of file
... ...
... ... @@ -7,13 +7,6 @@ type Error struct {
err error
}
func (e Error)Error()string{
if e.err==nil{
return ""
}
return e.err.Error()
}
func NewError(code int,e error)Error{
return Error{
Code:code,
... ... @@ -24,3 +17,14 @@ func NewError(code int,e error)Error{
func NewErrorWithMsg(code int,msg string)Error{
return NewError(code,fmt.Errorf(msg))
}
func (e Error)Error()string{
if e.err==nil{
return ""
}
return e.err.Error()
}
func(e Error)Unwrap()error{
return e.err
}
... ...
package common
import (
"errors"
"fmt"
"testing"
)
... ... @@ -12,4 +13,19 @@ func Test_Error(t *testing.T){
emsg :=NewErrorWithMsg(2,"some error")
t.Log(emsg,emsg.Code)
}
func Test_AssertError(t *testing.T){
var targetErr = NewError(1,fmt.Errorf("%v","some error"))
var e error =targetErr
if !errors.Is(e,targetErr){
t.Fatal("errors.Is not equal")
}
if errors.Unwrap(e) ==nil{
t.Fatal("errors.Unwrap not nil")
}
var commErr Error
if !errors.As(e,&commErr){
t.Fatal("errors.As error")
}
}
\ No newline at end of file
... ...
package log
import (
"log"
)
type ConsoleLog struct {
}
func newConsoleLog()Log{
return &ConsoleLog{}
}
func(this *ConsoleLog)Debug(args ...interface{}){
//this.log.Debug(args...)
log.Println(args...)
}
func(this *ConsoleLog)Info(args ...interface{}){
log.Println(args...)
}
func(this *ConsoleLog)Warn(args ...interface{}){
log.Println(args...)
}
func(this *ConsoleLog)Error(args ...interface{}){
log.Println(args...)
}
func(this *ConsoleLog)Panic(args ...interface{}){
log.Println(args...)
}
func(this *ConsoleLog)Fatal(args ...interface{}){
log.Println(args...)
}
\ No newline at end of file
... ...
... ... @@ -12,7 +12,7 @@ type Log interface{
}
var(
DefaultLog Log
DefaultLog Log =newConsoleLog()
)
func InitLog(conf config.Logger){
... ... @@ -36,3 +36,4 @@ func Panic(args ...interface{}){
func Fatal(args ...interface{}){
DefaultLog.Fatal(args...)
}
... ...
... ... @@ -33,17 +33,16 @@ func (this *BaseController) Options() {
func (this *BaseController) AllowCross() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
//this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")
this.Ctx.WriteString("")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
//this.Ctx.WriteString("")
}
func (this *BaseController) Prepare() {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Origin", "*")
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "*")
if this.Ctx.Input.Method() == "OPTIONS" {
this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
//this.Ctx.ResponseWriter.Header().Set("Access-Control-Allow-Headers", "uid, token,jwt, deviceid, appid,Content-Type,Authorization,from")
this.Ctx.WriteString("")
//this.Ctx.WriteString("")
return
}
... ... @@ -118,34 +117,3 @@ func (this *BaseController) Finish() {
log.Info(fmt.Sprintf("<====Send to uid(%d) client: %d byte\nRequestId:%s RspBodyData: %s", this.RequestHead.Uid, length,this.RequestHead.GetRequestId(), string(strByte)))
}
}
// BaseControllerCallBack
type BaseControllerCallBack struct {
beego.Controller
Query map[string]string
JSONBody map[string]interface{}
ByteBody []byte
}
func (this *BaseControllerCallBack) Prepare() {
this.Query = map[string]string{}
input := this.Input()
for k := range input {
this.Query[k] = input.Get(k)
}
if this.Ctx.Input.RequestBody != nil {
log.Info("RecvHead:", string(this.Ctx.Input.Header("Authorization")))
this.ByteBody = this.Ctx.Input.RequestBody
}
}
func (this *BaseControllerCallBack) Resp(msg *Message) {
this.Data["json"] = msg
this.ServeJSON()
}
func (this *BaseControllerCallBack) Finish() {
strByte, _ := json.Marshal(this.Data["json"])
log.Debug("<====Send to client:\n", string(strByte))
}
... ...
... ... @@ -3,7 +3,6 @@ package mybeego
import (
"fmt"
"github.com/astaxie/beego"
"time"
)
//type IMessage interface {
... ... @@ -32,7 +31,7 @@ func NewMessage(code int) *Message {
return &Message{
Errno: code,
Errmsg: ErrnoMsg[code],
SysTime: time.Now().Unix(),
//SysTime: time.Now().Unix(),
}
}
... ... @@ -64,4 +63,8 @@ func init() {
ErrnoMsg[4] = "您目前使用的版本过低,无法显示最新的相关内容,请使用最新版本。"
ErrnoMsg[5] = "描述包含敏感词,请重新编辑"
ErrnoMsg[6] ="重复提交,请稍后再试"
}
func SetMessage(code int,msg string){
ErrnoMsg[code] = msg
}
\ No newline at end of file
... ...