| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "log" |
| 9 | "net/http" |
| 10 | "os" |
| 11 | ) |
| 12 | |
| 13 | type deployReq struct { |
| 14 | State any `json:"state"` |
| 15 | Config map[string]any `json:"config"` |
| 16 | } |
| 17 | |
| 18 | type req struct { |
| 19 | Id string `json:"id"` |
| 20 | SSHPrivateKey string `json:"sshPrivateKey"` |
| 21 | Config map[string]any `json:"config"` |
| 22 | } |
| 23 | |
| 24 | const publicKey = `ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIPK58vMu0MwIzdZT+mqpIBkhl48p9+/YwDCZv7MgTesF` |
| 25 | const privateKey = `-----BEGIN OPENSSH PRIVATE KEY----- |
| 26 | b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW |
| 27 | QyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQAAAKA42oIlONqC |
| 28 | JQAAAAtzc2gtZWQyNTUxOQAAACDyufLzLtDMCM3WU/pqqSAZIZePKffv2MAwmb+zIE3rBQ |
| 29 | AAAEC2CdpXtaFfqA8/mqjf9uITU1mrPOI4CeWgiQFEefFW1/K58vMu0MwIzdZT+mqpIBkh |
| 30 | l48p9+/YwDCZv7MgTesFAAAAGXJvb3RAY2FudmFzYnVpbGRlci1tYXN0ZXIBAgME |
| 31 | -----END OPENSSH PRIVATE KEY-----` |
| 32 | |
| 33 | func 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 | |
| 48 | func 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 | |
| 57 | func 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 | |
| 86 | type Network struct { |
| 87 | Name string `json:"name"` |
| 88 | Domain string `json:"domain"` |
| 89 | } |
| 90 | |
| 91 | type envResp struct { |
| 92 | DeployKey string `json:"deployKey"` |
| 93 | Networks []Network `json:"networks"` |
| 94 | } |
| 95 | |
| 96 | func 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 | |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 111 | func getPort() string { |
| 112 | if p, ok := os.LookupEnv("DODO_PORT_WEB"); ok { |
| 113 | return p |
| 114 | } |
| 115 | panic("port missing") |
| 116 | } |
| 117 | |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 118 | func main() { |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 119 | port := getPort() |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 120 | http.HandleFunc("GET /saved", handleSavedGet) |
| 121 | http.HandleFunc("POST /saved", handleSave) |
| 122 | http.HandleFunc("GET /env", handleEnv) |
| 123 | http.HandleFunc("POST /deploy", handleDeploy) |
| gio | fc441e3 | 2024-11-11 16:26:14 +0400 | [diff] [blame] | 124 | http.Handle("/", http.FileServer(http.Dir("./front"))) |
| 125 | log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil)) |
| gio | 5f2f100 | 2025-03-20 18:38:48 +0400 | [diff] [blame] | 126 | } |