Created
February 17, 2022 09:35
-
-
Save akoserwal/67a9e370ac7ab363a6e2a5e88e7a34e1 to your computer and use it in GitHub Desktop.
ocm-server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"log" | |
"net/http" | |
"os/exec" | |
"strings" | |
) | |
func tokenHandler(w http.ResponseWriter, r *http.Request) { | |
if r.URL.Path != "/token" { | |
http.Error(w, "404 not found.", http.StatusNotFound) | |
return | |
} | |
if r.Method != "GET" { | |
http.Error(w, "Method is not supported.", http.StatusNotFound) | |
return | |
} | |
// Create the connection, and remember to close it: | |
_, err := exec.LookPath("ocm") | |
if err != nil { | |
log.Fatal("installing ocm is in your future") | |
} | |
cmd := exec.Command("ocm", "token") | |
cmd.Stdin = strings.NewReader("some input") | |
var out bytes.Buffer | |
cmd.Stdout = &out | |
err = cmd.Run() | |
if err != nil { | |
log.Fatal(err) | |
} | |
token := strings.TrimSuffix(out.String(), "\n") | |
data := map[string]string{ | |
"token": token, | |
} | |
w.Header().Set("Content-Type", "application/json") | |
w.WriteHeader(http.StatusCreated) | |
json.NewEncoder(w).Encode(data) | |
} | |
func main() { | |
http.HandleFunc("/token", tokenHandler) // Update this line of code | |
fmt.Printf("Starting server at port 8061\n") | |
if err := http.ListenAndServe(":8061", nil); err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment