/
reb.go
93 lines (73 loc) · 2.09 KB
/
reb.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
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
)
import (
"github.com/ranjanprj/rebataurview/cmds"
)
var nwPath, repositoryPath, uploadedFilePath string
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/cmd", cmdHandler)
http.HandleFunc("/upload", uploadHandler)
http.Handle("/app/", http.FileServer(http.Dir(nwPath)))
http.ListenAndServe(":9999", nil)
}
func homeHandler(w http.ResponseWriter, r *http.Request) {
// fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
fmt.Fprintf(w, "This is home")
// executeCommand("initpg D:\\uploads\\cc.csv")
}
func cmdHandler(w http.ResponseWriter, r *http.Request) {
cmd := r.FormValue("cmd")
fmt.Fprintf(w, "%s", executeCommand(cmd))
}
func executeCommand(cmd string) []byte {
return cmds.SetAndExecuteCmd(cmd)
}
func uploadHandler(w http.ResponseWriter, r *http.Request) {
action := r.FormValue("action")
if action == "upload" {
var fileName string
r.ParseMultipartForm(32 << 20) // 32MB is the default used by FormFile
fhs := r.MultipartForm.File["file"]
for _, fh := range fhs {
fileName = fh.Filename
f, err := fh.Open()
if err == nil {
if file, err := ioutil.ReadAll(f); err == nil {
path := strings.Join([]string{repositoryPath, fh.Filename}, "")
ioutil.WriteFile(path, file, 0644)
// fmt.Fprintf(w, "Done")
fmt.Println("Writing to file done")
http.Redirect(w, r, "/app", http.StatusFound)
//fmt.Fprintf(w, "File uploaded to repository",http.StatusFound)
} else {
fmt.Fprintf(w, "ERROR IN : File uploaded to repository", http.StatusNotFound)
log.Fatal(err)
}
} else {
log.Fatal(err)
}
}
uploadedFilePath = strings.Join([]string{repositoryPath, fileName}, "")
fmt.Println("PG COPY**************8", uploadedFilePath)
cmds.LoadDataIntoPG(uploadedFilePath, true)
}
}
func init() {
fmt.Println("init")
config, err := cmds.GetConfig()
fmt.Println("getting config")
if err == nil {
nwPath = config.NW.NWPath
repositoryPath = config.Repository.Path
fmt.Println(repositoryPath)
} else {
log.Fatal("Error getting NW Path")
}
}