-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
147 lines (121 loc) · 2.93 KB
/
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
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
146
147
package main
import (
"log"
"net/http"
"os/exec"
"archive/zip"
"io/ioutil"
"os"
"strings"
erpc "github.com/Varunram/essentials/rpc"
utils "github.com/Varunram/essentials/utils"
)
func ZipWriter(orgName string) error {
baseFolder, err := os.Getwd()
if err != nil {
return err
}
// Get a Buffer to Write To
outFile, err := os.Create(baseFolder + "/temp.zip")
if err != nil {
return err
}
defer outFile.Close()
baseFolder += "/" + orgName + "/"
w := zip.NewWriter(outFile)
err = addFiles(w, baseFolder, "")
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return nil
}
func addFiles(w *zip.Writer, basePath, baseInZip string) error {
files, err := ioutil.ReadDir(basePath)
if err != nil {
return err
}
for _, file := range files {
if !file.IsDir() {
data, err := ioutil.ReadFile(basePath + file.Name())
if err != nil {
return err
}
// Add some files to the archive.
f, err := w.Create(baseInZip + file.Name())
if err != nil {
return err
}
_, err = f.Write(data)
if err != nil {
return err
}
} else {
newBase := basePath + file.Name() + "/"
addFiles(w, newBase, baseInZip+file.Name()+"/")
}
}
return nil
}
func triggerScript() {
http.HandleFunc("/setup", func(w http.ResponseWriter, r *http.Request) {
err := erpc.CheckPost(w, r)
if err != nil {
log.Println(err)
return
}
org := r.FormValue("org") // the name of the org that the user selects
platform := r.FormValue("platform") // the name of the platform that the user wants to use
emailentity := r.FormValue("emailentity") // entity to send emails as
abl := r.FormValue("abl") // add other blockchain handlers
invo := r.FormValue("invo") // investor options
recpo := r.FormValue("recpo") // recipient options
if org == "" {
org = "org"
}
if len(strings.Split(org, " ")) > 1 {
org = strings.Split(org, " ")[0] // avoid deadling with parsing multi word GitHub imports
}
if platform == "" {
platform = "plat"
}
if len(strings.Split(platform, " ")) > 1 {
platform = strings.Split(platform, " ")[0] // avoid deadling with parsing multi word GitHub imports
}
if emailentity == "" {
emailentity = "The Plat"
}
if abl == "" {
abl = "y"
}
if invo == "" {
invo = "y"
}
if recpo == "" {
recpo = "y"
}
_, err = exec.Command("./gen.sh", org, platform, invo, recpo, emailentity, abl).Output()
if err != nil {
erpc.ResponseHandler(w, erpc.StatusInternalServerError)
return
}
err = ZipWriter(org)
if err != nil {
erpc.ResponseHandler(w, erpc.StatusInternalServerError)
return
}
http.ServeFile(w, r, "temp.zip")
})
}
func StartServer(portx int) {
triggerScript()
port, err := utils.ToString(portx)
if err != nil {
log.Fatal("Port not string")
}
log.Println("Starting RPC Server on Port: ", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}