func Route(m *martini.ClassicMartini) { m.Use(render.Renderer()) m.Get("/", func(r render.Render) { r.HTML(200, "index", nil) }) //post handlers Signup(m) Signin(m) m.Get("/inspect", func() string { return "Hello inspect!" }) m.Get("/add", func() string { return "Hello add!" }) m.Post("/inspect", func() string { return "Hello inspect!" }) m.Post("/add", func() string { return "Hello add!" }) }
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("/") }) }
/** * Martini application configuration. */ func configuration(app *martini.ClassicMartini) { app.Use(martini.Static("public")) app.Use(render.Renderer(render.Options{ Directory: "templets", Layout: "layout", Extensions: []string{".tmpl", ".html"}, Charset: "UTF-8", IndentJSON: true, })) }