diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..fa9fd60 --- /dev/null +++ b/main_test.go @@ -0,0 +1,35 @@ +package main + +import ( + "github.com/kataras/iris/v12" + "github.com/kataras/iris/v12/httptest" + "maintainman/config" + "maintainman/logger" + "maintainman/model" + "maintainman/route" + "testing" +) + +func TestNewApp(t *testing.T) { + app := newApp() + e := httptest.New(t, app) + + e.POST("/v1/register").WithJSON(model.ModifyUserJson{ + Name: "test1", + Password: "12345678", + DisplayName: "admin", + }).Expect().Status(httptest.StatusOK) + + e.POST("/v1/login").WithJSON(model.LoginJson{ + Account: "张城玮", + Password: "12345678", + }).Expect().Status(httptest.StatusOK) +} + +func newApp() *iris.Application { + app := iris.New() + app.Logger().SetLevel(config.AppConfig.GetString("app.loglevel")) + logger.Logger = app.Logger() + route.Route(app) + return app +} diff --git a/util/interfaceToStirng.go b/util/interfaceToStirng.go new file mode 100644 index 0000000..c2a305e --- /dev/null +++ b/util/interfaceToStirng.go @@ -0,0 +1,59 @@ +package util + +import ( + "encoding/json" + "strconv" +) + +func Strval(value interface{}) string { + var key string + if value == nil { + return key + } + switch value.(type) { + case float64: + ft := value.(float64) + key = strconv.FormatFloat(ft, 'f', -1, 64) + case float32: + ft := value.(float32) + key = strconv.FormatFloat(float64(ft), 'f', -1, 64) + case int: + it := value.(int) + key = strconv.Itoa(it) + case uint: + it := value.(uint) + key = strconv.Itoa(int(it)) + case int8: + it := value.(int8) + key = strconv.Itoa(int(it)) + case uint8: + it := value.(uint8) + key = strconv.Itoa(int(it)) + case int16: + it := value.(int16) + key = strconv.Itoa(int(it)) + case uint16: + it := value.(uint16) + key = strconv.Itoa(int(it)) + case int32: + it := value.(int32) + key = strconv.Itoa(int(it)) + case uint32: + it := value.(uint32) + key = strconv.Itoa(int(it)) + case int64: + it := value.(int64) + key = strconv.FormatInt(it, 10) + case uint64: + it := value.(uint64) + key = strconv.FormatUint(it, 10) + case string: + key = value.(string) + case []byte: + key = string(value.([]byte)) + default: + newValue, _ := json.Marshal(value) + key = string(newValue) + } + return key +}