-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
109 lines (91 loc) · 3.16 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
"html/template"
"os"
"path/filepath"
"github.com/GeertJohan/go.rice"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
"github.com/zeropage/mukgoorm/cmd"
"github.com/zeropage/mukgoorm/grant"
"github.com/zeropage/mukgoorm/handlers"
"github.com/zeropage/mukgoorm/image"
"github.com/zeropage/mukgoorm/path"
"github.com/zeropage/mukgoorm/setting"
)
// When starting server directory parameter is needed. Else error occurs.
// Run Command:
// go run main.go -D tmp/dat -A *PASSWORD* -R *PASSWORD*
func main() {
CheckStartOptions()
go resizeImages()
r := NewEngine()
// FIXME recieve hostname or bind address
r.Run("localhost:8080")
}
func CheckStartOptions() {
cmd.RootCmd.Execute()
if setting.GetPassword().AdminPwd == "" {
log.Panic("Admin password must required")
}
if setting.GetPassword().ROnlyPwd == "" {
log.Panic("ReadOnly password must required")
}
if dir := setting.GetDirectory(); dir.Path == "" || dir.Path == "." {
log.Panicf("You need to set directory: %s", dir.Path)
}
}
func NewEngine() *gin.Engine {
r := gin.Default()
r.SetHTMLTemplate(templates())
r.StaticFS("/static", rice.MustFindBox("static").HTTPBox())
r.GET("/login", handlers.LoginForm)
r.POST("/login", handlers.Login)
loginedRoute := r.Group("/", handlers.CheckLogin)
loginedRoute.GET("/", handlers.CheckRole(grant.ADMIN, grant.READ_ONLY), handlers.List)
loginedRoute.GET("/set-password", handlers.CheckRole(grant.ADMIN), handlers.SetPasswordForm)
loginedRoute.POST("/set-password", handlers.CheckRole(grant.ADMIN), handlers.SetPassword)
loginedRoute.GET("/list", handlers.CheckRole(grant.ADMIN, grant.READ_ONLY), handlers.List)
loginedRoute.GET("/down", handlers.CheckRole(grant.ADMIN, grant.READ_ONLY), handlers.Down)
loginedRoute.GET("/info", handlers.CheckRole(grant.ADMIN, grant.READ_ONLY), handlers.Info)
loginedRoute.POST("/upload", handlers.CheckRole(grant.ADMIN), handlers.Upload)
loginedRoute.DELETE("/delete", handlers.CheckRole(grant.ADMIN), handlers.Delete)
loginedRoute.GET("/search", handlers.CheckRole(grant.ADMIN, grant.READ_ONLY), handlers.Search)
loginedRoute.POST("/remote-download", handlers.CheckRole(grant.ADMIN), handlers.RemoteDownload)
loginedRoute.DELETE("/logout", handlers.CheckRole(grant.ADMIN, grant.READ_ONLY), handlers.Logout)
r.GET("/img/:name", handlers.Image)
return r
}
func templates() *template.Template {
all := template.New("__main__").Funcs(template.FuncMap{})
templateBox := rice.MustFindBox("templates")
templateBox.Walk("/", func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
if info.Name()[0] == '.' {
return nil
}
slashedPath := filepath.ToSlash(path)
template.Must(all.New(slashedPath).Parse(templateBox.MustString(path)))
return nil
})
str := fmt.Sprintf("Loaded HTML Templates (%d):\n", len(all.Templates()))
for _, v := range all.Templates() {
str += fmt.Sprintf("\t - %s\n", v.Name())
}
fmt.Println(str)
return all
}
func resizeImages() {
image.MakeImageDir()
root := setting.GetDirectory().Path
files, _ := path.PathInfoWithDirFrom(root)
for _, f := range *files {
if f.File.IsDir() {
continue
}
image.Resize(f.Path, 300)
}
}