| 108 | } |
| 109 | |
| 110 | func startHTTPServer(inputChan chan string) { |
| 111 | handleSubmit := func(w http.ResponseWriter, r *http.Request) { |
| 112 | code, err := io.ReadAll(r.Body) |
| 113 | if err != nil { |
| 114 | return |
| 115 | } |
| 116 | inputChan <- string(code) |
| 117 | // Clear current line |
| 118 | fmt.Print("\n\033[1A\033[K") |
| 119 | } |
| 120 | |
| 121 | handleHealth := func(w http.ResponseWriter, r *http.Request) { |
| 122 | // 200 OK |
| 123 | } |
| 124 | |
| 125 | handleRedirect := func(w http.ResponseWriter, r *http.Request) { |
| 126 | loginURL := viper.GetString("frontend_url") + "/cli/login" |
| 127 | http.Redirect(w, r, loginURL, http.StatusSeeOther) |
| 128 | } |
| 129 | |
| 130 | http.Handle("POST /submit", cors(http.HandlerFunc(handleSubmit))) |
| 131 | http.Handle("/health", cors(http.HandlerFunc(handleHealth))) |
| 132 | http.Handle("/{$}", cors(http.HandlerFunc(handleRedirect))) |
| 133 | |
| 134 | // if we fail, oh well. we fall back to entering the code |
| 135 | _ = http.ListenAndServe("localhost:9417", nil) |
| 136 | } |
| 137 | |
| 138 | func init() { |
| 139 | rootCmd.AddCommand(loginCmd) |