-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
149 lines (133 loc) · 3.94 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"context"
"encoding/json"
"flag"
"github.com/gorilla/mux"
"github.com/richardbizik/db-schema-web-view/database/postgres"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"time"
)
const port = "9090"
var addr = flag.String("addr", ":"+port, "http service address")
var wait = flag.Duration("graceful-timeout", time.Second*15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
func serveHome(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL)
if r.URL.Path != "/" {
http.Error(w, "Not found", http.StatusNotFound)
return
}
if r.Method != "GET" {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
http.ServeFile(w, r, "_/webapp/dist/home.html")
}
func corsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusNoContent)
} else {
next.ServeHTTP(w, r)
}
})
}
func main() {
r := mux.NewRouter()
r.Use(corsMiddleware)
r.Path("/database").Methods("GET").HandlerFunc(getDatabases)
r.Path("/database/{name}/{schema}").Methods("GET").HandlerFunc(getSchema)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./_webapp/dist/")))
http.Handle("/", r)
flag.Parse()
srv := &http.Server{
Handler: r,
Addr: *addr,
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
// Run our server in a goroutine so that it doesn't block.
go func() {
log.Print("Server running at port: " + port)
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
}
}()
c := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(c, os.Interrupt)
// Block until we receive our signal.
<-c
// Create a deadline to wait for.
ctx, cancel := context.WithTimeout(context.Background(), *wait)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
srv.Shutdown(ctx)
// Optionally, you could run srv.Shutdown in a goroutine and block on
// <-ctx.Done() if your application should wait for other services
// to finalize based on context cancellation.
log.Println("shutting down")
os.Exit(0)
}
func getDatabases(w http.ResponseWriter, r *http.Request) {
file, err := ioutil.ReadFile("connections.yaml")
var config Config
err = yaml.Unmarshal(file, &config)
if err != nil {
log.Println("Failed to read schema")
}
var response []GetDatabasesResp
for _, database := range config.Databases {
response = append(response, GetDatabasesResp{
Name: database.Name,
Schema: database.Schema,
})
}
marshal, _ := json.Marshal(response)
w.Write(marshal)
}
func getSchema(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
dbName := vars["name"]
dbSchema := vars["schema"]
file, err := ioutil.ReadFile("connections.yaml")
var config Config
var foundConfig DatabaseConfiguration
err = yaml.Unmarshal(file, &config)
for i := range config.Databases {
if config.Databases[i].Name == dbName {
foundConfig = config.Databases[i]
break
}
}
schema, err := postgres.GetDBSchema(dbName, foundConfig.Host, foundConfig.Port, foundConfig.User, foundConfig.Password, dbSchema)
if err != nil {
log.Println("Failed to read schema")
}
marshal, err := json.Marshal(schema)
w.Write(marshal)
}
type GetDatabasesResp struct {
Name string `json:"name"`
Schema string `json:"schema"`
}
type Config struct {
Databases []DatabaseConfiguration `yaml:"databases"`
}
type DatabaseConfiguration struct {
Name string `yaml:"name"`
Schema string `yaml:"schema"`
User string `yaml:"user"`
Password string `yaml:"password"`
Host string `yaml:"host"`
Port string `yaml:"port"`
}