diff options
author | Pacien TRAN-GIRARD | 2012-09-08 14:02:56 +0200 |
---|---|---|
committer | Pacien | 2015-12-07 22:48:22 +0100 |
commit | c5a9744b87354f67e219ee0069180a94ed27c3ee (patch) | |
tree | 51327c79a2e5cfe2c74c0a4d704f18affdaa1a44 /src | |
parent | 13482ad673640b281dd2de46a66a60a54d280fc7 (diff) | |
download | ninja-go-local-cloud-c5a9744b87354f67e219ee0069180a94ed27c3ee.tar.gz |
First draft: program structure.
Diffstat (limited to 'src')
-rw-r--r-- | src/ninjacloud.go | 145 |
1 files changed, 145 insertions, 0 deletions
diff --git a/src/ninjacloud.go b/src/ninjacloud.go new file mode 100644 index 0000000..2d3c2f0 --- /dev/null +++ b/src/ninjacloud.go | |||
@@ -0,0 +1,145 @@ | |||
1 | package main | ||
2 | |||
3 | import ( | ||
4 | "flag" | ||
5 | "log" | ||
6 | "net/http" | ||
7 | "path" | ||
8 | //"io/ioutil" | ||
9 | //"os" | ||
10 | ) | ||
11 | |||
12 | const APP_NAME = "Ninja Go Local Cloud" | ||
13 | const APP_VERSION = "0.1 Draft" | ||
14 | |||
15 | var versionFlag bool | ||
16 | var interfaceFlag string | ||
17 | var portFlag string | ||
18 | var rootFlag string | ||
19 | |||
20 | const filePath = "/file/" | ||
21 | const dirPath = "/directory/" | ||
22 | const webPath = "/web?url=" | ||
23 | const statusPath = "/cloudstatus" | ||
24 | |||
25 | const filePathLen = len(filePath) | ||
26 | const dirPathLen = len(dirPath) | ||
27 | const webPathLen = len(webPath) | ||
28 | const statusPathLen = len(statusPath) | ||
29 | |||
30 | //////// FILESYSTEM | ||
31 | |||
32 | //// Files | ||
33 | |||
34 | func writeFile() { | ||
35 | } | ||
36 | |||
37 | func readFile() { | ||
38 | } | ||
39 | |||
40 | func removeFile() { | ||
41 | } | ||
42 | |||
43 | func copyFile() { | ||
44 | } | ||
45 | |||
46 | //// Dirs | ||
47 | |||
48 | func createDir() { | ||
49 | } | ||
50 | |||
51 | func removeDir() { | ||
52 | } | ||
53 | |||
54 | func listDir() { | ||
55 | } | ||
56 | |||
57 | func copyDir() { | ||
58 | } | ||
59 | |||
60 | //////// REQUEST HANDLERS | ||
61 | |||
62 | //// File APIs | ||
63 | |||
64 | func fileHandler(w http.ResponseWriter, r *http.Request) { | ||
65 | p := r.URL.Path[filePathLen:] | ||
66 | path.Clean(p) | ||
67 | |||
68 | switch r.Method { | ||
69 | case "POST": | ||
70 | // Create a new file | ||
71 | case "PUT": | ||
72 | if r.Header.Get("sourceURI") == "" { | ||
73 | // Update an existing file (save over existing file) | ||
74 | } else { | ||
75 | // Copy, Move of an existing file | ||
76 | } | ||
77 | case "DELETE": | ||
78 | // Delete an existing file | ||
79 | case "GET": | ||
80 | // Read an existing file | ||
81 | } | ||
82 | |||
83 | } | ||
84 | |||
85 | //// Directory APIs | ||
86 | |||
87 | func dirHandler(w http.ResponseWriter, r *http.Request) { | ||
88 | p := r.URL.Path[dirPathLen:] | ||
89 | path.Clean(p) | ||
90 | |||
91 | switch r.Method { | ||
92 | case "POST": | ||
93 | // Create a new directory | ||
94 | case "DELETE": | ||
95 | // Delete an existing directory | ||
96 | case "GET": | ||
97 | // List the contents of an existing directory | ||
98 | case "PUT": | ||
99 | // Copy, Move of an existing directory | ||
100 | } | ||
101 | |||
102 | } | ||
103 | |||
104 | //// Web API | ||
105 | |||
106 | // Get text or binary data from a URL | ||
107 | func getDataHandler(w http.ResponseWriter, r *http.Request) { | ||
108 | } | ||
109 | |||
110 | //// Cloud Status API | ||
111 | |||
112 | // Get the cloud status JSON | ||
113 | func getStatusHandler(w http.ResponseWriter, r *http.Request) { | ||
114 | } | ||
115 | |||
116 | //////// INIT and MAIN | ||
117 | |||
118 | func init() { | ||
119 | flag.BoolVar(&versionFlag, "v", false, "Print the version number.") | ||
120 | flag.StringVar(&interfaceFlag, "i", "localhost", "Listening interface.") | ||
121 | flag.StringVar(&portFlag, "p", "58080", "Listening port.") | ||
122 | flag.StringVar(&rootFlag, "r", ".", "Root directory.") | ||
123 | } | ||
124 | |||
125 | func main() { | ||
126 | flag.Parse() | ||
127 | |||
128 | if versionFlag { | ||
129 | log.Println("Version:", APP_VERSION) | ||
130 | return | ||
131 | } | ||
132 | |||
133 | log.Println("Starting " + APP_NAME + " " + APP_VERSION + " on " + interfaceFlag + ":" + portFlag + " in " + rootFlag) | ||
134 | |||
135 | http.HandleFunc(filePath, fileHandler) | ||
136 | http.HandleFunc(dirPath, dirHandler) | ||
137 | http.HandleFunc(webPath, getDataHandler) | ||
138 | http.HandleFunc(statusPath, getStatusHandler) | ||
139 | |||
140 | err := http.ListenAndServe(interfaceFlag+":"+portFlag, nil) | ||
141 | if err != nil { | ||
142 | log.Println(err) | ||
143 | return | ||
144 | } | ||
145 | } | ||