Skip to content

Commit

Permalink
update web
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed Jan 14, 2019
1 parent b6ab685 commit 37937e7
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 56 deletions.
7 changes: 4 additions & 3 deletions vgo/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ package cmd

import (
"fmt"
"os"
"os/signal"
"syscall"

"github.com/mafanr/g"
"github.com/mafanr/vgo/vgo/misc"
"github.com/mafanr/vgo/vgo/service"
"go.uber.org/zap"
"os"
"os/signal"
"syscall"

"github.com/spf13/cobra"
)
Expand Down
27 changes: 7 additions & 20 deletions web/misc/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,21 @@ type Config struct {
AdminToken string
}

Vgo struct {
ListenAddr string
AgentTimeout int
}

Etcd struct {
Addrs []string
Dltimeout int
WatchKey string
}

Storage struct {
Cluster []string
Keyspace string
NumConns int
SpanCacheLen int // 缓存长度
SpanChunkCacheLen int //
SpanStoreInterval int // 毫秒
AgentStatUseTTL bool
AgentStatTTL int64
Login struct {
SsoLogin string
SsoLogout string
}

Mysql struct {
Addr string
Port string
Database string
Acc string
Pw string
Storage struct {
Cluster []string
Keyspace string
NumConns int
}

Web struct {
Expand Down
172 changes: 172 additions & 0 deletions web/service/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package service

import (
"encoding/json"
"fmt"
"net/http"

"time"

"github.com/mafanr/vgo/web/misc"

"go.uber.org/zap"

"github.com/labstack/echo"
"github.com/mafanr/g"
"github.com/valyala/fasthttp"
)

var defaultRole = "normal"

type Session struct {
User *UserInfo
CreateTime time.Time
}

type UserInfo struct {
ID string `json:"id"`
Name string `json:"name"`
Avatar string `json:"avatar"`
Role []string `json:"role"`
SsoToken string `json:"ssoToken"`
}

func (web *Web) login(c echo.Context) error {
subToken := c.FormValue("subToken")
// 通过subtoken获取用户和ssotoken
body := "{'subToken':'" + subToken + "'}"
url := misc.Conf.Login.SsoLogin

b := requestToSso(body, url)
tokenInfo := &TokenInfo{}
err := json.Unmarshal(b, tokenInfo)
if err != nil {
g.L.Info("解析sso用户信息失败", zap.Error(err), zap.String("body", string(b)))
return nil
}

uid := tokenInfo.Data.SubTokenObj.UserSession.UserId

user := &UserInfo{
ID: uid,
Name: tokenInfo.Data.SubTokenObj.UserSession.UserName,
Avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
SsoToken: tokenInfo.Data.SubTokenObj.SsoToken,
}

if user.ID == "" {
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusUnauthorized,
Message: "sub token无效,请重新登陆",
})
}

//sub token验证成功,保存session
web.sessions.Store(user.SsoToken, &Session{
User: user,
CreateTime: time.Now(),
})

return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: user,
})
}

func (a *Web) loginMock(c echo.Context) error {
user := &UserInfo{
ID: "13269",
Name: "孙飞",
Avatar: "https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif",
SsoToken: "0af8c18eed353af38b2e2524f4850f76",
}

//sub token验证成功,保存session
a.sessions.Store(user.SsoToken, &Session{
User: user,
CreateTime: time.Now(),
})

return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: user,
})
}

func (a *Web) logout(c echo.Context) error {
token := getToken(c)
// 删除用户的session
a.sessions.Delete(token)

// 请求sso 注销token
body := "{'ssoToken':'" + token + "', 'clientNo':'1' }"
url := misc.Conf.Login.SsoLogout
b := requestToSso(body, url)
fmt.Println(string(b))
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Message: "退出登陆成功",
})
}

func (a *Web) userInfo(c echo.Context) error {
token := getToken(c)
sess, ok := a.sessions.Load(token)
if !ok {
// 用户未登陆或者session失效
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusUnauthorized,
ErrCode: g.NeedLoginC,
Message: g.NeedLoginE,
})
}

return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: sess.(*Session).User,
})
}

//subToken认证,返回的用户信息
type TokenInfo struct {
Code int `json:"code"`
Data struct {
SubTokenObj struct {
Message string `json:"message"`
SsoToken string `json:"ssoToken"`
Status int `json:"status"`
UserSession struct {
Facility string `json:"facility"`
HeadImgUrl string `json:"headImgUrl"`
SysDepartmentId string `json:"sysDepartmentId"`
UserId string `json:"userId"`
UserName string `json:"userName"`
UserType string `json:"userType"`
} `json:"userSession"`
} `json:"subTokenObj"`
} `json:"data"`
Message string `json:"message"`
}

func requestToSso(body string, url string) []byte {
req := &fasthttp.Request{}
resp := &fasthttp.Response{}

req.Header.SetMethod("POST")
req.Header.SetContentType("application/json")
req.SetBodyString(body)

req.SetRequestURI(url)
var cli = &fasthttp.Client{}
err := cli.DoTimeout(req, resp, 15*time.Second)
if err != nil {
g.L.Info("获取sso用户信息失败", zap.Error(err))
return nil
}

return resp.Body()
}

func getToken(c echo.Context) string {
return c.Request().Header.Get("X-Token")
}
30 changes: 15 additions & 15 deletions web/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
import (
"fmt"
"net/http"
"sync"
"time"

"github.com/gocql/gocql"
Expand All @@ -17,8 +18,9 @@ import (
// 后台服务
// Stats 离线计算
type Web struct {
Cql *gocql.Session
cache *cache
Cql *gocql.Session
cache *cache
sessions *sync.Map
}

// New ...
Expand Down Expand Up @@ -49,6 +51,9 @@ func (s *RetryPolicy) GetRetryType(err error) gocql.RetryType {

// Start ...
func (s *Web) Start() error {
// 用户登录session
s.sessions = &sync.Map{}

// 初始化内部缓存
s.cache = &cache{}
// 初始化Cql连接
Expand Down Expand Up @@ -81,26 +86,21 @@ func (s *Web) Start() error {

// 回调相关
//同步回调接口
e.POST("/login", func(c echo.Context) error {
return c.JSON(http.StatusOK, g.Result{
Status: http.StatusOK,
Data: "hello login",
})
})
e.POST("/apm/web/login", s.login)

// 应用查询接口
//查询应用列表
e.GET("/apm/query/appList", s.appList)
e.GET("/apm/web/appList", s.appList)
//获取指定应用的一段时间内的状态
e.GET("/apm/query/appDash", s.appDash)
e.GET("/apm/web/appDash", s.appDash)
//查询所有的app名
e.GET("/apm/query/appNames", s.appNames)
e.GET("/apm/web/appNames", s.appNames)
//查询所有服务器名
e.GET("/apm/query/agentList", s.agentList)
e.GET("/apm/web/agentList", s.agentList)

e.GET("/apm/query/serviceMap", queryServiceMap)
e.GET("/apm/query/traces", queryTraces)
e.GET("/apm/query/trace", queryTrace)
e.GET("/apm/web/serviceMap", queryServiceMap)
e.GET("/apm/web/traces", queryTraces)
e.GET("/apm/web/trace", queryTrace)

e.Logger.Fatal(e.Start(misc.Conf.Web.Addr))
}()
Expand Down
23 changes: 5 additions & 18 deletions web/web.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,13 @@ common:
loglevel: debug
admintoken: "vgo.io"

vgo:
# listenaddr: "10.50.18.157:8081"
listenaddr: "127.0.0.1:8081"
agenttimeout: 5

storage:
cluster:
- "10.100.7.46:9042"
# - "10.50.24.201:9042"
keyspace: "vgo_v1_datacenter"
numconns: 3
spancachelen: 1000
spanchunkcachelen: 1000
spanstoreinterval: 500
# stat信息是否自动删除
agentstatusettl: false
# 数据库种保存多久,单位秒
agentstatttl: 6

etcd:
addrs:
Expand All @@ -29,12 +18,10 @@ etcd:
dltimeout: 10
watchkey: "vvvblink"

mysql:
addr: "localhost"
port: "3306"
database: "vgo"
acc: "root"
pw: ""

web:
addr: ":8085"
addr: ":8085"

login:
ssologin: "http://10.7.24.3/opensso/auth/validateSubToken"
ssologout: "http://10.7.24.3/opensso/auth/ssoLogout"

0 comments on commit 37937e7

Please sign in to comment.