Kaynağa Gözat

optimize example dir

zcool321@sina.com 6 yıl önce
ebeveyn
işleme
c78515fdb9
4 değiştirilmiş dosya ile 116 ekleme ve 129 silme
  1. 0 43
      example/boot/boot.go
  2. 0 77
      example/boot/router.go
  3. 108 1
      example/main.go
  4. 8 8
      gtoken/gtoken_test.go

+ 0 - 43
example/boot/boot.go

@@ -1,43 +0,0 @@
-package boot
-
-import (
-	"github.com/gogf/gf/frame/g"
-	"github.com/gogf/gf/net/ghttp"
-	"github.com/gogf/gf/os/glog"
-)
-
-// 管理初始化顺序.
-func init() {
-	initConfig()
-	initRouter()
-}
-
-// 用于配置初始化.
-func initConfig() {
-	glog.Info("########service start...")
-
-	v := g.View()
-	c := g.Config()
-	s := g.Server()
-
-	path := ""
-	// 配置对象及视图对象配置
-	c.AddPath(path + "config")
-
-	v.SetDelimiters("${", "}")
-	v.AddPath(path + "template")
-
-	// glog配置
-	logPath := c.GetString("log-path")
-	glog.SetPath(logPath)
-	glog.SetStdoutPrint(true)
-
-	s.SetServerRoot("./public")
-	s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
-	s.SetLogPath(logPath)
-	s.SetErrorLogEnabled(true)
-	s.SetAccessLogEnabled(true)
-	s.SetPort(c.GetInt("http-port"))
-
-	glog.Info("########service finish.")
-}

+ 0 - 77
example/boot/router.go

@@ -1,77 +0,0 @@
-package boot
-
-import (
-	"github.com/goflyfox/gtoken/gtoken"
-	"github.com/gogf/gf/frame/g"
-	"github.com/gogf/gf/net/ghttp"
-	"github.com/gogf/gf/os/glog"
-)
-
-/*
-绑定业务路由
-*/
-func bindRouter() {
-
-	s := g.Server()
-	// 调试路由
-	s.BindHandler("/hello", func(r *ghttp.Request) {
-		r.Response.WriteJson(gtoken.Succ("hello"))
-	})
-	s.BindHandler("/system/user", func(r *ghttp.Request) {
-		r.Response.WriteJson(gtoken.Succ("system user"))
-	})
-
-	loginFunc := Login
-	// 启动gtoken
-	gtoken := &gtoken.GfToken{
-		//Timeout:         10 * 1000,
-		CacheMode:       g.Config().GetInt8("cache-mode"),
-		LoginPath:       "/login",
-		LoginBeforeFunc: loginFunc,
-		LogoutPath:      "/user/logout",
-		AuthPaths:       g.SliceStr{"/user/*", "/system/*"},
-	}
-	gtoken.Start()
-
-}
-
-/*
-统一路由注册
-*/
-func initRouter() {
-
-	s := g.Server()
-
-	// 绑定路由
-	bindRouter()
-
-	// 首页
-	s.BindHandler("/", func(r *ghttp.Request) {
-		content, err := g.View().Parse("index.html", map[string]interface{}{
-			"id":    1,
-			"name":  "GTOKEN",
-			"title": g.Config().GetString("setting.title"),
-		})
-		if err != nil {
-			glog.Error(err)
-		}
-		r.Response.Write(content)
-
-	})
-
-	// 某些浏览器直接请求favicon.ico文件,特别是产生404时
-	s.SetRewrite("/favicon.ico", "/resource/image/favicon.ico")
-
-}
-
-func Login(r *ghttp.Request) (string, interface{}) {
-	username := r.GetPostString("username")
-	passwd := r.GetPostString("passwd")
-
-	if username == "" || passwd == "" {
-		r.Response.WriteJson(gtoken.Fail("账号或密码错误."))
-		r.ExitAll()
-	}
-
-	return username, ""
-}

+ 108 - 1
example/main.go

@@ -1,10 +1,117 @@
 package main
 
 import (
-	_ "github.com/goflyfox/gtoken/example/boot"
+	"github.com/goflyfox/gtoken/gtoken"
 	"github.com/gogf/gf/frame/g"
+	"github.com/gogf/gf/net/ghttp"
+	"github.com/gogf/gf/os/glog"
 )
 
 func main() {
 	g.Server().Run()
 }
+
+// 管理初始化顺序.
+func init() {
+	initConfig()
+	initRouter()
+}
+
+// 用于配置初始化.
+func initConfig() {
+	glog.Info("########service start...")
+
+	v := g.View()
+	c := g.Config()
+	s := g.Server()
+
+	path := ""
+	// 配置对象及视图对象配置
+	c.AddPath(path + "config")
+
+	v.SetDelimiters("${", "}")
+	v.AddPath(path + "template")
+
+	// glog配置
+	logPath := c.GetString("log-path")
+	glog.SetPath(logPath)
+	glog.SetStdoutPrint(true)
+
+	s.SetServerRoot("./public")
+	s.SetNameToUriType(ghttp.NAME_TO_URI_TYPE_ALLLOWER)
+	s.SetLogPath(logPath)
+	s.SetErrorLogEnabled(true)
+	s.SetAccessLogEnabled(true)
+	s.SetPort(c.GetInt("http-port"))
+
+	glog.Info("########service finish.")
+}
+
+/*
+绑定业务路由
+*/
+func bindRouter() {
+
+	s := g.Server()
+	// 调试路由
+	s.BindHandler("/hello", func(r *ghttp.Request) {
+		r.Response.WriteJson(gtoken.Succ("hello"))
+	})
+	s.BindHandler("/system/user", func(r *ghttp.Request) {
+		r.Response.WriteJson(gtoken.Succ("system user"))
+	})
+
+	loginFunc := Login
+	// 启动gtoken
+	gtoken := &gtoken.GfToken{
+		//Timeout:         10 * 1000,
+		CacheMode:       g.Config().GetInt8("cache-mode"),
+		LoginPath:       "/login",
+		LoginBeforeFunc: loginFunc,
+		LogoutPath:      "/user/logout",
+		AuthPaths:       g.SliceStr{"/user/*", "/system/*"},
+	}
+	gtoken.Start()
+
+}
+
+/*
+统一路由注册
+*/
+func initRouter() {
+
+	s := g.Server()
+
+	// 绑定路由
+	bindRouter()
+
+	// 首页
+	s.BindHandler("/", func(r *ghttp.Request) {
+		content, err := g.View().Parse("index.html", map[string]interface{}{
+			"id":    1,
+			"name":  "GTOKEN",
+			"title": g.Config().GetString("setting.title"),
+		})
+		if err != nil {
+			glog.Error(err)
+		}
+		r.Response.Write(content)
+
+	})
+
+	// 某些浏览器直接请求favicon.ico文件,特别是产生404时
+	s.SetRewrite("/favicon.ico", "/resource/image/favicon.ico")
+
+}
+
+func Login(r *ghttp.Request) (string, interface{}) {
+	username := r.GetPostString("username")
+	passwd := r.GetPostString("passwd")
+
+	if username == "" || passwd == "" {
+		r.Response.WriteJson(gtoken.Fail("账号或密码错误."))
+		r.ExitAll()
+	}
+
+	return username, ""
+}

+ 8 - 8
gtoken/gtoken_test.go

@@ -7,17 +7,17 @@ import (
 
 func TestEncryptDecryptToken(t *testing.T) {
 	t.Log("encrypt and decrypt token test ")
-	gtoken := gtoken.GfToken{}
-	gtoken.Init()
+	gfToken := gtoken.GfToken{}
+	gfToken.Init()
 
 	userKey := "123123"
-	token := gtoken.EncryptToken(userKey)
+	token := gfToken.EncryptToken(userKey)
 	if !token.Success() {
 		t.Error(token.Json())
 	}
 	t.Log(token.DataString())
 
-	token2 := gtoken.DecryptToken(token.GetString("token"))
+	token2 := gfToken.DecryptToken(token.GetString("token"))
 	if !token2.Success() {
 		t.Error(token2.Json())
 	}
@@ -33,18 +33,18 @@ func TestEncryptDecryptToken(t *testing.T) {
 
 func BenchmarkEncryptDecryptToken(b *testing.B) {
 	b.Log("encrypt and decrypt token test ")
-	gtoken := gtoken.GfToken{}
-	gtoken.Init()
+	gfToken := gtoken.GfToken{}
+	gfToken.Init()
 
 	userKey := "123123"
-	token := gtoken.EncryptToken(userKey)
+	token := gfToken.EncryptToken(userKey)
 	if !token.Success() {
 		b.Error(token.Json())
 	}
 	b.Log(token.DataString())
 
 	for i := 0; i < b.N; i++ {
-		token2 := gtoken.DecryptToken(token.GetString("token"))
+		token2 := gfToken.DecryptToken(token.GetString("token"))
 		if !token2.Success() {
 			b.Error(token2.Json())
 		}