Skip to content

Commit

Permalink
refactor: fixed merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
elraphty committed Nov 20, 2024
1 parent 6e65a37 commit 9c7beb9
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
94 changes: 94 additions & 0 deletions handlers/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ type FeatureBriefRequest struct {
FeatureUUID string `json:"featureUUID"`
} `json:"output"`
}
type AudioBriefPostData struct {
AudioLink string `json:"audioLink"`
FeatureUUID string `json:"featureUUID"`
Source string `json:"source"`
Examples []string `json:"examples"`
}

type featureHandler struct {
db db.Database
Expand Down Expand Up @@ -603,3 +609,91 @@ func (oh *featureHandler) StoriesSend(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}

func (oh *featureHandler) BriefSend(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
pubKeyFromAuth, _ := ctx.Value(auth.ContextKey).(string)
if pubKeyFromAuth == "" {
fmt.Println("no pubkey from auth")
w.WriteHeader(http.StatusUnauthorized)
return
}

body, err := io.ReadAll(r.Body)
r.Body.Close()
if err != nil {
http.Error(w, "Failed to read requests body", http.StatusBadRequest)
return
}

var postData AudioBriefPostData
err = json.Unmarshal(body, &postData)
if err != nil {
fmt.Println("[BriefSend] JSON Unmarshal error:", err)
http.Error(w, "Invalid JSON format", http.StatusNotAcceptable)
return
}

host := os.Getenv("HOST")
if host == "" {
http.Error(w, "HOST environment variable not set", http.StatusInternalServerError)
return
}

completePostData := struct {
AudioBriefPostData
WebhookURL string `json:"webhook_url"`
}{
AudioBriefPostData: postData,
WebhookURL: fmt.Sprintf("%s/feature/brief", host),
}

apiKey := os.Getenv("SWWFKEY")
if apiKey == "" {
http.Error(w, "API key not set in environment", http.StatusInternalServerError)
return
}

stakworkPayload := map[string]interface{}{
"name": "string",
"workflow_id": 36928,
"workflow_params": map[string]interface{}{
"set_var": map[string]interface{}{
"attributes": map[string]interface{}{
"vars": completePostData,
},
},
},
}

stakworkPayloadJSON, err := json.Marshal(stakworkPayload)
if err != nil {
http.Error(w, "Failed to encode payload", http.StatusInternalServerError)
return
}

req, err := http.NewRequest(http.MethodPost, "https://api.stakwork.com/api/v1/projects", bytes.NewBuffer(stakworkPayloadJSON))
if err != nil {
http.Error(w, "Failed to create request to Stakwork API", http.StatusInternalServerError)
return
}
req.Header.Set("Authorization", "Token token="+apiKey)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
http.Error(w, "Failed to send request to Stakwork API", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

respBody, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, "Failed to read response from Stakwork API", http.StatusInternalServerError)
return
}

w.WriteHeader(resp.StatusCode)
w.Write(respBody)
}
1 change: 1 addition & 0 deletions routes/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func FeatureRoutes() chi.Router {
r.Post("/", featureHandlers.CreateOrEditFeatures)
r.Post("/brief", featureHandlers.UpdateFeatureBrief)
r.Get("/{uuid}", featureHandlers.GetFeatureByUuid)
r.Post("/brief/send", featureHandlers.BriefSend)
// Old route for to getting features for workspace uuid
r.Get("/forworkspace/{workspace_uuid}", featureHandlers.GetFeaturesByWorkspaceUuid)
r.Get("/workspace/count/{uuid}", featureHandlers.GetWorkspaceFeaturesCount)
Expand Down

0 comments on commit 9c7beb9

Please sign in to comment.