yxh 6 лет назад
Родитель
Сommit
4e08a78c26
5 измененных файлов с 47 добавлено и 13 удалено
  1. 0 1
      .gitignore
  2. 1 9
      boot/boot.go
  3. 15 0
      config/config.toml
  4. 1 1
      go.mod
  5. 30 2
      test/demo_test.go

+ 0 - 1
.gitignore

@@ -15,7 +15,6 @@ pkg/
 bin/
 cbuild
 */.DS_Store
-config/config.toml
 main
 .vscode
 go.sum

+ 1 - 9
boot/boot.go

@@ -1,19 +1,11 @@
 package boot
 
 import (
-	"github.com/goflyfox/gtoken/gtoken"
 	"github.com/gogf/gf/frame/g"
 )
 
 func init() {
 	g.Server().SetPort(8200)
 	g.Server().AddStaticPath("/public", g.Cfg().Get("server.ServerRoot").(string))
-	// 启动gtoken
-	gtoken := &gtoken.GfToken{
-		LoginPath:       "/login",
-		LoginBeforeFunc: loginFunc,
-		LogoutPath:      "/user/logout",
-		AuthPaths:       g.SliceStr{"/system/*"},
-	}
-	gtoken.Start()
+
 }

+ 15 - 0
config/config.toml

@@ -0,0 +1,15 @@
+# 数据库连接
+[database]
+    link = "mysql:root:123456@tcp(127.0.0.1:3306)/gfast"
+#web服务器配置
+[server]
+    Address          = ":8080"
+    ServerRoot       = "./public/resource"
+    AccessLogEnabled = true
+    ErrorLogEnabled  = true
+    PProfEnabled     = true
+    LogPath          = "./data/log/server_log"
+    SessionIdName    = "sysSessionId"
+    SessionPath      = "./data/session"
+    SessionMaxAge    = "24h"
+    DumpRouterMap    = true

+ 1 - 1
go.mod

@@ -2,7 +2,7 @@ module gfast
 
 require (
 	github.com/casbin/casbin/v2 v2.1.2
-	github.com/goflyfox/gtoken v1.3.9 // indirect
+	github.com/goflyfox/gtoken v1.3.9
 	github.com/gogf/gf v1.10.1
 	github.com/mojocn/base64Captcha v1.2.2
 )

+ 30 - 2
test/demo_test.go

@@ -5,6 +5,9 @@ import (
 	"gfast/library/utils"
 	"github.com/casbin/casbin/v2"
 	"github.com/casbin/casbin/v2/util"
+	"github.com/goflyfox/gtoken/gtoken"
+	"github.com/gogf/gf/frame/g"
+	"github.com/gogf/gf/net/ghttp"
 	"github.com/gogf/gf/os/glog"
 	"github.com/mojocn/base64Captcha"
 	"testing"
@@ -13,8 +16,9 @@ import (
 func TestDemo(t *testing.T) {
 	//t.Run("demo1" ,Demo1)
 	//t.Run("Adapters_test", Adapters)
-	t.Run("CaptchaDemo", CaptchaDemo)
-	t.Run("CaptchaVerify", CaptchaVerify)
+	//t.Run("CaptchaDemo", CaptchaDemo)
+	//t.Run("CaptchaVerify", CaptchaVerify)
+	t.Run("GTokenTest", GTokenTest)
 }
 
 func Demo1(t *testing.T) {
@@ -38,6 +42,30 @@ func Demo1(t *testing.T) {
 	}
 }
 
+func GTokenTest(t *testing.T) {
+	// 启动gtoken
+	gtoken := &gtoken.GfToken{
+		LoginPath:        "/login",
+		LoginBeforeFunc:  loginFunc,
+		LogoutPath:       "/user/logout",
+		AuthPaths:        g.SliceStr{"/system/*"},
+		LogoutBeforeFunc: loginOutFunc,
+	}
+	gtoken.Start()
+	s := g.Server()
+	s.BindHandler("/system/admin", func(r *ghttp.Request) {
+		r.Response.Write("hello admin")
+	})
+	s.SetPort(8080)
+	s.Run()
+}
+
+func loginFunc(r *ghttp.Request) (string, interface{}) {
+	return "yixiaohu", []g.MapStrStr{{"name": "张三", "age": "18"}, {"name": "李四", "age": "32"}}
+}
+func loginOutFunc(r *ghttp.Request) bool {
+	return true
+}
 func demoCodeCaptchaCreate() {
 	//config struct for digits
 	//数字验证码配置