-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_manager.go
143 lines (116 loc) · 2.84 KB
/
server_manager.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os/exec"
)
type Adapter func(http.Handler) http.Handler
var PORT string
var MEDIA_FOLDER string
var DB string
func set_globals() {
PORT = ":17901"
MEDIA_FOLDER = "/mnt/data/"
DB = "data.db"
}
func start(mux http.Handler) {
fmt.Println("Staring on " + PORT)
// http.li
panic(http.ListenAndServe(":17901", mux))
}
func mountMedia() error {
log.Println("Mounting")
mount_all := exec.Command("mount", "-a")
if err := runCommand(mount_all); err != nil {
log.Printf("Error in mounting %q", err.Error())
return err
}
return nil
}
func main() {
log.Println("Starting")
set_globals()
err := initDatabase(DB)
if err != nil {
log.Printf("Error Initializing database %s\n", err.Error())
}
mux := register_routes()
start(mux)
}
// TODO: I think what I should do is have a do message adaptor that will call the
// do message with the correct arguments. I don't know why I wanted it to be an
// adapter though.
func renameMessageAdapter() Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// l.Println(r.Method, r.URL.Path)
h.ServeHTTP(w, r)
})
}
}
func runCommand(cmd *exec.Cmd) error {
var out_stream bytes.Buffer
var err_stream bytes.Buffer
cmd.Stdout = &out_stream
cmd.Stderr = &err_stream
err := cmd.Run()
if err != nil {
fmt.Printf("%q\n", err_stream.String())
fmt.Printf("%q\n", err.Error())
return err
}
fmt.Printf("STD out of %q: %q\n", cmd.Args, out_stream.String())
return nil
}
func getDirContents(dir string) ([]string, []string) {
log.Printf("Looking at %+v", dir)
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Fatal(err)
}
var file_names = make([]string, 0)
var dir_names = make([]string, 0)
for _, file := range files {
if file.IsDir() {
dir_names = append(dir_names, file.Name())
} else {
file_names = append(file_names, file.Name())
}
}
log.Printf("File names%+v", file_names)
return dir_names, file_names
}
func getDirFromRenameMessage(message Message) string {
renameDirMessage := makeRenameMessage(message)
// fmt.Printf("Dir message %+v\n", renameDirMessage)
return renameDirMessage.Dir
}
func makeRenameMessage(message Message) renameMessage {
var renameMessage renameMessage
err := json.Unmarshal(message.Args, &renameMessage)
if err != nil {
fmt.Printf("Something went wrong %+v\n", message)
fmt.Printf("Args %+s\n", message.Args)
}
return renameMessage
}
func makePlexMessage(message Message) PlexMessage {
var plexMessage PlexMessage
err := json.Unmarshal(message.Args, &plexMessage)
if err != nil {
fmt.Printf("Something went wrong %+v\n", message)
fmt.Printf("Args %+s\n", message.Args)
}
return plexMessage
}
func getReplaceMentMap() {
return
}
func cleanPlexNames() {
}
func renameTorrentDir() {
}