Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
rudern
物联大师
提交
3627eb5a
物
物联大师
项目概览
rudern
/
物联大师
通知
5
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
物
物联大师
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
3627eb5a
编写于
10月 22, 2020
作者:
J
Jason
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
将session中间件 和 basic auth中间件移到 api 中
上级
1bc177e5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
44 addition
and
53 deletion
+44
-53
web/api/router.go
web/api/router.go
+33
-12
web/app.go
web/app.go
+11
-41
未找到文件。
web/api/router.go
浏览文件 @
3627eb5a
...
...
@@ -5,6 +5,7 @@ import (
"git.zgwit.com/zgwit/iot-admin/conf"
"git.zgwit.com/zgwit/iot-admin/models"
"github.com/gorilla/mux"
"github.com/quasoft/memstore"
"net/http"
"reflect"
)
...
...
@@ -31,6 +32,7 @@ type paramId2 struct {
Id
int64
`uri:"id"`
Id2
int64
`uri:"id2"`
}
//
//var (
// cookieNameForSessionID = "iot-admin"
...
...
@@ -53,15 +55,40 @@ type paramId2 struct {
func
RegisterRoutes
(
app
*
mux
.
Router
)
{
if
conf
.
Config
.
SysAdmin
.
Enable
{
//启用session
store
:=
memstore
.
NewMemStore
([]
byte
(
"iot-admin"
),
[]
byte
(
"iot-admin"
))
app
.
Use
(
func
(
next
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
sess
,
err
:=
store
.
Get
(
request
,
"iot-admin"
)
if
err
!=
nil
{
http
.
Error
(
writer
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
if
sess
.
IsNew
{
_
=
sess
.
Save
(
request
,
writer
)
}
//TODO 检查session,及权限
next
.
ServeHTTP
(
writer
,
request
)
})
})
//检查 session,必须登录
//app.Use(mustLogin)
}
else
if
conf
.
Config
.
BaseAuth
.
Enable
{
//检查HTTP认证
app
.
Use
(
func
(
next
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
if
username
,
password
,
ok
:=
request
.
BasicAuth
();
ok
{
if
pass
,
ok
:=
conf
.
Config
.
BaseAuth
.
Users
[
username
];
ok
&&
password
==
pass
{
next
.
ServeHTTP
(
writer
,
request
)
return
}
}
writer
.
Header
()
.
Set
(
"WWW-Authenticate"
,
`Basic realm="Please enter your username and password for this site"`
)
http
.
Error
(
writer
,
"Unauthorised"
,
http
.
StatusUnauthorized
)
})
})
//app.Use(gin.BasicAuth(gin.Accounts(conf.Config.BaseAuth.Users)))
//authConfig := basicauth.Config{
// Users: conf.Config.BaseAuth.Users,
//}
//app.Use(basicauth.New(authConfig))
}
else
{
//支持匿名访问
}
...
...
@@ -75,7 +102,7 @@ func RegisterRoutes(app *mux.Router) {
"disabled"
}
app
.
HandleFunc
(
"/project/{id}/tunnels"
,
curdApiListById
(
mod
,
"project_id"
))
.
Methods
(
"POST"
)
app
.
HandleFunc
(
"/tunnels"
,
curdApiList
(
mod
))
.
Methods
(
"POST"
)
app
.
HandleFunc
(
"/tunnel"
,
curdApiCreate
(
mod
,
nil
))
.
Methods
(
"POST"
)
//TODO 启动
app
.
HandleFunc
(
"/tunnel"
,
curdApiCreate
(
mod
,
nil
))
.
Methods
(
"POST"
)
//TODO 启动
app
.
HandleFunc
(
"/tunnel/{id}"
,
curdApiDelete
(
mod
,
nil
))
.
Methods
(
"DELETE"
)
//TODO 停止
app
.
HandleFunc
(
"/tunnel/{id}"
,
curdApiModify
(
mod
,
fields
,
nil
))
.
Methods
(
"PUT"
)
//TODO 重新启动
app
.
HandleFunc
(
"/tunnel/{id}"
,
curdApiGet
(
mod
))
.
Methods
(
"GET"
)
...
...
@@ -90,7 +117,7 @@ func RegisterRoutes(app *mux.Router) {
fields
=
[]
string
{
"name"
}
app
.
HandleFunc
(
"/tunnel/{id}/links"
,
curdApiListById
(
mod
,
"tunnel_id"
))
.
Methods
(
"POST"
)
app
.
HandleFunc
(
"/links"
,
curdApiList
(
mod
))
.
Methods
(
"POST"
)
app
.
HandleFunc
(
"/link/{id}"
,
curdApiDelete
(
mod
,
nil
))
.
Methods
(
"DELETE"
)
//TODO 停止
app
.
HandleFunc
(
"/link/{id}"
,
curdApiDelete
(
mod
,
nil
))
.
Methods
(
"DELETE"
)
//TODO 停止
app
.
HandleFunc
(
"/link/{id}"
,
curdApiModify
(
mod
,
fields
,
nil
))
.
Methods
(
"PUT"
)
app
.
HandleFunc
(
"/link/{id}"
,
curdApiGet
(
mod
))
.
Methods
(
"GET"
)
...
...
@@ -130,12 +157,6 @@ func RegisterRoutes(app *mux.Router) {
app
.
HandleFunc
(
"/project/{id}"
,
curdApiModify
(
mod
,
fields
,
nil
))
.
Methods
(
"PUT"
)
app
.
HandleFunc
(
"/project/{id}"
,
curdApiGet
(
mod
))
.
Methods
(
"GET"
)
//app.HandleFunc("/project/{id}/tunnels", nop)
//app.HandleFunc("/project/{id}/variables", nop)
//app.HandleFunc("/project/{id}/batches", nop)
//app.HandleFunc("/project/{id}/jobs", nop)
//app.HandleFunc("/project/{id}/strategies", nop)
//app.HandleFunc("/project/import", projectImport).Methods("POST")
//app.HandleFunc("/project/{id}/export", projectExport).Methods("GET")
//app.HandleFunc("/project/{id}/deploy", projectDeploy).Methods("GET")
...
...
web/app.go
浏览文件 @
3627eb5a
...
...
@@ -6,7 +6,6 @@ import (
"git.zgwit.com/zgwit/iot-admin/web/open"
wwwFiles
"git.zgwit.com/zgwit/iot-admin/web/www"
"github.com/gorilla/mux"
"github.com/quasoft/memstore"
"log"
"net/http"
"time"
...
...
@@ -20,12 +19,6 @@ func Serve() {
app
:=
mux
.
NewRouter
()
//GIN初始化
//app := gin.Default()
//app := iris.New()
//加入swagger会增加10MB多体积,使用github.com/zgwit/swagger-files,去除Map文件,可以节省7MB左右
//Swagger文档,需要先执行swag init生成文档
//app.Get("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
...
...
@@ -38,23 +31,6 @@ func Serve() {
//开放接口
open
.
RegisterRoutes
(
app
.
PathPrefix
(
"/open"
)
.
Subrouter
())
//启用session
store
:=
memstore
.
NewMemStore
([]
byte
(
"iot-admin"
),
[]
byte
(
"iot-admin"
))
app
.
Use
(
func
(
next
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
//sess.ID
sess
,
err
:=
store
.
Get
(
request
,
"iot-admin"
)
if
err
!=
nil
{
http
.
Error
(
writer
,
err
.
Error
(),
http
.
StatusInternalServerError
)
return
}
if
sess
.
IsNew
{
_
=
sess
.
Save
(
request
,
writer
)
}
next
.
ServeHTTP
(
writer
,
request
)
})
})
//授权检查,启用了SysAdmin的OAuth2,就不能再使用基本HTTP认证了
//if conf.Config.SysAdmin.Enable {
// //注册OAuth2相关接口
...
...
@@ -83,23 +59,17 @@ func Serve() {
}
//前端静态文件
//app.Get("/*any", func(c iris.Context) {
app
.
Use
(
func
(
next
http
.
Handler
)
http
.
Handler
{
return
http
.
HandlerFunc
(
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
if
request
.
Method
==
http
.
MethodGet
{
//支持前端框架的无“#”路由
if
request
.
RequestURI
==
"/"
{
request
.
URL
.
Path
=
"index.html"
}
else
if
_
,
err
:=
wwwFiles
.
FS
.
Stat
(
wwwFiles
.
CTX
,
request
.
RequestURI
);
err
!=
nil
{
request
.
URL
.
Path
=
"index.html"
}
//TODO 如果未登录,则跳转SysAdmin OAuth2自动授权页面
//文件失效期已经在Handler中处理
wwwFiles
.
Handler
.
ServeHTTP
(
writer
,
request
)
}
})
})
//app.PathPrefix("/").Handler(wwwFiles.Handler).Methods("GET")
app
.
PathPrefix
(
"/"
)
.
HandlerFunc
(
func
(
writer
http
.
ResponseWriter
,
request
*
http
.
Request
)
{
//支持前端框架的无“#”路由
if
request
.
RequestURI
==
"/"
{
request
.
URL
.
Path
=
"index.html"
}
else
if
_
,
err
:=
wwwFiles
.
FS
.
Stat
(
wwwFiles
.
CTX
,
request
.
RequestURI
);
err
!=
nil
{
request
.
URL
.
Path
=
"index.html"
}
//文件失效期已经在Handler中处理
wwwFiles
.
Handler
.
ServeHTTP
(
writer
,
request
)
})
.
Methods
(
"GET"
)
//监听HTTP
srv
:=
&
http
.
Server
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录