summaryrefslogtreecommitdiff
path: root/src/ninjacloud.go
blob: 2d3c2f0b2238b4f8f4b60f70da14c1c4996906fe (plain)
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
144
145
package main

import (
	"flag"
	"log"
	"net/http"
	"path"
	//"io/ioutil"
	//"os"
)

const APP_NAME = "Ninja Go Local Cloud"
const APP_VERSION = "0.1 Draft"

var versionFlag bool
var interfaceFlag string
var portFlag string
var rootFlag string

const filePath = "/file/"
const dirPath = "/directory/"
const webPath = "/web?url="
const statusPath = "/cloudstatus"

const filePathLen = len(filePath)
const dirPathLen = len(dirPath)
const webPathLen = len(webPath)
const statusPathLen = len(statusPath)

//////// FILESYSTEM

//// Files

func writeFile() {
}

func readFile() {
}

func removeFile() {
}

func copyFile() {
}

//// Dirs

func createDir() {
}

func removeDir() {
}

func listDir() {
}

func copyDir() {
}

//////// REQUEST HANDLERS

//// File APIs

func fileHandler(w http.ResponseWriter, r *http.Request) {
	p := r.URL.Path[filePathLen:]
	path.Clean(p)

	switch r.Method {
	case "POST":
		// Create a new file
	case "PUT":
		if r.Header.Get("sourceURI") == "" {
			// Update an existing file (save over existing file)
		} else {
			// Copy, Move of an existing file 
		}
	case "DELETE":
		// Delete an existing file
	case "GET":
		// Read an existing file
	}

}

//// Directory APIs

func dirHandler(w http.ResponseWriter, r *http.Request) {
	p := r.URL.Path[dirPathLen:]
	path.Clean(p)

	switch r.Method {
	case "POST":
		// Create a new directory
	case "DELETE":
		// Delete an existing directory
	case "GET":
		// List the contents of an existing directory
	case "PUT":
		// Copy, Move of an existing directory
	}

}

//// Web API

// Get text or binary data from a URL
func getDataHandler(w http.ResponseWriter, r *http.Request) {
}

//// Cloud Status API

// Get the cloud status JSON
func getStatusHandler(w http.ResponseWriter, r *http.Request) {
}

//////// INIT and MAIN

func init() {
	flag.BoolVar(&versionFlag, "v", false, "Print the version number.")
	flag.StringVar(&interfaceFlag, "i", "localhost", "Listening interface.")
	flag.StringVar(&portFlag, "p", "58080", "Listening port.")
	flag.StringVar(&rootFlag, "r", ".", "Root directory.")
}

func main() {
	flag.Parse()

	if versionFlag {
		log.Println("Version:", APP_VERSION)
		return
	}

	log.Println("Starting " + APP_NAME + " " + APP_VERSION + " on " + interfaceFlag + ":" + portFlag + " in " + rootFlag)

	http.HandleFunc(filePath, fileHandler)
	http.HandleFunc(dirPath, dirHandler)
	http.HandleFunc(webPath, getDataHandler)
	http.HandleFunc(statusPath, getStatusHandler)

	err := http.ListenAndServe(interfaceFlag+":"+portFlag, nil)
	if err != nil {
		log.Println(err)
		return
	}
}