-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
131 lines (115 loc) · 3.95 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
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
package main
import (
"log"
"net/http"
"os"
"sshmanager/apis"
"sshmanager/libraries"
"strconv"
"github.com/gorilla/handlers"
"github.com/joho/godotenv"
)
// func executeCmd(cmd, hostname string, config *ssh.ClientConfig) string {
// conn, err := ssh.Dial("tcp", hostname+":22", config)
// if err != nil {
// log.Fatalf("dial failed:%v", err)
// }
// defer conn.Close()
// session, err := conn.NewSession()
// if err != nil {
// log.Fatalf("session failed:%v", err)
// }
// defer session.Close()
// var stdoutBuf bytes.Buffer
// session.Stdout = &stdoutBuf
// err = session.Run(cmd)
// if err != nil {
// log.Fatalf("Run failed:%v", err)
// }
// return stdoutBuf.String()
// }
func main() {
// Load .env file
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
// Load environment variables from .env
psHost := os.Getenv("POSTGRES_HOST")
psPort := os.Getenv("POSTGRES_PORT")
psUser := os.Getenv("POSTGRES_USER")
servicePort := os.Getenv("SERVICE_PORT")
psPassword := os.Getenv("POSTGRES_PASSWORD")
psDatabase := os.Getenv("POSTGRES_DATABASE")
privateKeyFile := os.Getenv("PRIVATE_KEY_PATH")
publicKeyFile := os.Getenv("PUBLIC_KEY_PATH")
// _ = os.Getenv("PUBLIC_KEY_PATH")
// Get postgres DB connection
psPortInt, err := strconv.Atoi(psPort)
db, err := libraries.GetPostgresClient(psHost, psPortInt, psUser, psPassword, psDatabase)
if err != nil {
panic(err)
}
mux := http.NewServeMux()
mux.Handle("/", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.HomePage(w, r, publicKeyFile)
})))
mux.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
mux.Handle("/addUser", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.AddUser(w, r, db)
})))
mux.Handle("/getUsers", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.GetUsers(w, r, db)
})))
mux.Handle("/deleteUser", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.DeleteUser(w, r, db, privateKeyFile)
})))
mux.Handle("/addServer", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.AddServer(w, r, db)
})))
mux.Handle("/getServers", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.GetServers(w, r, db)
})))
mux.Handle("/deleteServer", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.DeleteServer(w, r, db)
})))
mux.Handle("/toggleAccess", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.ToggleAccess(w, r, db, privateKeyFile)
})))
mux.Handle("/getAccess", handlers.LoggingHandler(os.Stdout, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
apis.GetAccess(w, r, db)
})))
log.Println("Server started at :" + servicePort)
log.Fatal(http.ListenAndServe("0.0.0.0:"+servicePort, handlers.RecoveryHandler()(mux)))
// cmd := "ls"
// hosts := []string{}
// results := make(chan string, 10)
// timeout := time.After(30 * time.Second)
// pemBytes, err := ioutil.ReadFile(os.Getenv("HOME") + privateKeyFile)
// if err != nil {
// log.Fatal(err)
// }
// signer, err := ssh.ParsePrivateKey(pemBytes)
// if err != nil {
// log.Fatalf("parse key failed:%v", err)
// }
// config := &ssh.ClientConfig{
// User: "ubuntu",
// Auth: []ssh.AuthMethod{ssh.PublicKeys(signer)},
// HostKeyCallback: ssh.InsecureIgnoreHostKey(),
// }
// for _, hostname := range hosts {
// go func(hostname string) {
// results <- executeCmd(cmd, hostname, config)
// }(hostname)
// }
// for i := 0; i < len(hosts); i++ {
// select {
// case res := <-results:
// fmt.Print(res)
// case <-timeout:
// fmt.Println("Timed out!")
// return
// }
// }
}