-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
80 lines (71 loc) · 2.15 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
package main
import (
"bytes"
"fmt"
"github.com/gorilla/mux"
"log"
"net/http"
"strings"
"time"
)
type funcType func() map[string]string
func main() {
r := mux.NewRouter()
r.HandleFunc("/mariadb", mariadbHandler)
r.HandleFunc("/postgres", postgresHandler)
r.HandleFunc("/redis", redisHandler)
r.HandleFunc("/solr", solrHandler)
r.HandleFunc("/mongo", mongoHandler)
r.HandleFunc("/opensearch", opensearchHandler)
r.HandleFunc("/storage", persistentStorageHandler)
r.HandleFunc("/mysql", mariadbHandler)
r.HandleFunc("/", handleReq)
http.Handle("/", r)
log.Fatal(http.ListenAndServe(":3000", handler(r)))
}
func handler(m *mux.Router) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
driver := strings.ReplaceAll(r.URL.Path, "/", "")
service := r.URL.Query().Get("service")
incompatibleError := fmt.Sprintf("%s is not a compatible driver with service: %s", driver, service)
defer func() {
if err := recover(); err != nil {
http.Error(w, incompatibleError, http.StatusInternalServerError)
}
}()
timeoutHandler := http.TimeoutHandler(m, 3*time.Second, incompatibleError)
timeoutHandler.ServeHTTP(w, r)
}
}
func handleReq(w http.ResponseWriter, r *http.Request) {
var funcToCall []funcType
for _, conFunc := range funcToCall {
fmt.Fprintf(w, dbConnectorPairs(conFunc(), ""))
}
}
func dbConnectorPairs(m map[string]string, connectorHost string) string {
b := new(bytes.Buffer)
for key, value := range m {
fmt.Fprintf(b, "\"%s=%s\"\n", key, value)
}
host := fmt.Sprintf(`"SERVICE_HOST=%s"`, connectorHost)
connectorOutput := host + "\n" + b.String()
return connectorOutput
}
func connectorKeyValues(values []string) string {
b := new(bytes.Buffer)
for _, value := range values {
if value != "" {
v := strings.SplitN(value, ":", 2)
fmt.Fprintf(b, "\"%s=%s\"\n", v[0], v[1])
}
}
return b.String()
}
func cleanRoute(basePath string) (string, string) {
cleanRoute := strings.ReplaceAll(basePath, "/", "")
localService := strings.ReplaceAll(cleanRoute, ".", "-")
replaceHyphen := strings.ReplaceAll(localService, "-", "_")
lagoonService := strings.ToUpper(replaceHyphen)
return localService, lagoonService
}