{"id":608,"date":"2022-02-09T00:18:45","date_gmt":"2022-02-08T21:18:45","guid":{"rendered":"http:\/\/atonko.ru\/?p=608"},"modified":"2022-03-18T17:47:15","modified_gmt":"2022-03-18T14:47:15","slug":"%d1%81%d0%b0%d0%b9%d1%82-%d0%bd%d0%b0-golang-%d0%b0%d0%b2%d1%82%d0%be%d1%80%d0%b8%d0%b7%d0%b0%d1%86%d0%b8%d1%8f-%d1%87%d0%b0%d1%81%d1%82%d1%8c-3","status":"publish","type":"post","link":"https:\/\/alextonkonogov.ru\/?p=608","title":{"rendered":"\u0421\u0430\u0439\u0442 \u043d\u0430 Golang. \u0410\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u044f. \u0427\u0430\u0441\u0442\u044c 3"},"content":{"rendered":"\n<p>\u042f \u0443\u0436\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u043f\u043e\u0439\u043b\u0435\u0440\u0438\u043b \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043d\u0435 \u043a\u043e\u0448\u043c\u0430\u0440\u0438\u043b\u043e \u0438\u0445 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0447\u0438\u0445\u0443.<\/p>\n\n\n\n<p>\u0412 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0443\u0431\u0438\u0440\u0430\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0439 flag (\u0432\u0441\u043f\u043e\u043c\u0438\u043d\u0430\u0435\u043c, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0431\u044b\u043b\u0430 \u0432\u043d\u0435\u0448\u043d\u044f\u044f \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u0430\u044f \u0438 \u043c\u044b \u043e\u0431\u0440\u0430\u0449\u0430\u043b\u0438\u0441\u044c \u043a \u043d\u0435\u0439 \u0438\u0437 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 Login, StartPage \u0438 authorized.). \u0412\u043c\u0435\u0441\u0442\u043e \u043d\u0435\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043c\u0430\u043f\u0443 (map \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f\u043c \u043f\u043e \u043a\u043b\u044e\u0447\u0443). <\/p>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0432 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0443 app \u043d\u043e\u0432\u044b\u0439 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440 cache \u0441 \u0442\u0438\u043f\u043e\u043c map[string]repository.User. \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043a\u043b\u044e\u0447\u0430, \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0442\u043e\u043a\u0435\u043d, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0439 \u043f\u0440\u0438 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438, \u0430 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0438\u0437 \u0431\u0430\u0437\u044b.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">type app struct {\n\tctx    context.Context\n\trepo   *repository.Repository\n\tcache  map[string]repository.User\n}<\/code><\/pre>\n\n\n\n<p>\u0418 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u043c\u0430\u043f\u0443 \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438-\u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0435:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">func NewApp(ctx context.Context, dbpool *pgxpool.Pool) *app {\n\treturn &amp;app{ctx, repository.NewRepository(dbpool), make(map[string]repository.User)}\n}<\/code><\/pre>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0434\u0443\u043c\u0430\u0435\u043c \u0432\u043e\u0442 \u043e \u0447\u0435\u043c. \u0415\u0441\u043b\u0438 \u043f\u0440\u0438 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0435\u0433\u043e \u0432 \u043f\u0430\u043c\u044f\u0442\u0438, \u0442\u043e \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430\u0448 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0443\u0437\u043d\u0430\u0435\u0442 \u043e \u043d\u0435\u043c \u0438 \u0441\u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u043d\u043e\u0433\u043e \u0432\u0445\u043e\u0434\u0430? \u041d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043a\u0430\u043a-\u0442\u043e \u0435\u0433\u043e  \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044e, \u0432\u0435\u0434\u044c \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0439\u0442\u0438 \u043f\u043e\u0442\u043e\u043c \u043f\u043e \u0442\u043e\u043a\u0435\u043d\u0443 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0443 \u043d\u0430\u0441, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u0440\u0430\u0432\u043d\u0438\u0442\u044c \u0442\u043e\u043a\u0435\u043d \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0441 \u0438\u043c\u0435\u044e\u0449\u0438\u043c\u0438\u0441\u044f. \u041f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043f\u0435\u0447\u0435\u043d\u044c\u043a\u0438? \u0417\u0434\u0435\u0441\u044c \u044f , \u043a\u043e\u043d\u0435\u0447\u043d\u043e,  \u0438\u043c\u0435\u044e \u0432 \u0432\u0438\u0434\u0443 cookies ))<\/p>\n\n\n\n<p>\u0412 \u0436\u0438\u0437\u043d\u0438 \u044d\u0442\u043e \u043f\u043e\u0445\u043e\u0436\u0435 \u043d\u0430 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u0435 \u043a\u043b\u0443\u0431\u0430 &#8211; \u043f\u0440\u0438 \u0432\u0445\u043e\u0434\u0435 \u0432\u044b \u043f\u043e\u043a\u0443\u043f\u0430\u0435\u0442\u0435 \u0431\u0438\u043b\u0435\u0442 \u0438 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u0435 \u0444\u0435\u0439\u0441-\u043a\u043e\u043d\u0442\u0440\u043e\u043b\u044c, \u043f\u043e\u0441\u043b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0432\u0430\u043c \u043d\u0430 \u0437\u0430\u043f\u044f\u0441\u0442\u044c\u0435 \u043d\u0430\u0434\u0435\u0432\u0430\u044e\u0442 \u0431\u0443\u043c\u0430\u0436\u043d\u044b\u0439 \u0431\u0440\u0430\u0441\u043b\u0435\u0442. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u0445\u043e\u0434\u0438\u0442\u044c \u0432\u043d\u0443\u0442\u0440\u0438, \u043f\u043e\u0441\u0435\u0449\u0430\u0442\u044c \u0443\u0431\u043e\u0440\u043d\u0443\u044e \u0438\u043b\u0438 \u0431\u0430\u0440, \u043d\u0435 \u0431\u0435\u0441\u043f\u043e\u043a\u043e\u044f\u0441\u044c, \u0447\u0442\u043e \u043e\u0445\u0440\u0430\u043d\u043d\u0438\u043a \u0432\u044b\u0441\u0442\u0430\u0432\u0438\u0442 \u0432\u0430\u0441 \u043d\u0430\u0440\u0443\u0436\u0443 \ud83d\ude42 <\/p>\n\n\n\n<p>\u0414\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 Login:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">func (a app) Login(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {\n\tlogin := r.FormValue(\"login\")\n\tpassword := r.FormValue(\"password\")\n\tif login == \"\" || password == \"\" {\n\t\ta.LoginPage(rw, \"\u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c!\")\n\t\treturn\n\t}\n\thash := md5.Sum([]byte(password))\n\thashedPass := hex.EncodeToString(hash[:])\n\tuser, err := a.repo.Login(a.ctx, login, hashedPass)\n\tif err != nil {\n\t\ta.LoginPage(rw, \"\u0412\u044b \u0432\u0432\u0435\u043b\u0438 \u043d\u0435\u0432\u0435\u0440\u043d\u044b\u0439 \u043b\u043e\u0433\u0438\u043d \u0438\u043b\u0438 \u043f\u0430\u0440\u043e\u043b\u044c!\")\n\t\treturn\n\t}\n\t\/\/\u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d, \u043f\u0438\u0448\u0435\u043c \u0435\u0433\u043e \u0432 \u043a\u0435\u0448 \u0438 \u0432 \u043a\u0443\u043a\u0438\n\ttime64 := time.Now().Unix()\n\ttimeInt := string(time64)\n\ttoken := login + password + timeInt\n\thashToken := md5.Sum([]byte(token))\n\thashedToken := hex.EncodeToString(hashToken[:])\n\ta.cache[hashedToken] = user\n\tlivingTime := 60 * time.Minute\n\texpiration := time.Now().Add(livingTime)\n\t\/\/\u043a\u0443\u043a\u0430 \u0431\u0443\u0434\u0435\u0442 \u0436\u0438\u0442\u044c 1 \u0447\u0430\u0441\n\tcookie := http.Cookie{Name: \"token\", Value: url.QueryEscape(hashedToken), Expires: expiration}\n\thttp.SetCookie(rw, &amp;cookie)\n\thttp.Redirect(rw, r, \"\/\", http.StatusSeeOther)\n}<\/code><\/pre>\n\n\n\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c\u0441\u044f, \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0437\u0434\u0435\u0441\u044c \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u043e\u0441\u044c. <\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>\u0412\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0443\u0441\u043f\u0435\u0448\u043d\u043e\u0439 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f.<\/li><li>\u0412\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u043c\u044b \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u0442\u043e\u043a\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0443 \u043d\u0430\u0441 \u0438\u0437 \u0445\u0435\u0448\u0430, \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0433\u043e \u0438\u0437 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u043d\u044b\u0445 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043b\u043e\u0433\u0438\u043d\u0430, \u043f\u0430\u0440\u043e\u043b\u044f \u0438 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438. <\/li><li>\u0412-\u0442\u0440\u0435\u0442\u044c\u0438\u0445, \u043c\u044b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u043c \u0442\u043e\u043a\u0435\u043d \u0438 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f \u0432 \u043a\u0435\u0448.<\/li><li>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0432-\u0447\u0435\u0442\u0432\u0435\u0440\u0442\u044b\u0445, \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043a\u0443\u043a\u0443 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0436\u0438\u0442\u044c \u043e\u0434\u0438\u043d \u0447\u0430\u0441.<\/li><\/ul>\n\n\n\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u043c\u0435\u0442\u043e\u0434 authorized \u0438 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432\u0441\u043f\u043e\u043c\u043e\u0433\u0430\u0442\u0435\u043b\u044c\u043d\u0443\u044e \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u0447\u0442\u0435\u043d\u0438\u044f cookies:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">func (a app) authorized(next httprouter.Handle) httprouter.Handle {\n\treturn func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {\n\t\ttoken, err := readCookie(\"token\", r)\n\t\tif err != nil {\n\t\t\thttp.Redirect(rw, r, \"\/login\", http.StatusSeeOther)\n\t\t\treturn\n\t\t}\n\t\tif _, ok := a.cache[token]; !ok {\n\t\t\thttp.Redirect(rw, r, \"\/login\", http.StatusSeeOther)\n\t\t\treturn\n\t\t}\n\t\tnext(rw, r, ps)\n\t}\n}\n\nfunc readCookie(name string, r *http.Request) (value string, err error) {\n\tif name == \"\" {\n\t\treturn value, errors.New(\"you are trying to read empty cookie\")\n\t}\n\tcookie, err := r.Cookie(name)\n\tif err != nil {\n\t\treturn value, err\n\t}\n\tstr := cookie.Value\n\tvalue, _ = url.QueryUnescape(str)\n\treturn value, err\n}<\/code><\/pre>\n\n\n\n<p>\u0417\u0434\u0435\u0441\u044c \u0443 \u043d\u0430\u0441 \u0432\u0441\u0435 \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e: \u0435\u0441\u043b\u0438 \u043d\u0435\u0442 \u043a\u0443\u043a\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c &#8211; \u0438\u0434\u0438\u0442\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f, \u0435\u0441\u043b\u0438 \u0442\u043e\u043a\u0435\u043d \u0435\u0441\u0442\u044c, \u043d\u043e \u0435\u0433\u043e \u043d\u0435\u0442 \u0432 \u043a\u0435\u0448\u0435 &#8211; \u0438\u0434\u0438\u0442\u0435 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c\u0441\u044f.<\/p>\n\n\n\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 go run main.go, \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0443\u0435\u043c\u0441\u044f \u0438 \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0438 \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0437\u043e\u043a \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0442 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438. \u0410 \u0432\u0441\u0435 \u043f\u043e\u0442\u043e\u043c\u0443, \u0447\u0442\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0431\u0443\u043c\u0430\u0436\u043d\u044b\u0439 \u0431\u0440\u0430\u0441\u043b\u0435\u0442 \u0432 \u0432\u0438\u0434\u0435 \u043a\u0443\u043a\u0438 \u0441 \u0442\u043e\u043a\u0435\u043d\u043e\u043c!<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"879\" height=\"1024\" src=\"https:\/\/atonko.ru\/wp-content\/uploads\/2022\/02\/image-8-879x1024.png\" alt=\"\" class=\"wp-image-609\" srcset=\"https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/image-8-879x1024.png 879w, https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/image-8-258x300.png 258w, https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/image-8-768x895.png 768w, https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/image-8.png 898w\" sizes=\"auto, (max-width: 879px) 100vw, 879px\" \/><\/figure>\n\n\n\n<p>\u041a \u0441\u043b\u043e\u0432\u0443 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0445\u043e\u0434 \u0438\u0437 \u0441\u0435\u0441\u0441\u0438\u0438 \u0438\u043b\u0438 logout \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u043e \u0447\u0435\u0440\u0435\u0437 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u0435 \u043a\u0443\u043a\u0438 \u0438 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u0440\u0435\u0434\u0438\u0440\u0435\u043a\u0442 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u0430\u0446\u0438\u0438:<\/p>\n\n\n\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u044d\u043d\u0434\u043f\u043e\u0438\u043d\u0442 &#8211; r.GET(&#8220;\/logout&#8221;, a.Logout):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">func (a app) Routes(r *httprouter.Router) {\n\tr.ServeFiles(\"\/public\/*filepath\", http.Dir(\"public\"))\n\tr.GET(\"\/\", a.authorized(a.StartPage))\n\tr.GET(\"\/login\", func(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {\n\t\ta.LoginPage(rw, \"\")\n\t})\n\tr.POST(\"\/login\", a.Login)\n\tr.GET(\"\/logout\", a.Logout)\n}<\/code><\/pre>\n\n\n\n<p>\u0418 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u043c \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043c\u0435\u0442\u043e\u0434-\u0445\u0435\u043d\u0434\u043b\u0435\u0440: <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"go\" class=\"language-go\">func (a app) Logout(rw http.ResponseWriter, r *http.Request, p httprouter.Params) {\n\tfor _, v := range r.Cookies() {\n\t\tc := http.Cookie{\n\t\t\tName:   v.Name,\n\t\t\tMaxAge: -1}\n\t\thttp.SetCookie(rw, &amp;c)\n\t}\n\thttp.Redirect(rw, r, \"\/login\", http.StatusSeeOther)\n}<\/code><\/pre>\n\n\n\n<p>\u0422\u043e \u0435\u0441\u0442\u044c \u0434\u043b\u044f \u0432\u044b\u0445\u043e\u0434\u0430, \u0432\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0435\u0440\u0435\u0439\u0442\u0438 \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 <a href=\"http:\/\/localhost:8080\/logout\">http:\/\/localhost:8080\/logout<\/a> <\/p>\n\n\n\n<p>\u0422\u0435\u043a\u0443\u0449\u0430\u044f \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"858\" height=\"662\" src=\"https:\/\/atonko.ru\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-09-at-00.12.07.png\" alt=\"\" class=\"wp-image-610\" srcset=\"https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-09-at-00.12.07.png 858w, https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-09-at-00.12.07-300x231.png 300w, https:\/\/alextonkonogov.ru\/wp-content\/uploads\/2022\/02\/Screenshot-2022-02-09-at-00.12.07-768x593.png 768w\" sizes=\"auto, (max-width: 858px) 100vw, 858px\" \/><\/figure>\n\n\n\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 pull request: <a href=\"https:\/\/github.com\/alextonkonogov\/atonko-authorization\/pull\/3\">https:\/\/github.com\/alextonkonogov\/atonko-authorization\/pull\/3<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u042f \u0443\u0436\u0435 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0441\u043f\u043e\u0439\u043b\u0435\u0440\u0438\u043b \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c \u043a\u0430\u043a \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u0448\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u043e\u043c\u0438\u043d\u0430\u043b\u043e \u0430\u0432\u0442\u043e\u0440\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439 \u0438 \u043d\u0435 \u043a\u043e\u0448\u043c\u0430\u0440\u0438\u043b\u043e \u0438\u0445 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f\u043c\u0438 \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043d \u0438 \u043f\u0430\u0440\u043e\u043b\u044c \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0447\u0438\u0445\u0443. \u0412 \u043f\u0435\u0440\u0432\u0443\u044e&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-608","post","type-post","status-publish","format-standard","hentry","category-golang"],"_links":{"self":[{"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=\/wp\/v2\/posts\/608","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=608"}],"version-history":[{"count":1,"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=\/wp\/v2\/posts\/608\/revisions"}],"predecessor-version":[{"id":757,"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=\/wp\/v2\/posts\/608\/revisions\/757"}],"wp:attachment":[{"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=608"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=608"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alextonkonogov.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=608"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}