From 18cd0d051451025020c900b5afedfbdbfad25bea Mon Sep 17 00:00:00 2001 From: badkaktus Date: Sat, 12 Oct 2019 21:40:43 +0300 Subject: [PATCH] =?UTF-8?q?http=20=D1=81=D0=B5=D1=80=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples.txt | 2 +- examples/http-servers/http-servers.go | 43 ++++++++++++++------------- examples/http-servers/http-servers.sh | 4 +-- 3 files changed, 25 insertions(+), 24 deletions(-) diff --git a/examples.txt b/examples.txt index 933c76a..77f39bd 100644 --- a/examples.txt +++ b/examples.txt @@ -67,7 +67,7 @@ Epoch Подкоманды командной строки (Command-Line Subcommands) Переменные среды (Environment Variables) HTTP клиенты (HTTP Clients) -HTTP Servers +HTTP серверы (HTTP Servers) Spawning Processes Exec'ing Processes Signals diff --git a/examples/http-servers/http-servers.go b/examples/http-servers/http-servers.go index 42a83b3..22b739b 100644 --- a/examples/http-servers/http-servers.go +++ b/examples/http-servers/http-servers.go @@ -1,5 +1,5 @@ -// Writing a basic HTTP server is easy using the -// `net/http` package. +// Написание базового HTTP сервера очень легкореализуемо +// с пакетом `net/http`. package main import ( @@ -7,26 +7,27 @@ import ( "net/http" ) -// A fundamental concept in `net/http` servers is -// *handlers*. A handler is an object implementing the -// `http.Handler` interface. A common way to write -// a handler is by using the `http.HandlerFunc` adapter -// on functions with the appropriate signature. +// Фундаментальная концепция серверов `net/http` - это +// *обработчики*. Обработчик - это объект, реализующий +// интерфейс `http.Handler`. Распространенным способом +// написания обработчика является использование адаптера +// `http.HandlerFunc` для функций с соответствующей +// подписью. func hello(w http.ResponseWriter, req *http.Request) { - // Functions serving as handlers take a - // `http.ResponseWriter` and a `http.Request` as - // arguments. The response writer is used to fill in the - // HTTP response. Here our simple response is just + // Функции, выполняющие функции обработчиков, принимают + // в качестве аргументов `http.ResponseWriter` и + // `http.Request`. Response writer используется для + // наполнения HTTP-ответа. Здесь наш простой ответ // "hello\n". fmt.Fprintf(w, "hello\n") } func headers(w http.ResponseWriter, req *http.Request) { - // This handler does something a little more - // sophisticated by reading all the HTTP request - // headers and echoing them into the response body. + // Этот обработчик делает что-то более сложное, + // читая все заголовки HTTP-запроса и вставляя их в + // тело ответа. for name, headers := range req.Header { for _, h := range headers { fmt.Fprintf(w, "%v: %v\n", name, h) @@ -36,15 +37,15 @@ func headers(w http.ResponseWriter, req *http.Request) { func main() { - // We register our handlers on server routes using the - // `http.HandleFunc` convenience function. It sets up - // the *default router* in the `net/http` package and - // takes a function as an argument. + // Мы регистрируем наши обработчики на сервере, + // используя удобную функцию `http.HandleFunc`. Она + // устанавливает *маршрут по умолчанию* в пакете + // `net/http` и принимает функцию в качестве аргумента. http.HandleFunc("/hello", hello) http.HandleFunc("/headers", headers) - // Finally, we call the `ListenAndServe` with the port - // and a handler. `nil` tells it to use the default - // router we've just set up. + // Наконец, мы вызываем `ListenAndServe` с портом и + // обработчиком. nil говорит использовать только что + // настроенный маршрутизатор по умолчанию. http.ListenAndServe(":8090", nil) } diff --git a/examples/http-servers/http-servers.sh b/examples/http-servers/http-servers.sh index 2db5f45..130feff 100644 --- a/examples/http-servers/http-servers.sh +++ b/examples/http-servers/http-servers.sh @@ -1,6 +1,6 @@ -# Run the server in the background. +# Запускаем сервер в фоне. $ go run http-servers.go & -# Access the `/hello` route. +# Делаем запрос по адресу `/hello`. $ curl localhost:8090/hello hello