TODO: Route

This commit is contained in:
eson 2020-01-03 02:12:31 +08:00
parent a5bb450d66
commit fa4156426d
11 changed files with 201 additions and 15 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
.vscode
configworker

53
config.go Normal file
View File

@ -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))
}

3
config.yaml Normal file
View File

@ -0,0 +1,3 @@
users:
eson: xxx
admin: yame

16
config_test.go Normal file
View File

@ -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)
}
}
}

8
go.mod
View File

@ -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
)

20
go.sum
View File

@ -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=

55
main.go
View File

@ -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"))
}

20
main_test.go Normal file
View File

@ -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"))
}

5
web/package-lock.json generated
View File

@ -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",

View File

@ -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",

View File

@ -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 <Route path='//' component={App} />
});
}
@ -19,19 +25,27 @@ class Login extends React.Component {
return (
<Layout style={{ minHeight: "100vh" }}>
<div style={{ marginTop: "40vh" }}>
<div style={{ marginTop: "30vh" }}>
<Row className="login-input" type="flex" justify="center"><Title>Apollo Local</Title></Row>
<Row className="login-input" type="flex" justify="center">
<Input ref="login-user" placeholder="Basic usage" style={{ width: "50vh" }} />
<Input ref="login-user" placeholder="用户" style={{ width: "50vh" }}
prefix={ <Tooltip title="用户名"> <Icon type="user" style={{ color: 'rgba(0,0,0,.25)' }} /> </Tooltip>}
suffix={
<Icon type="info-circle" style={{ color: 'rgba(0,0,0,.45)' }} />
}
/>
</Row>
<Row className="login-input" type="flex" justify="center">
<Input ref="login-passwd" placeholder="Basic usage" style={{ width: "50vh" }} />
<Input.Password ref="login-passwd" placeholder="密码" style={{ width: "50vh" }}
prefix={<Tooltip title="密码"> <Icon type="safety" style={{ color: 'rgba(0,0,0,.25)' }} /> </Tooltip>}
/>
</Row>
<Row className="login-input" type="flex" justify="center">
<Button className="login-button" onClick={this.onClick}>登录</Button>
</Row>
<Row className="login-input" type="flex" justify="center">
<Button className="login-button">登录</Button>
</Row>
</div>