7 years ago
parent
commit
bb67f43d27
5 changed files with 50 additions and 5 deletions
  1. 13 0
      src/jy/admin/admin.go
  2. 1 1
      src/jy/front/front.go
  3. 17 1
      src/jy/router/router.go
  4. 5 3
      src/main.go
  5. 14 0
      src/web/admin/a1.html

+ 13 - 0
src/jy/admin/admin.go

@@ -0,0 +1,13 @@
+package admin
+
+import (
+	. "jy/router"
+)
+
+func init() {
+	admin := Router.Group("/admin")
+	admin.Use(AuthMiddleWare())
+	{
+		admin.Static("/web", "./web/admin")
+	}
+}

+ 1 - 1
src/jy/front/front.go

@@ -12,7 +12,7 @@ import (
 
 func init() {
 	//登录
-	Router.POST("/login", func(c *gin.Context) {
+	Router.GET("/login", func(c *gin.Context) {
 		name := c.GetString("name")
 		pwd := c.GetString("pwd")
 		res, b := Mgo.FindOne("user", fmt.Sprintf(`{"name":"%s","pwd":"%s"}`, name, pwd))

+ 17 - 1
src/jy/router/router.go

@@ -1,6 +1,8 @@
 package router
 
 import (
+	"net/http"
+
 	"github.com/gin-contrib/sessions"
 	"github.com/gin-contrib/sessions/memstore"
 	"github.com/gin-gonic/gin"
@@ -11,5 +13,19 @@ var Router = gin.Default()
 func init() {
 	store := memstore.NewStore([]byte("jy_data_extract"))
 	Router.Use(sessions.Sessions("mysession", store))
-	Router.Run(":9090")
+}
+
+func AuthMiddleWare() gin.HandlerFunc {
+	return func(c *gin.Context) {
+		session := sessions.Default(c)
+		if session.Get("name") != nil {
+			c.Next()
+			return
+		}
+		c.JSON(http.StatusUnauthorized, gin.H{
+			"error": "Unauthorized",
+		})
+		c.Abort()
+		return
+	}
 }

+ 5 - 3
src/main.go

@@ -1,14 +1,16 @@
 package main
 
 import (
-	"jy/front"
+	_ "jy/admin"
+	_ "jy/front"
 	_ "jy/mongodbutil"
+	. "jy/router"
 	"log"
 	"time"
 )
 
 func main() {
-	go front.Server()
-	log.Println("启动..")
+	go Router.Run(":9090")
+	go log.Println("启动..")
 	time.Sleep(99999 * time.Hour)
 }

+ 14 - 0
src/web/admin/a1.html

@@ -0,0 +1,14 @@
+<!doctype html>
+<html lang="en">
+ <head>
+  <meta charset="UTF-8">
+  <meta name="Generator" content="EditPlus®">
+  <meta name="Author" content="">
+  <meta name="Keywords" content="">
+  <meta name="Description" content="">
+  <title>Document</title>
+ </head>
+ <body>
+  aaaaaaaaaaaaaaaaa
+ </body>
+</html>