Skip to content

Instantly share code, notes, and snippets.

@VimleshS
Created February 1, 2016 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VimleshS/7dc4085ca8d0d2773094 to your computer and use it in GitHub Desktop.
Save VimleshS/7dc4085ca8d0d2773094 to your computer and use it in GitHub Desktop.
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