func AccountHandlers(m *martini.ClassicMartini) { m.Use(BearerTokenValidation) m.Get("/account", func(r render.Render) { r.HTML(200, "account/login", nil) }) // Authenticate user m.Post("/account", binding.Bind(LoginUserModel{}), func(user LoginUserModel, r *http.Request, render render.Render) { log.Println("user", user.Username) log.Println("pass", user.Password) if user.Username == ValidUser && user.Password == ValidPass { // Create JWT token token := jwt.New(jwt.GetSigningMethod("HS256")) token.Claims["username"] = user.Username // Expire in 5 mins token.Claims["exp"] = time.Now().Add(time.Minute * 5).Unix() tokenString, err := token.SignedString([]byte(SecretKey)) if err == nil { data := map[string]string{"token": tokenString} render.JSON(201, data) return } r.Header.Add("Authorization", tokenString) } render.Redirect("/") }) }
func main() { githubAccessToken := os.Getenv("GITHUB_ACCESS_TOKEN") if githubAccessToken == "" { fmt.Println("Create new tokens via https://github.com/settings/applications 'Personal Access Tokens' section") log.Fatalln("Please set environment variable $GITHUB_ACCESS_TOKEN") } t := &oauth.Transport{ Token: &oauth.Token{AccessToken: githubAccessToken}, } client = github.NewClient(t.Client()) m := martini.Classic() m.Use(render.Renderer()) m.Get("/github/:org/:repo/pull/(?P<pull_id>\\d+).(?P<badge_type>(svg|png|json))", prBadgeHandler) m.Get("/github/:org/:repo/pull/:pull_id", prRedirectHandler) m.Get("/github/:org/:repo/issues/(?P<issue_id>\\d+).(?P<badge_type>(svg|png|json))", issueBadgeHandler) m.Get("/github/:org/:repo/issues/:issue_id", issueRedirectHandler) // Redirect to blog post for any other route (e.g. root route) until some human website implemented m.NotFound(func(render render.Render) { render.Redirect("https://blog.starkandwayne.com/2014/12/30/live-github-pr-status-in-your-blogs-docs/") }) m.Run() }