瀏覽代碼

update sample

jflyfox 4 年之前
父節點
當前提交
d2d065761a

+ 0 - 3
example/public/resource/css/index.css

@@ -1,3 +0,0 @@
-body {
-    text-align: center;
-}

二進制
example/public/resource/image/favicon.ico


+ 0 - 0
example/public/resource/js/.gitkeep


+ 5 - 11
example/config/config.toml → example/sample1/config/config.toml

@@ -1,20 +1,14 @@
-log-path = "./logs"
-http-port = 80
+[server]
+    Address        = "127.0.0.1:80"
+    ServerAgent    = "gtoken-demo"
+    LogPath        = "./example/sample1/logs"
+
 [gtoken]
     # 缓存模式 1 gcache 2 gredis
     cache-mode = 1
     # 是否支持多端登录
     multi-login = true
 
-# title设置
-[setting]
-    title = "token demo"
-
-# WebServer管理
-[admin]
-    user = "admin"
-    pass = "123"
-
 # Redis数据库配置
 [redis]
   default = "127.0.0.1:16379,0,soccer"

+ 7 - 85
example/main.go → example/sample1/main.go

@@ -5,7 +5,6 @@ import (
 	"github.com/gogf/gf/frame/g"
 	"github.com/gogf/gf/net/ghttp"
 	"github.com/gogf/gf/os/glog"
-	"github.com/gogf/gf/text/gstr"
 )
 
 var TestServerName string
@@ -13,54 +12,22 @@ var TestServerName string
 //var TestServerName string = "gtoken"
 
 func main() {
-	g.Server(TestServerName).Run()
-}
-
-// 管理初始化顺序.
-func init() {
-	initConfig()
-	initRouter()
-}
-
-// 用于配置初始化.
-func initConfig() {
 	glog.Info("########service start...")
 
-	v := g.View()
-	c := g.Config()
+	g.Cfg().SetPath("example/sample1")
 	s := g.Server(TestServerName)
-
-	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.URI_TYPE_ALLLOWER)
-	s.SetLogPath(logPath)
-	s.SetErrorLogEnabled(true)
-	s.SetAccessLogEnabled(true)
-	s.SetPort(c.GetInt("http-port"))
+	initRouter(s)
 
 	glog.Info("########service finish.")
+	s.Run()
 }
 
 var gfToken *gtoken.GfToken
 
 /*
-绑定业务路由
+统一路由注册
 */
-func bindRouter() {
-
-	s := g.Server(TestServerName)
-
+func initRouter(s *ghttp.Server) {
 	s.Group("/", func(g *ghttp.RouterGroup) {
 		g.Middleware(CORS)
 
@@ -94,36 +61,6 @@ func bindRouter() {
 		MultiLogin:       g.Config().GetBool("gtoken.multi-login"),
 	}
 	gfToken.Start()
-
-}
-
-/*
-统一路由注册
-*/
-func initRouter() {
-
-	s := g.Server(TestServerName)
-
-	// 绑定路由
-	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{}) {
@@ -138,23 +75,8 @@ func Login(r *ghttp.Request) (string, interface{}) {
 	return username, "1"
 }
 
+// 跨域
 func CORS(r *ghttp.Request) {
-	opt := ghttp.CORSOptions{
-		AllowOrigin:      "*",
-		AllowMethods:     ghttp.HTTP_METHODS,
-		AllowCredentials: "true",
-		AllowHeaders:     "Origin,Content-Type,Accept,User-Agent,Cookie,Authorization,X-Auth-Token,X-Requested-With,version,time,uuid,sign",
-		MaxAge:           3628800,
-	}
-	if origin := r.Request.Header.Get("Origin"); origin != "" {
-		opt.AllowOrigin = origin
-	} else if referer := r.Request.Referer(); referer != "" {
-		if p := gstr.PosR(referer, "/", 6); p != -1 {
-			opt.AllowOrigin = referer[:p]
-		} else {
-			opt.AllowOrigin = referer
-		}
-	}
-	r.Response.CORS(opt)
+	r.Response.CORSDefault()
 	r.Middleware.Next()
 }

+ 0 - 0
example/test/api.http → example/sample1/test/api.http


+ 0 - 0
example/test/api_test.go → example/sample1/test/api_test.go


+ 0 - 0
example/test/cache_test.go → example/sample1/test/cache_test.go


+ 0 - 0
example/test/time_test.go → example/sample1/test/time_test.go


+ 0 - 16
example/template/index.html

@@ -1,16 +0,0 @@
-<!DOCTYPE html>
-<html lang="en">
-<head>
-    <meta charset="UTF-8">
-    <title>${.title}</title>
-    <link rel="icon" href="/resource/image/favicon.ico" type="image/x-icon">
-    <link rel="stylesheet" href="/resource/css/index.css">
-</head>
-<body>
-<div class="container">
-    <div>
-        欢迎您访问${.name}示例
-    </div>
-</div>
-</body>
-</html>