-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
293 lines (256 loc) · 7.87 KB
/
handler.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"database/sql"
"encoding/json"
"errors"
"fmt"
"github.com/go-chi/chi/v5"
"html/template"
"math/rand"
"mintlify-previewer-backend/log"
"net"
"net/http"
"net/http/httputil"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/oklog/ulid/v2"
)
func createDeploymentHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
return
}
var req Deployment
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
effectiveMintJSONPath, err := getValidDocsPath(req.DocsPath)
if err != nil {
http.Error(w, "Invalid docs path: "+err.Error(), http.StatusBadRequest)
return
}
req.DocsPath = effectiveMintJSONPath
dir, err := os.Getwd()
if err != nil {
http.Error(w, "Failed to get working directory", http.StatusInternalServerError)
return
}
newUUID := strings.ToLower(ulid.Make().String())
req.UUID = newUUID
deploymentDir := filepath.Join(dir, ".repos", req.UUID)
if err := os.MkdirAll(deploymentDir, 0755); err != nil {
http.Error(w, "Failed to create deployment directory", http.StatusInternalServerError)
return
}
_, repoURL := extractPRID(req.GitHubURL)
if err := checkRepoExists(repoURL, req.Branch); err != nil {
http.Error(w, fmt.Sprintf("Repository check failed: %v", err), http.StatusInternalServerError)
return
}
port := getUniquePort()
deployURL := fmt.Sprintf("http://localhost:%d", port)
reverseProxyURL := fmt.Sprintf("https://%s.%s", newUUID, r.Host)
_, err = db.Exec("INSERT INTO deployments (uuid, github_url, branch, docs_path, deployment_url, deployment_proxy_url, status) VALUES (?, ?, ?, ?, ?, ?, ?)",
newUUID, req.GitHubURL, req.Branch, req.DocsPath, deployURL, reverseProxyURL, "starting")
if err != nil {
log.Info("failed to create deployment:", err)
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
response := Deployment{UUID: newUUID, GitHubURL: req.GitHubURL, Branch: req.Branch, DeployURL: reverseProxyURL, Status: "starting"}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(response)
if err != nil {
log.Errorf("Failed to encode response: %v", err)
}
startProcessing(newUUID, repoURL, req, deploymentDir, port)
}
func startProcessing(newUUID string, repoURL string, req Deployment, deploymentDir string, port int) {
go func() {
if err := ensureMintlifyInstalled(); err != nil {
log.Infof("Failed to install Mintlify: %v", err)
_, _ = db.Exec("UPDATE deployments SET status = ?, error = ? WHERE uuid = ?", "failed", err.Error(), newUUID)
return
}
if _, err := cloneRepo(repoURL, req.Branch, deploymentDir); err != nil {
log.Errorln(err)
_, _ = db.Exec("UPDATE deployments SET status = ?, error = ? WHERE uuid = ?", "failed", err.Error(), newUUID)
return
}
mintFilePath := filepath.Join(deploymentDir, req.DocsPath)
if _, err := os.Stat(mintFilePath); os.IsNotExist(err) {
_, _ = db.Exec("UPDATE deployments SET status = ?, error = ? WHERE uuid = ?", "failed", "mint.json file not found", newUUID)
return
}
serverDir := filepath.Dir(mintFilePath)
startMintlifyDev(newUUID, port, serverDir)
}()
}
func getValidDocsPath(path string) (string, error) {
if path == "" {
return "", errors.New("docs_path cannot be empty")
}
if !strings.HasSuffix(path, ".json") {
return "", errors.New("docs_path must end with .json")
}
return path, nil
}
func getDeploymentHandler(w http.ResponseWriter, r *http.Request) {
uuidParam := chi.URLParam(r, "uuid")
var dep Deployment
err := db.QueryRow("SELECT uuid, github_url, branch, deployment_proxy_url as deployment_url, status FROM deployments WHERE uuid = ?",
uuidParam).Scan(&dep.UUID, &dep.GitHubURL, &dep.Branch, &dep.DeployURL, &dep.Status)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
http.Error(w, "Deployment not found", http.StatusNotFound)
return
}
log.Info("Failed to query deployment:", err)
http.Error(w, "Database error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(dep)
if err != nil {
log.Error("Failed to encode response:", err)
}
}
func getUniquePort() int {
for {
port := 5000 + rand.Intn(1000)
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err == nil {
err2 := ln.Close()
if err2 != nil {
log.Error("Failed to close listener:", err2)
}
return port
}
}
}
func extractPRID(githubURL string) (string, string) {
// Extract the PR ID and the repo URL
// Assuming URL is in format https://github.com/username/repository/pull/42
parts := strings.Split(githubURL, "/")
prID := parts[len(parts)-1]
// Reconstruct the repo URL (without the PR part)
repoURL := strings.Join(parts[:len(parts)-2], "/") + ".git"
return prID, repoURL
}
func deleteDeploymentHandler(w http.ResponseWriter, r *http.Request) {
uuid := chi.URLParam(r, "uuid")
err := stopMintlifyServer(uuid)
if err != nil {
if strings.Contains(err.Error(), "not found") {
http.Error(w, err.Error(), http.StatusNotFound)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
w.WriteHeader(http.StatusOK)
_, err = fmt.Fprintf(w, "Mintlify server for UUID %s stopped", uuid)
if err != nil {
log.Error("Failed to write response:", err)
}
}
func proxyOrShowStatus(w http.ResponseWriter, r *http.Request) {
host := r.Host
hostParts := strings.Split(host, ".")
if len(hostParts) < 2 {
http.Error(w, "Invalid hostname", http.StatusBadRequest)
return
}
uuid := strings.ToLower(hostParts[0]) // Extract UUID
var status string
var deploymentUrl string
err := db.QueryRow("SELECT status, deployment_url FROM deployments WHERE uuid = ?", uuid).Scan(&status, &deploymentUrl)
if err != nil {
http.Error(w, "Deployment not found", http.StatusNotFound)
return
}
// Handle proxying for running deployments
if status == "running" {
parsedUrl, err := url.Parse(deploymentUrl)
if err != nil {
http.Error(w, "Invalid deployment URL", http.StatusInternalServerError)
return
}
if parsedUrl.Scheme == "" {
parsedUrl.Scheme = "http"
}
log.Infof("Incoming request URL: %s", r.URL.String())
log.Infof("Incoming request URL (parsedPath): %s", parsedUrl)
proxy := httputil.NewSingleHostReverseProxy(parsedUrl)
proxy.ServeHTTP(w, r)
return
} else if status == "starting" {
http.ServeFile(w, r, "static/loading.html")
return
}
// Define the status page data for other states
var data struct {
Title string
Message string
Icon string
Color string
Refresh bool
}
switch status {
case "failed":
data = struct {
Title string
Message string
Icon string
Color string
Refresh bool
}{
Title: "Deployment Failed",
Message: "Something went wrong while starting the server. If this issue persists, please contact support.",
Icon: "⚠️",
Color: "#ef4444",
Refresh: false,
}
case "stopped":
data = struct {
Title string
Message string
Icon string
Color string
Refresh bool
}{
Title: "Deployment Stopped",
Message: "The documentation preview is currently unavailable.",
Icon: "🛑",
Color: "#6366f1",
Refresh: false,
}
default:
data = struct {
Title string
Message string
Icon string
Color string
Refresh bool
}{
Title: "Unknown Deployment State",
Message: "We're unable to determine the current state of your deployment. Please check back later or contact support if this persists.",
Icon: "❓",
Color: "#eab308",
Refresh: false,
}
}
// Load and render the template
tmpl, err := template.ParseFiles("static/status.html")
if err != nil {
http.Error(w, "Failed to load template", http.StatusInternalServerError)
return
}
err = tmpl.Execute(w, data)
if err != nil {
log.Error("Failed to load template:", err)
}
}