diff --git a/main.go b/main.go index 26b8539..c2b2d3b 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/taoshihan1991/imaptool/docs" "github.com/taoshihan1991/imaptool/middleware" "github.com/taoshihan1991/imaptool/tmpl" + "github.com/taoshihan1991/imaptool/tools" "log" "os" "os/exec" @@ -118,5 +119,8 @@ func main() { docs.SwaggerInfo.BasePath = "/" //docs.SwaggerInfo.Schemes = []string{"http"} engine.GET("/docs/*any", ginSwagger.WrapHandler(swaggerFiles.Handler)) + + logFile, _ := os.OpenFile("./fatal.log", os.O_CREATE|os.O_APPEND|os.O_RDWR, 0660) + tools.RedirectStderr(logFile) engine.Run(baseServer) } diff --git a/middleware/rbac.go b/middleware/rbac.go new file mode 100644 index 0000000..2a9c877 --- /dev/null +++ b/middleware/rbac.go @@ -0,0 +1,38 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "github.com/taoshihan1991/imaptool/models" + "strings" +) + +func RbacAuth(c *gin.Context){ + roleId, _ :=c.Get("role_id") + role:=models.FindRole(roleId) + if role.Method!="*"{ + methods:=strings.Split(role.Method,",") + for _,m:=range methods{ + if c.Request.Method!=m{ + c.JSON(200, gin.H{ + "code": 403, + "msg": "没有权限:"+c.Request.Method+","+c.Request.RequestURI, + }) + c.Abort() + return + } + } + } + if role.Path!="*"{ + paths:=strings.Split(role.Path,",") + for _,p:=range paths{ + if c.Request.RequestURI!=p{ + c.JSON(200, gin.H{ + "code": 403, + "msg": "没有权限:"+c.Request.Method+","+c.Request.RequestURI, + }) + c.Abort() + } + } + } +} + diff --git a/tools/paniclog_unix.go b/tools/paniclog_unix.go new file mode 100644 index 0000000..ba58bde --- /dev/null +++ b/tools/paniclog_unix.go @@ -0,0 +1,19 @@ +// Log the panic under unix to the log file + +//+build unix + +package tools + +import ( + "log" + "os" + "syscall" +) + +// redirectStderr to the file passed in +func RedirectStderr(f *os.File) { + err := syscall.Dup2(int(f.Fd()), int(os.Stderr.Fd())) + if err != nil { + log.Printf("Failed to redirect stderr to file: %v", err) + } +} \ No newline at end of file diff --git a/tools/paniclog_windows.go b/tools/paniclog_windows.go new file mode 100644 index 0000000..59602f4 --- /dev/null +++ b/tools/paniclog_windows.go @@ -0,0 +1,41 @@ +// Log the panic under windows to the log file +// +// Code from minix, via +// +// http://play.golang.org/p/kLtct7lSUg + +//+build windows + +package tools + +import ( + "log" + "os" + "syscall" +) + +var ( + kernel32 = syscall.MustLoadDLL("kernel32.dll") + procSetStdHandle = kernel32.MustFindProc("SetStdHandle") +) + +func setStdHandle(stdhandle int32, handle syscall.Handle) error { + r0, _, e1 := syscall.Syscall(procSetStdHandle.Addr(), 2, uintptr(stdhandle), uintptr(handle), 0) + if r0 == 0 { + if e1 != 0 { + return error(e1) + } + return syscall.EINVAL + } + return nil +} + +// redirectStderr to the file passed in +func RedirectStderr(f *os.File) { + err := setStdHandle(syscall.STD_ERROR_HANDLE, syscall.Handle(f.Fd())) + if err != nil { + log.Printf("Failed to redirect stderr to file: %v", err) + } + // SetStdHandle does not affect prior references to stderr + os.Stderr = f +} \ No newline at end of file