-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (63 loc) · 1.77 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
package main
import (
"github.com/drpytho/encrptCloud/encrpt"
"io/ioutil"
"log"
"net/http"
"strconv"
)
var KEY = [...]byte{4, 46, 53, 180, 151, 173, 194, 191, 100, 17, 107, 253, 230, 35, 19, 155, 161, 229, 146, 117, 11, 38, 221, 194, 234, 157, 204, 210, 26, 247, 37, 190}
var store = make(map[string]encrpt.StoreUnit)
var ramStore = encrpt.NewRamStore()
func Hello(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Write([]byte("Some text"))
}
func InitializeFS(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
vals := r.URL.Query()
size, _ := strconv.Atoi(vals.Get("size"))
str, err := ramStore.NewUnit(uint(size), KEY[:])
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
w.Write([]byte(*str))
}
func setBlockAt(w http.ResponseWriter, r *http.Request) {
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal("request", err)
}
vals := r.URL.Query()
unit := vals.Get("unit")
blockID, _ := strconv.Atoi(vals.Get("block"))
err = ramStore.GetUnit(unit).SetBlock(blockID, buf)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
return
}
w.Write([]byte("OK"))
}
func getBlockAt(w http.ResponseWriter, r *http.Request) {
vals := r.URL.Query()
unit := vals.Get("unit")
blockID, _ := strconv.Atoi(vals.Get("block"))
buf, _, err := ramStore.GetUnit(unit).GetBlock(blockID)
if err != nil {
w.WriteHeader(500)
w.Write([]byte(err.Error()))
}
w.Write(buf)
}
func main() {
http.HandleFunc("/hello", Hello)
http.HandleFunc("/init", InitializeFS)
http.HandleFunc("/get", getBlockAt)
http.HandleFunc("/set", setBlockAt)
err := http.ListenAndServeTLS(":4433", "server.crt", "server.key", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}