blob: 82db48118e4fbd28170cca7a3ac83cd3b5b27113 [file] [log] [blame]
gio5f2f1002025-03-20 18:38:48 +04001package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "io"
8 "log"
9 "net/http"
10 "os"
11)
12
13type deployReq struct {
14 State any `json:"state"`
15 Config map[string]any `json:"config"`
16}
17
18type req struct {
19 Id string `json:"id"`
20 SSHPrivateKey string `json:"sshPrivateKey"`
21 Config map[string]any `json:"config"`
22}
23
24const publicKey = `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPK58vMu0MwIzdZT+mqpIBkhl48p9+/YwDCZv7MgTesF`
25const privateKey = `-----BEGIN OPENSSH PRIVATE KEY-----
26b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW
27QyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQAAAKA42oIlONqC
28JQAAAAtzc2gtZWQyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQ
29AAAEC2CdpXtaFfqA8/mqjf9uITU1mrPOI4CeWgiQFEefFW1/K58vMu0MwIzdZT+mqpIBkh
30l48p9+/YwDCZv7MgTesFAAAAGXJvb3RAY2FudmFzYnVpbGRlci1tYXN0ZXIBAgME
31-----END OPENSSH PRIVATE KEY-----`
32
33func handleSave(w http.ResponseWriter, r *http.Request) {
34 var state map[string]any
35 if err := json.NewDecoder(r.Body).Decode(&state); err != nil {
36 panic(err)
37 }
38 out, err := os.Create("/tmp/state.json")
39 if err != nil {
40 panic(err)
41 }
42 defer out.Close()
43 if err := json.NewEncoder(out).Encode(state); err != nil {
44 panic(err)
45 }
46}
47
48func handleSavedGet(w http.ResponseWriter, r *http.Request) {
49 inp, err := os.Open("/tmp/state.json")
50 if err != nil {
51 panic(err)
52 }
53 defer inp.Close()
54 io.Copy(w, inp)
55}
56
57func handleDeploy(w http.ResponseWriter, r *http.Request) {
58 var dr deployReq
59 if err := json.NewDecoder(r.Body).Decode(&dr); err != nil {
60 http.Error(w, err.Error(), http.StatusBadRequest)
61 return
62 }
63 req := req{
64 Id: "test",
65 SSHPrivateKey: privateKey,
66 Config: dr.Config,
67 }
68 var buf bytes.Buffer
69 if err := json.NewEncoder(&buf).Encode(req); err != nil {
70 http.Error(w, err.Error(), http.StatusInternalServerError)
71 return
72 }
73 resp, err := http.Post("http://appmanager.hgrz-appmanager.svc.cluster.local/api/dodo-app", "application/json", &buf)
74 if err != nil {
75 http.Error(w, err.Error(), http.StatusInternalServerError)
76 return
77 }
78 var b bytes.Buffer
79 io.Copy(&b, resp.Body)
80 if resp.StatusCode != http.StatusOK {
81 http.Error(w, b.String(), http.StatusInternalServerError)
82 return
83 }
84}
85
86type Network struct {
87 Name string `json:"name"`
88 Domain string `json:"domain"`
89}
90
91type envResp struct {
92 DeployKey string `json:"deployKey"`
93 Networks []Network `json:"networks"`
94}
95
96func handleEnv(w http.ResponseWriter, r *http.Request) {
97 resp := envResp{
98 DeployKey: publicKey,
99 Networks: []Network{{
100 Name: "Public",
101 Domain: "v1.dodo.cloud",
102 }, {
103 Name: "Private",
104 Domain: "p.v1.dodo.cloud",
105 }},
106 }
107 fmt.Println(resp)
108 json.NewEncoder(w).Encode(resp)
109}
110
giofc441e32024-11-11 16:26:14 +0400111func getPort() string {
112 if p, ok := os.LookupEnv("DODO_PORT_WEB"); ok {
113 return p
114 }
115 panic("port missing")
116}
117
gio5f2f1002025-03-20 18:38:48 +0400118func main() {
giofc441e32024-11-11 16:26:14 +0400119 port := getPort()
gio5f2f1002025-03-20 18:38:48 +0400120 http.HandleFunc("GET /saved", handleSavedGet)
121 http.HandleFunc("POST /saved", handleSave)
122 http.HandleFunc("GET /env", handleEnv)
123 http.HandleFunc("POST /deploy", handleDeploy)
giofc441e32024-11-11 16:26:14 +0400124 http.Handle("/", http.FileServer(http.Dir("./front")))
125 log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
gio5f2f1002025-03-20 18:38:48 +0400126}