-
Notifications
You must be signed in to change notification settings - Fork 3
/
sail.go
127 lines (103 loc) · 3.33 KB
/
sail.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
package main
import (
directory "gopaddle/sail/directory"
trace "gopaddle/sail/trace"
util "gopaddle/sail/util"
json "gopaddle/sail/util/json"
clog "gopaddle/sail/util/log"
"log"
"net/http"
"os"
"runtime"
"time"
"github.com/gorilla/mux"
)
const (
config = "./config/profiles-%s.json"
)
func main() {
port := ":9000"
runtime.GOMAXPROCS(runtime.NumCPU())
args := os.Args[1:]
var env string
if len(args) < 1 {
log.Println("Sever Environment is not given,\n\t $ ./server <profile>")
os.Exit(0)
}
env = args[0]
log.Println("Selected Environment : ", env)
//Loading Config data to context
res := directory.LoadConfig(env)
if res == false {
log.Println("Config not loaded")
os.Exit(0)
}
clog.Init()
//To load configDirectory
go loadDirectoryConfig(env)
//db.Instance().Info()
router := mux.NewRouter()
router.Headers("Content-Type", "application/json", "X-Requested-With", "XMLHttpRequest")
/* Server Specific API */
router.HandleFunc("/api/status", GetStatus).Methods("GET")
router.HandleFunc("/api/version", GetVersion).Methods("GET")
/* Process List API */
router.HandleFunc("/api/{accountID}/v1/listProcesses", trace.GetList).Methods("GET")
/* Trace API */
router.HandleFunc("/api/{accountID}/v1/startTracing", trace.StartTracing).Methods("PUT")
/* File and Package list API */
router.HandleFunc("/api/{accountID}/v1/getfilepkg", trace.GetFilesPkg).Methods("GET")
/* Ports */
router.HandleFunc("/api/{accountID}/v1/getports", trace.GetPorts).Methods("GET")
/* Environment Variables */
router.HandleFunc("/api/{accountID}/v1/getenv", trace.GetEnvVariables).Methods("GET")
/* NFS Mount API */
router.HandleFunc("/api/{accountID}/v1/getNfsMounts", trace.NfsMounts).Methods("GET")
/* Default shell API */
router.HandleFunc("/api/{accountID}/v1/getshell", trace.GetShell).Methods("GET")
/* UID and GID API*/
router.HandleFunc("/api/{accountID}/v1/getuser", trace.GetUser).Methods("GET")
/* Start Command API */
router.HandleFunc("/api/{accountID}/v1/getstartcmd", trace.GetStartCmd).Methods("GET")
/* Docker Checks */
router.HandleFunc("/api/{accountID}/v1/dockercreate", trace.DockerCreate).Methods("PUT")
/* Docker Copy Files and Folders */
router.HandleFunc("/api/{accountID}/v1/dockercopy", trace.DockerCopy).Methods("PUT")
/* Create Final Image */
router.HandleFunc("/api/{accountID}/v1/finalimage", trace.FinalImageCreate).Methods("PUT")
log.Printf("Server listening at %s", port)
e := http.ListenAndServe(port, router)
if e != nil {
log.Fatalf("Error While establishing connection: %v", e)
}
}
// GetStatus to get status of service
func GetStatus(rw http.ResponseWriter, req *http.Request) {
jobj := json.New()
jobj.Put("status", "Running")
rw.WriteHeader(200)
rw.Write([]byte(jobj.ToString()))
}
// GetVersion to get version of service
func GetVersion(rw http.ResponseWriter, req *http.Request) {
config, err := util.LoadConfig(config, "")
jobj := json.New()
if err != nil {
jobj.Put("reason", err.Error())
rw.WriteHeader(404)
rw.Write([]byte(jobj.ToString()))
return
}
jobj.Put("version", config.GetString("version"))
rw.WriteHeader(200)
rw.Write([]byte(jobj.ToString()))
}
//It will periodically load configDirectory
func loadDirectoryConfig(env string) {
for {
directory.LoadDirectory(env)
//Wait for 30 secs between each call
timer := time.NewTimer(time.Second * 300)
<-timer.C
}
}