-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsimple-synoindex-server.go
108 lines (76 loc) · 1.89 KB
/
simple-synoindex-server.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
"github.com/go-ini/ini"
)
var (
inifile string
cfg *ini.File
volumeMappings map[string]string
lastMTime time.Time
)
func init() {
// get current execute file path
execdir := GetCurrentExecDir()
inifile = fmt.Sprintf("%s/simple-synoindex-server.ini", execdir)
cfg, _ = ini.LooseLoad(inifile)
reloadMappings()
}
func reloadMappings() {
stat, err := os.Stat(inifile)
if err != nil {
log.Printf("reloadMappings Error: %s \n", err)
return
}
iniMTime := stat.ModTime()
if iniMTime.After(lastMTime) {
cfg.Reload()
volumeMappings = cfg.Section("mappings").KeysHash()
lastMTime = iniMTime
}
}
func remappingPath(srcPath string) string {
newPath := srcPath
for vPath, mPath := range volumeMappings {
newPath = strings.Replace(newPath, vPath, mPath, 1)
if newPath != srcPath {
return newPath
}
}
return newPath
}
func SynoIndex(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "ok\n")
req.ParseForm()
args := req.Form["args"]
// skip execute synoindex if only one argument
if len(args) == 1 {
log.Printf("Simple-SynoIndex NOT Support [%s] argument, but response OK to clients!\n", args[0])
return
}
// reload mapping settings if necessarily
reloadMappings()
args[1] = remappingPath(args[1])
// log to stdout
log.Printf("SynoIndex: %s %s \n", args[0], args[1])
// execute /usr/syno/bin/synoindex
cmd := exec.Command("/usr/syno/bin/synoindex", args...)
err := cmd.Run()
if err != nil {
log.Printf("SynoIndex Error: %s \n")
}
}
func main() {
srvIp := cfg.Section("main").Key("SERVER_IP").MustString("172.17.0.1")
srvPort := cfg.Section("main").Key("SERVER_PORT").MustString("32699")
srvListen := fmt.Sprintf("%s:%s", srvIp, srvPort)
http.HandleFunc("/synoindex", SynoIndex)
log.Fatal(http.ListenAndServe(srvListen, nil))
}