Created
April 23, 2015 18:45
-
-
Save pagreczner/cdd12b2dcc8a725954ee to your computer and use it in GitHub Desktop.
Some files
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
/* | |
https://golang.org/pkg/io/ioutil/ | |
http://stackoverflow.com/questions/19253469/make-a-url-encoded-post-request-using-http-newrequest | |
https://github.com/gin-gonic/gin | |
http://stackoverflow.com/questions/3751429/reading-an-integer-from-standard-input-in-golang | |
*/ | |
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"os" | |
) | |
func main() { | |
var s string | |
for true { | |
fmt.Print("Enter a command> ") | |
_, err := fmt.Scanf("%s", &s) | |
if s == "quit" { | |
fmt.Println("Quitting...") | |
os.Exit(0) | |
} | |
var msg string | |
if s == "api" { | |
msg, err = SendApiRequest("test") | |
fmt.Printf("Server Says: %s\r\n", msg) | |
} | |
// Error catch all | |
if err != nil { | |
fmt.Println(err.Error()) | |
fmt.Println("UH OH!") | |
os.Exit(1) | |
} | |
fmt.Printf("You inputted: %s \r\n", s) | |
} | |
} | |
func SendApiRequest(message string) (string, error) { | |
apiUrl := "http://localhost:8080/ping" | |
client := &http.Client{} | |
r, _ := http.NewRequest("GET", apiUrl, nil) | |
resp, err := client.Do(r) | |
if err != nil { | |
return "", err | |
} | |
fmt.Println(resp.Status) | |
msg, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
return "", err | |
} | |
return string(msg), nil | |
} |
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
/* | |
https://golang.org/pkg/io/ioutil/ | |
http://stackoverflow.com/questions/19253469/make-a-url-encoded-post-request-using-http-newrequest | |
https://github.com/gin-gonic/gin | |
http://stackoverflow.com/questions/3751429/reading-an-integer-from-standard-input-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 ( | |
"net/http" | |
"github.com/gin-gonic/gin" | |
) | |
func main() { | |
r := gin.Default() | |
r.GET("/ping", func(c *gin.Context) { | |
c.String(http.StatusOK, "pong") | |
}) | |
// Listen and serve on 0.0.0.0:8080 | |
r.Run(":8080") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment