Created
May 9, 2018 13:57
-
-
Save ujjkumsi/90c99658680c03a72d568202ce43825e to your computer and use it in GitHub Desktop.
Challenge Handler for Slack in Golang
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 ( | |
| "encoding/json" | |
| "log" | |
| "net/http" | |
| ) | |
| type Data struct { | |
| Category string `json:"type"` | |
| Token string `json:"token"` | |
| Challenge string `json:"challenge"` | |
| } | |
| type Challenge struct { | |
| Challenge string `json:"challenge"` | |
| } | |
| func challengeHandler(w http.ResponseWriter, r *http.Request) { | |
| challenge := &Data{} | |
| err := json.NewDecoder(r.Body).Decode(challenge) | |
| if err != nil { | |
| log.Printf("[ERROR] %s", err) | |
| panic(err) | |
| } | |
| log.Printf("[CHALLENGE] %s", challenge.Challenge) | |
| resp := &Challenge{} | |
| resp.Challenge = challenge.Challenge | |
| challengeJson, err := json.Marshal(resp) | |
| if err != nil { | |
| panic(err) | |
| } | |
| //set content-type to json | |
| w.Header().Set("Content-Type", "application/json") | |
| w.WriteHeader(http.StatusOK) | |
| w.Write(challengeJson) | |
| } |
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
| ....... | |
| http.HandleFunc("/", challengeHandler) | |
| ....... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment