From fa4156426da9584273417a1cf162afc01fb2f282 Mon Sep 17 00:00:00 2001 From: eson <474420502@qq.com> Date: Fri, 3 Jan 2020 02:12:31 +0800 Subject: [PATCH] TODO: Route --- .gitignore | 1 + config.go | 53 +++++++++++++++++++++++++++++++++++++++++ config.yaml | 3 +++ config_test.go | 16 +++++++++++++ go.mod | 8 ++++++- go.sum | 20 ++++++++++++++++ main.go | 55 +++++++++++++++++++++++++++++++++++++++---- main_test.go | 20 ++++++++++++++++ web/package-lock.json | 5 ++++ web/package.json | 1 + web/src/login.js | 34 ++++++++++++++++++-------- 11 files changed, 201 insertions(+), 15 deletions(-) create mode 100644 config.go create mode 100644 config.yaml create mode 100644 config_test.go create mode 100644 main_test.go diff --git a/.gitignore b/.gitignore index 722d5e7..31727ed 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .vscode +configworker diff --git a/config.go b/config.go new file mode 100644 index 0000000..1fc471d --- /dev/null +++ b/config.go @@ -0,0 +1,53 @@ +package main + +import ( + "log" + "os" + "sync" + + "gopkg.in/yaml.v2" +) + +// GlobalConfig 初始化后的全局配置 +var GlobalConfig *Config = loadConfig() + +// User 用户结构 +// type User struct { +// // Name 用户名 +// Name string `yaml:"name"` +// // Password 密码 +// Password string `yaml:"pwd"` +// } + +// Config 配置结构 +type Config struct { + // Users 账号密码集 + Users map[string]string `yaml:"users"` + configLock *sync.Mutex +} + +// GetUser Get return user map[string]string +func (config *Config) GetUser(key string) (string, bool) { + config.configLock.Lock() + value, ok := config.Users[key] + config.configLock.Unlock() + return value, ok +} + +func loadConfig() *Config { + f, err := os.Open("config.yaml") + if err != nil { + log.Panic(err) + } + + config := &Config{} + config.configLock = &sync.Mutex{} + + dec := yaml.NewDecoder(f) + if err = dec.Decode(config); err != nil { + log.Panic(err) + } + + return config + // log.Panic(spew.Sdump(config)) +} diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..43901c3 --- /dev/null +++ b/config.yaml @@ -0,0 +1,3 @@ +users: + eson: xxx + admin: yame \ No newline at end of file diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..628d295 --- /dev/null +++ b/config_test.go @@ -0,0 +1,16 @@ +package main + +import "testing" + +func TestLoadConifg(t *testing.T) { + config := loadConfig() + if len(config.Users) >= 2 { + if value, ok := config.Users["eson"]; !ok { + t.Error(value) + } + + if value, ok := config.GetUser("admin"); !ok { + t.Error(value) + } + } +} diff --git a/go.mod b/go.mod index b4de616..a240732 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,10 @@ module configworker go 1.13 -require github.com/gin-gonic/gin v1.5.0 +require ( + github.com/davecgh/go-spew v1.1.1 + github.com/gin-contrib/sessions v0.0.3 + github.com/gin-gonic/gin v1.5.0 + github.com/go-yaml/yaml v2.1.0+incompatible // indirect + gopkg.in/yaml.v2 v2.2.2 +) diff --git a/go.sum b/go.sum index 2182a9f..587c681 100644 --- a/go.sum +++ b/go.sum @@ -1,27 +1,47 @@ +github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff/go.mod h1:+RTT1BOk5P97fT2CiHkbFQwkK3mjsFAP6zCYV2aXtjw= +github.com/bradfitz/gomemcache v0.0.0-20190329173943-551aad21a668/go.mod h1:H0wQNHz2YrLsuXOZozoeDmnHXkNCRmMW0gwFWDfEZDA= +github.com/bradleypeabody/gorilla-sessions-memcache v0.0.0-20181103040241-659414f458e1/go.mod h1:dkChI7Tbtx7H1Tj7TqGSZMOeGpMP5gLHtjroHd4agiI= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/gin-contrib/sessions v0.0.3 h1:PoBXki+44XdJdlgDqDrY5nDVe3Wk7wDV/UCOuLP6fBI= +github.com/gin-contrib/sessions v0.0.3/go.mod h1:8C/J6cad3Il1mWYYgtw0w+hqasmpvy25mPkXdOgeB9I= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.5.0 h1:fi+bqFAx/oLK54somfCtEZs9HeH1LHVoEPUgARpTqyc= github.com/gin-gonic/gin v1.5.0/go.mod h1:Nd6IXA8m5kNZdNEHMBd93KT+mdY3+bewLgRvmCsR2Do= +github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8/go.mod h1:xkRDCp4j0OGD1HRkm4kmhM+pmpv3AKq5SU7GMg4oO/Q= github.com/go-playground/locales v0.12.1 h1:2FITxuFt/xuCNP1Acdhv62OzaCiviiE4kotfhkmOqEc= github.com/go-playground/locales v0.12.1/go.mod h1:IUMDtCfWo/w/mtMfIE/IG2K+Ey3ygWanZIBtBW0W2TM= github.com/go-playground/universal-translator v0.16.0 h1:X++omBR/4cE2MNg91AoC3rmGrCjJ8eAeUP/K/EKx4DM= github.com/go-playground/universal-translator v0.16.0/go.mod h1:1AnU7NaIRDWWzGEKwgtJRd2xk99HeFyHw3yid4rvQIY= +github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= +github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/context v1.1.1 h1:AWwleXJkX/nhcU9bZSnZoi3h/qGYqQAGhq6zZe/aQW8= +github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg= +github.com/gorilla/securecookie v1.1.1 h1:miw7JPhV+b/lAHSXz4qd/nN9jRiAFV5FwjeKyCS8BvQ= +github.com/gorilla/securecookie v1.1.1/go.mod h1:ra0sb63/xPlUeL+yeDciTfxMRAA+MP+HVt/4epWDjd4= +github.com/gorilla/sessions v1.1.1/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w= +github.com/gorilla/sessions v1.1.3 h1:uXoZdcdA5XdXF3QzuSlheVRUvjl+1rKY7zBXL68L9RU= +github.com/gorilla/sessions v1.1.3/go.mod h1:8KCfur6+4Mqcc6S0FEfKuN15Vl5MgXW92AE8ovaJD0w= github.com/json-iterator/go v1.1.7 h1:KfgG9LzI+pYjr4xvmz/5H4FXjokeP+rlHLhv3iH62Fo= github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/kidstuff/mongostore v0.0.0-20181113001930-e650cd85ee4b/go.mod h1:g2nVr8KZVXJSS97Jo8pJ0jgq29P6H7dG0oplUA86MQw= github.com/leodido/go-urn v1.1.0 h1:Sm1gr51B1kKyfD2BlRcLSiEkffoG96g6TPv6eRoEiB8= github.com/leodido/go-urn v1.1.0/go.mod h1:+cyI34gQWZcE1eQU7NVgKkkzdXDQHr1dBMtdAPozLkw= github.com/mattn/go-isatty v0.0.9 h1:d5US/mDsogSGW37IV293h//ZFaeajb69h+EHFsv2xGg= github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= +github.com/memcachier/mc v2.0.1+incompatible/go.mod h1:7bkvFE61leUBvXz+yxsOnGBQSZpBSPIMUQSmmSHvuXc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742 h1:Esafd1046DLDQ0W1YjYsBW+p8U2u7vzgW2SQVmlNazg= github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/quasoft/memstore v0.0.0-20180925164028-84a050167438/go.mod h1:wTPjTepVu7uJBYgZ0SdWHQlIas582j6cn2jgk4DDdlg= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= diff --git a/main.go b/main.go index 8fa2f7c..5fdd72c 100644 --- a/main.go +++ b/main.go @@ -1,15 +1,62 @@ package main -import "github.com/gin-gonic/gin" +import ( + "log" + "net/http" -import "log" + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/cookie" + "github.com/gin-gonic/gin" +) + +const ( + // SessionUser 用户登录的Session标签 + SessionUser = "token" +) + +func auth(ctx *gin.Context) { + if ctx.Request.RequestURI != "/api/login" { + session := sessions.Default(ctx) + if token := session.Get(SessionUser); token == nil { + ctx.Redirect(http.StatusNotModified, "/api/login") + return + } + } + ctx.Next() +} func login(ctx *gin.Context) { - ctx.Writer.WriteString("hello") + ctx.Request.ParseForm() + user := ctx.PostForm("user") + + if realPassword, ok := GlobalConfig.GetUser(user); ok { + + pwd := ctx.PostForm("pwd") + if realPassword == pwd { + session := sessions.Default(ctx) + session.Set(SessionUser, user) + session.Save() + } else { + ctx.JSON(http.StatusUnauthorized, gin.H{"error": "密码错误"}) + return + } + } else { + ctx.JSON(http.StatusUnauthorized, gin.H{"error": "不存在该用户"}) + return + } + + ctx.JSON(http.StatusOK, gin.H{"message": "登录成功"}) + ctx.Redirect(http.StatusSeeOther, "/worker") + } func main() { + eg := gin.New() - eg.GET("/api/login", login) + + eg.Use(sessions.Sessions(SessionUser, cookie.NewStore([]byte("yame")))) + eg.Use(auth) + + eg.POST("/api/login", login) log.Fatal(eg.Run(":3001")) } diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..d5d7fa5 --- /dev/null +++ b/main_test.go @@ -0,0 +1,20 @@ +package main + +import ( + "log" + "testing" + + "github.com/gin-contrib/sessions" + "github.com/gin-contrib/sessions/cookie" + "github.com/gin-gonic/gin" +) + +func TestMain(t *testing.T) { + eg := gin.New() + + eg.Use(sessions.Sessions(SessionUser, cookie.NewStore([]byte("yame")))) + eg.Use(auth) + + eg.POST("/api/login", login) + log.Fatal(eg.Run(":3001")) +} diff --git a/web/package-lock.json b/web/package-lock.json index cfdae64..4220b49 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -8544,6 +8544,11 @@ "resolved": "https://registry.npmjs.org/js-levenshtein/-/js-levenshtein-1.1.6.tgz", "integrity": "sha512-X2BB11YZtrRqY4EnQcLX5Rh373zbK4alC1FW7D7MBhL2gtcC17cTnr6DmfHZeS0s2rTHjUTMMHfG7gO8SSdw+g==" }, + "js-md5": { + "version": "0.7.3", + "resolved": "https://registry.npmjs.org/js-md5/-/js-md5-0.7.3.tgz", + "integrity": "sha512-ZC41vPSTLKGwIRjqDh8DfXoCrdQIyBgspJVPXHBGu4nZlAEvG3nf+jO9avM9RmLiGakg7vz974ms99nEV0tmTQ==" + }, "js-tokens": { "version": "4.0.0", "resolved": "http://registry.npm.taobao.org/js-tokens/download/js-tokens-4.0.0.tgz", diff --git a/web/package.json b/web/package.json index f482283..4a0d31f 100644 --- a/web/package.json +++ b/web/package.json @@ -7,6 +7,7 @@ "axios": "^0.19.0", "babel-plugin-import": "^1.13.0", "customize-cra": "^0.9.1", + "js-md5": "^0.7.3", "less": "^3.10.3", "less-loader": "^5.0.0", "react": "^16.12.0", diff --git a/web/src/login.js b/web/src/login.js index 69ebf6b..966d5f5 100644 --- a/web/src/login.js +++ b/web/src/login.js @@ -1,7 +1,10 @@ import React from 'react'; +import Route from 'react-route'; import axios from 'axios'; import './login.css'; -import { TreeSelect, Row, Col, Input, Layout, Button } from 'antd'; +import App from './App'; +import { Row, Col, Input, Layout, Button, Icon, Tooltip } from 'antd'; +import Title from 'antd/lib/typography/Title'; class Login extends React.Component { @@ -9,9 +12,12 @@ class Login extends React.Component { var user = this.refs["login-user"].input.value var pwd = this.refs["login-passwd"].input.value console.log(user, pwd); - - axios.get("/api/login").then(value => { - console.log(value) + var form = new FormData(); + form.append("user", user); + form.append("pwd", pwd); + axios.post("/api/login", form).then(value => { + console.log(value); + return }); } @@ -19,19 +25,27 @@ class Login extends React.Component { return ( -
+
+ Apollo Local + + - + } + suffix={ + + } + /> - + } + + /> - - -