Forráskód Böngészése

添加请求js记录访问次数

tangs 7 éve
szülő
commit
fb58342a3a
39 módosított fájl, 245 hozzáadás és 23 törlés
  1. 61 6
      .idea/misc.xml
  2. 9 0
      blogScript.iml
  3. 0 0
      src/github.com/tangs-drm/go-tool/README.md
  4. 0 0
      src/github.com/tangs-drm/go-tool/config/README.md
  5. 0 0
      src/github.com/tangs-drm/go-tool/config/config.conf
  6. 0 0
      src/github.com/tangs-drm/go-tool/config/config.go
  7. 0 0
      src/github.com/tangs-drm/go-tool/config/config_test.go
  8. 0 0
      src/github.com/tangs-drm/go-tool/config/errconfigType.type
  9. 0 0
      src/github.com/tangs-drm/go-tool/config/testconfig.conf
  10. 0 0
      src/github.com/tangs-drm/go-tool/dbm/README.md
  11. 0 0
      src/github.com/tangs-drm/go-tool/dbm/mysql.go
  12. 0 0
      src/github.com/tangs-drm/go-tool/dbm/mysql_test.go
  13. 0 0
      src/github.com/tangs-drm/go-tool/http/README.md
  14. 1 0
      src/github.com/tangs-drm/go-tool/http/data/test.txt
  15. 63 15
      src/github.com/tangs-drm/go-tool/http/http.go
  16. 46 0
      src/github.com/tangs-drm/go-tool/http/http_test.go
  17. 0 0
      src/github.com/tangs-drm/go-tool/http/response.go
  18. 0 0
      src/github.com/tangs-drm/go-tool/log/.gitignore
  19. 0 0
      src/github.com/tangs-drm/go-tool/log/README.md
  20. 0 0
      src/github.com/tangs-drm/go-tool/log/log.go
  21. 0 0
      src/github.com/tangs-drm/go-tool/log/log_test.go
  22. 0 0
      src/github.com/tangs-drm/go-tool/util/README.md
  23. 0 0
      src/github.com/tangs-drm/go-tool/util/error.go
  24. 0 0
      src/github.com/tangs-drm/go-tool/util/error_test.go
  25. 0 0
      src/github.com/tangs-drm/go-tool/util/map.go
  26. 0 0
      src/github.com/tangs-drm/go-tool/util/map_test.go
  27. 0 0
      src/github.com/tangs-drm/go-tool/util/request.go
  28. 0 0
      src/github.com/tangs-drm/go-tool/util/request_test.go
  29. 0 0
      src/github.com/tangs-drm/go-tool/util/time.go
  30. 0 0
      src/github.com/tangs-drm/go-tool/util/time_test.go
  31. 0 0
      src/github.com/tangs-drm/go-tool/util/value.go
  32. 0 0
      src/github.com/tangs-drm/go-tool/util/value_test.go
  33. 9 1
      src/main.go
  34. 3 1
      src/view/viewdb/db.go
  35. 0 0
      src/www/index.html
  36. 14 0
      www/common/jquery-3.2.1.min/.bower.json
  37. 4 0
      www/common/jquery-3.2.1.min/index.js
  38. 14 0
      www/index.html
  39. 21 0
      www/js/page_view.js

+ 61 - 6
.idea/misc.xml

@@ -1,11 +1,66 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <project version="4">
-  <component name="GOROOT" path="E:\Go" />
-  <component name="GoLibraries">
-    <option name="urls">
-      <list>
-        <option value="file://$PROJECT_DIR$" />
-      </list>
+  <component name="MavenImportPreferences">
+    <option name="generalSettings">
+      <MavenGeneralSettings>
+        <option name="mavenHome" value="Bundled (Maven 3)" />
+      </MavenGeneralSettings>
     </option>
   </component>
+  <component name="ProjectInspectionProfilesVisibleTreeState">
+    <entry key="Project Default">
+      <profile-state>
+        <expanded-state>
+          <State>
+            <id />
+          </State>
+        </expanded-state>
+        <selected-state>
+          <State>
+            <id>Android</id>
+          </State>
+        </selected-state>
+      </profile-state>
+    </entry>
+  </component>
+  <component name="ProjectLevelVcsManager" settingsEditedManually="false">
+    <OptionsSetting value="true" id="Add" />
+    <OptionsSetting value="true" id="Remove" />
+    <OptionsSetting value="true" id="Checkout" />
+    <OptionsSetting value="true" id="Update" />
+    <OptionsSetting value="true" id="Status" />
+    <OptionsSetting value="true" id="Edit" />
+    <ConfirmationsSetting value="0" id="Add" />
+    <ConfirmationsSetting value="0" id="Remove" />
+  </component>
+  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="Go 1.8.3" project-jdk-type="Go SDK">
+    <output url="file://$PROJECT_DIR$/out" />
+  </component>
+  <component name="masterDetails">
+    <states>
+      <state key="Copyright.UI">
+        <settings>
+          <splitter-proportions>
+            <option name="proportions">
+              <list>
+                <option value="0.2" />
+              </list>
+            </option>
+          </splitter-proportions>
+        </settings>
+      </state>
+      <state key="ProjectJDKs.UI">
+        <settings>
+          <last-edited>Go 1.8.3</last-edited>
+          <splitter-proportions>
+            <option name="proportions">
+              <list>
+                <option value="0.2" />
+              </list>
+            </option>
+          </splitter-proportions>
+        </settings>
+      </state>
+    </states>
+  </component>
 </project>

+ 9 - 0
blogScript.iml

@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="GO_MODULE" version="4">
+  <component name="NewModuleRootManager" inherit-compiler-output="true">
+    <exclude-output />
+    <content url="file://$MODULE_DIR$" />
+    <orderEntry type="inheritedJdk" />
+    <orderEntry type="sourceFolder" forTests="false" />
+  </component>
+</module>

+ 0 - 0
src/github.com/tangs-drm/go-tool/README.md


+ 0 - 0
src/github.com/tangs-drm/go-tool/config/README.md


+ 0 - 0
src/github.com/tangs-drm/go-tool/config/config.conf


+ 0 - 0
src/github.com/tangs-drm/go-tool/config/config.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/config/config_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/config/errconfigType.type


+ 0 - 0
src/github.com/tangs-drm/go-tool/config/testconfig.conf


+ 0 - 0
src/github.com/tangs-drm/go-tool/dbm/README.md


+ 0 - 0
src/github.com/tangs-drm/go-tool/dbm/mysql.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/dbm/mysql_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/http/README.md


+ 1 - 0
src/github.com/tangs-drm/go-tool/http/data/test.txt

@@ -0,0 +1 @@
+qwertyuiopasdfghj

+ 63 - 15
src/github.com/tangs-drm/go-tool/http/http.go

@@ -17,8 +17,8 @@ const (
 )
 
 type Mux struct {
-	Handle map[string]http.Handler // 请求路由
-	Filter map[string]FilterFunc   // 过滤器
+	Handles map[string]http.Handler // 请求路由
+	Filters map[string]FilterFunc   // 过滤器
 }
 
 // ServeHTTP 实现了http的ServeHTTP接口,以实现http的封装
@@ -45,8 +45,8 @@ func (mux *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 func NewServerMux() *Mux {
 	return &Mux{
-		Handle: map[string]http.Handler{},
-		Filter: map[string]FilterFunc{},
+		Handles:map[string]http.Handler{},
+		Filters:map[string]FilterFunc{},
 	}
 }
 
@@ -55,12 +55,20 @@ var DefaultHandle = NewServerMux()
 
 // HandleFunc 设置路由和对应的处理handler
 func HandleFunc(pre string, handler func(http.ResponseWriter, *http.Request)) {
-	DefaultHandle.Handle[pre] = http.HandlerFunc(handler)
+	DefaultHandle.Handles[pre] = http.HandlerFunc(handler)
 }
 
 // HandleFunc 设置路由和对应的处理handler
 func (mux *Mux) HandleFunc(pre string, handler func(http.ResponseWriter, *http.Request)) {
-	mux.Handle[pre] = http.HandlerFunc(handler)
+	mux.Handles[pre] = http.HandlerFunc(handler)
+}
+
+func Handle(pre string, handler http.Handler) {
+	DefaultHandle.Handles[pre] = handler
+}
+
+func (mux *Mux) Handle(pre string, handler http.Handler) {
+	mux.Handles[pre] = handler
 }
 
 // FilterFunc 定义过滤器
@@ -70,30 +78,70 @@ type FilterFunc func(http.ResponseWriter, *http.Request) int
 
 // Filter设置过滤器
 func Filter(prefix string, filter FilterFunc) {
-	DefaultHandle.Filter[prefix] = filter
+	DefaultHandle.Filters[prefix] = filter
 }
 
 // FilterFunc 设置过滤器
 func (mux *Mux) FilterFunc(prefix string, filter FilterFunc) {
-	mux.Filter[prefix] = filter
+	mux.Filters[prefix] = filter
 }
 
 // deliverHandler 根据请求进来的路由找到对应的handler, 找不到则返回nil
 func (mux *Mux) deliverHandler(path string) http.Handler {
-	for key, handler := range mux.Handle {
-		if path == key {
-			return handler
-		}
-	}
-	return nil
+	return mux.matchHandler(path)
 }
 
 // deliverFilter 根据请求进来的路由找到对应的过滤器,找不到则返回nil
 func (mux *Mux) deliverFilter(path string) FilterFunc {
-	for key, filter := range mux.Filter {
+	for key, filter := range mux.Filters {
 		if strings.HasPrefix(path, key) {
 			return filter
 		}
 	}
 	return nil
 }
+
+func ListenAndServe(addr string, handler http.Handler) error {
+	if handler == nil {
+		handler = DefaultHandle
+	}
+	return http.ListenAndServe(addr, handler)
+}
+
+func ListenAndServeTLS(addr, certFile, keyFile string, handler http.Handler) error {
+	if handler == nil {
+		handler = DefaultHandle
+	}
+	return http.ListenAndServeTLS(addr, certFile, keyFile, handler)
+}
+
+// Does path match pattern?
+// copy from net/http/server.go
+func pathMatch(pattern, path string) bool {
+	if len(pattern) == 0 {
+		// should not happen
+		return false
+	}
+	n := len(pattern)
+	if pattern[n-1] != '/' {
+		return pattern == path
+	}
+	return len(path) >= n && path[0:n] == pattern
+}
+
+// Find a handler on a handler map given a path string
+// Most-specific (longest) pattern wins
+// copy from net/http/server.go and modify
+func (mux *Mux) matchHandler(path string) (h http.Handler) {
+	var n = 0
+	for k, v := range mux.Handles {
+		if !pathMatch(k, path) {
+			continue
+		}
+		if h == nil || len(k) > n {
+			n = len(k)
+			h = v
+		}
+	}
+	return
+}

+ 46 - 0
src/github.com/tangs-drm/go-tool/http/http_test.go

@@ -5,6 +5,8 @@ import (
 	"net/http"
 	"github.com/tangs-drm/go-tool/util"
 	"fmt"
+	"time"
+	"strings"
 )
 
 func HandleHalo(w http.ResponseWriter, r *http.Request) {
@@ -166,3 +168,47 @@ func TestHttp(t *testing.T) {
 
 	fmt.Println("TestHttp测试 Start")
 }
+
+func TestFileServer(t *testing.T) {
+	fmt.Println("TestFileServer 测试开始")
+	var err error
+	HandleFunc("/halo", HandleHalo)
+	Handle("/", http.FileServer(http.Dir(".")))
+	go func() {
+		err = ListenAndServe(":8901", nil)
+		if err != nil {
+			panic(err)
+		}
+	}()
+
+
+	time.Sleep(2*time.Second)
+
+	var resString string
+
+	// 检测访问文件,发送404
+	resString, err = util.HTTPGetString("%v/data/notfound.txt", "http://127.0.0.1:8901")
+	if err != nil {
+		t.Error(err)
+		return
+	}
+	fmt.Println(resString)
+
+	if !strings.Contains(resString, "not found") {
+		t.Error(resString)
+		return
+	}
+
+	// 检测访问文件,成功
+	resString, err = util.HTTPGetString("%v/data/test.txt", "http://127.0.0.1:8901")
+	if err != nil {
+		t.Error(err)
+		return
+	}
+	fmt.Println(resString)
+	if strings.Contains(resString, "not found") {
+		t.Error(resString)
+		return
+	}
+	fmt.Println("TestFileServer 测试完毕")
+}

+ 0 - 0
src/github.com/tangs-drm/go-tool/http/response.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/log/.gitignore


+ 0 - 0
src/github.com/tangs-drm/go-tool/log/README.md


+ 0 - 0
src/github.com/tangs-drm/go-tool/log/log.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/log/log_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/README.md


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/error.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/error_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/map.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/map_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/request.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/request_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/time.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/time_test.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/value.go


+ 0 - 0
src/github.com/tangs-drm/go-tool/util/value_test.go


+ 9 - 1
src/main.go

@@ -47,5 +47,13 @@ func main() {
 	fmt.Printf("[MAIN] process will start with port: %v \n", cfg.String("listen_port"))
 	log.Debug("[MAIN] process will start with port: %v", cfg.String("listen_port"))
 
-	fmt.Println(http.ListenAndServe(cfg.String("listen_port"), mux))
+	mux.Handle("/", http.FileServer(http.Dir("www")))
+	//mux.Handle("/common", http.StripPrefix("/common", http.FileServer(http.Dir("www/common"))))
+	//mux.Handle("/", http.FileServer(http.Dir("www")))
+	//mux.Handle("/common/jquery-3.2.1.min/", http.StripPrefix("/common/jquery-3.2.1.min", http.FileServer(http.Dir("www"))))
+	//mux.Handle("/js", http.FileServer(http.Dir("www/js")))
+
+	fmt.Println(thttp.ListenAndServe(cfg.String("listen_port"), mux))
+	//http.Handle("/", http.FileServer(http.Dir(".")))
+	//http.ListenAndServe(cfg.String("listen_port"), mux)
 }

+ 3 - 1
src/view/viewdb/db.go

@@ -1,6 +1,8 @@
 package viewdb
 
-import "gopkg.in/mgo.v2"
+import (
+	"gopkg.in/mgo.v2"
+)
 
 const (
 	VIEW_TIMES = "view_times"

+ 0 - 0
src/www/index.html


+ 14 - 0
www/common/jquery-3.2.1.min/.bower.json

@@ -0,0 +1,14 @@
+{
+  "name": "jquery-3.2.1.min",
+  "_cacheHeaders": {
+    "ETag": "W/\"58d026fb-15283\"",
+    "Last-Modified": "Mon, 20 Mar 2017 19:01:15 GMT",
+    "Content-Type": "application/javascript; charset=utf-8"
+  },
+  "_release": "e-tag:W/\"58d026f",
+  "main": "index.js",
+  "_source": "https://code.jquery.com/jquery-3.2.1.min.js",
+  "_target": "*",
+  "_originalSource": "https://code.jquery.com/jquery-3.2.1.min.js",
+  "_direct": true
+}

A különbségek nem kerülnek megjelenítésre, a fájl túl nagy
+ 4 - 0
www/common/jquery-3.2.1.min/index.js


+ 14 - 0
www/index.html

@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>记录访问次数</title>
+  <script type="text/javascript" src="common/jquery-3.2.1.min/index.js"></script>
+  <script type="text/javascript" src="js/page_view.js"></script>
+</head>
+
+<body onload="recordView()">
+记录访问次数
+<div id="page_times">请求中...</div>
+</body>
+
+</html>

+ 21 - 0
www/js/page_view.js

@@ -0,0 +1,21 @@
+var config = {
+  "blogRequestHost": "http://blogScript.5tangs.com"
+}
+
+function recordView() {
+  var aid = "123456789";
+  // var request = config["blogRequestHost"] + "/view_article?aid" + aid;
+  $.ajax({
+    "type": "GET",
+    "url": config["blogRequestHost"] + "/view_article?",
+    "data": {
+      "aid": aid
+    },
+    "dataType": "json",
+    "error": function (data) {
+      console.log(data);
+    },
+    "success": function (data) {
+    }
+  })
+}