Created
February 1, 2016 12:22
-
-
Save VimleshS/7dc4085ca8d0d2773094 to your computer and use it in GitHub Desktop.
This file contains 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 ( | |
"github.com/gin-gonic/gin" | |
"io/ioutil" | |
"log" | |
"net/http" | |
) | |
type LoginJSON struct { | |
User string `json:"user" binding:"required"` | |
Password string `json:"password" binding:"required"` | |
} | |
func main() { | |
r := gin.Default() | |
r.POST("/login", func(c *gin.Context) { | |
var json LoginJSON | |
c.Bind(&json) | |
Logger(json.User) | |
if json.User == "username" && json.Password == "password" { | |
c.JSON(200, gin.H{"status": "You are logged in"}) | |
response, _ := http.Get("http://www.google.co.in/robots.txt") | |
body, _ := ioutil.ReadAll(response.Body) | |
c.JSON(200, gin.H{"Data": string(body)}) | |
} else { | |
c.JSON(401, gin.H{"status": "You are unauthorized"}) | |
} | |
}) | |
if err := r.Run(":8080"); err != nil { | |
Logger("Server Failed to Start port :8080") | |
} | |
} | |
func Logger(msg string) { | |
log.Printf("-> %s \n", msg) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment