{"id":469981,"date":"2025-08-07T15:01:30","date_gmt":"2025-08-07T15:01:30","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=469981"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=469981","title":{"rendered":"<span>\u041f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u0435 REST API \u043d\u0430 Go \u0441 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c Gorilla Mux \u0438 MongoDB<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9f2\/00a\/89f\/9f200a89f160fc6e8d572f26d016b230.png\" alt=\"go lang\" title=\"go lang\" width=\"1024\" height=\"1024\" sizes=\"auto, (max-width: 780px) 100vw, 50vw\" srcset=\"https:\/\/habrastorage.org\/r\/w780\/getpro\/habr\/upload_files\/9f2\/00a\/89f\/9f200a89f160fc6e8d572f26d016b230.png 780w,&#10;       https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/9f2\/00a\/89f\/9f200a89f160fc6e8d572f26d016b230.png 781w\" loading=\"lazy\" decode=\"async\"\/><\/p>\n<div><figcaption>go lang<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f <strong>MongoDB<\/strong> \u0441 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u043d\u0430 Go, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u043d\u0430 \u0431\u0430\u0437\u0435 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u0430 <strong>Gorilla Mux<\/strong>. \u0426\u0435\u043b\u044c \u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439, \u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 REST API \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 CRUD-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e <em>Book<\/em>, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u044f \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d \u043d\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0445 \u0441 Go, HTTP API \u0438 \u043e\u0441\u043d\u043e\u0432\u0430\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p><strong>\u0412\u044b\u0431\u043e\u0440 \u0441\u0442\u0435\u043a\u0430<\/strong><\/p>\n<p>Go \u2014 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u044f\u0437\u044b\u043a \u0441 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u043e\u043c, \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0438 \u0431\u043e\u0433\u0430\u0442\u043e\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e. \u042d\u0442\u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u0434\u0435\u043b\u0430\u044e\u0442 \u0435\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 API-\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432.<\/p>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 Gorilla Mux<\/h4>\n<p><a href=\"https:\/\/github.com\/gorilla\/mux\" rel=\"noopener noreferrer nofollow\">Gorilla Mux<\/a> \u2014 \u0437\u0440\u0435\u043b\u044b\u0439, \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 HTTP-\u0440\u043e\u0443\u0442\u0435\u0440 \u0434\u043b\u044f Go, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438 (<code>\/books\/{id}<\/code>);<\/p>\n<\/li>\n<li>\n<p>\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043f\u043e HTTP-\u043c\u0435\u0442\u043e\u0434\u0443;<\/p>\n<\/li>\n<li>\n<p>middleware.<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 MongoDB<\/h4>\n<p>MongoDB \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f NoSQL-\u0411\u0414, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0441\u043b\u0430\u0431\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0451 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0433\u0438\u0431\u043a\u0438\u0445 \u0441\u0445\u0435\u043c.<\/p>\n<h3>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430:<\/p>\n<pre><code class=\"cpp\">\/cmd     \/api         main.go \/internal     \/books         handler.go         model.go         repository.go \/pkg     \/db         mongo.go go.mod <\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 (<code>internal\/books<\/code>) \u043e\u0442 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 (<code>pkg\/db<\/code>).  <\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 MongoDB (Linux, apt)  <\/p>\n<pre><code class=\"bash\">sudo apt update sudo apt install -y mongodb<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0441\u0435\u0440\u0432\u0438\u0441 \u0437\u0430\u043f\u0443\u0449\u0435\u043d:  <\/p>\n<pre><code class=\"bash\">systemctl status mongodb<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a shell:<\/p>\n<pre><code class=\"bash\">  mongo <\/code><\/pre>\n<p>C\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 <code>booksdb<\/code> \u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e <code>books<\/code>:  <\/p>\n<pre><code class=\"go\">use booksdb db.books.insertOne({   title: \"A Song of Ice and Fire\",   authors: [\"George R.R. Martin\", \"Phyllis Eisenstein\"],   publish_date: ISODate(\"1996-08-01\"),   publisher: {     name: \"Bantam Books\",     country: \"US\"   } }) <\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/p>\n<pre><code class=\"bash\">go mod init github.com\/username\/mongo-gorilla-api go get github.com\/gorilla\/mux go get go.mongodb.org\/mongo-driver\/mongo go get go.mongodb.org\/mongo-driver\/mongo\/options <\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435:<\/strong> \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0435\u0433\u043e <code>mgo.v2<\/code>, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 mongo-driver.<\/p>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MongoDB    <\/p>\n<p><code>\/pkg\/db\/mongo.go<\/code>:  <\/p>\n<pre><code class=\"go\">package db  import (     \"context\"     \"log\"     \"time\"      \"go.mongodb.org\/mongo-driver\/mongo\"     \"go.mongodb.org\/mongo-driver\/mongo\/options\" )  func Connect(uri string) *mongo.Database {     ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)     defer cancel()      client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))     if err != nil {         log.Fatalf(\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MongoDB: %v\", err)     }      if err := client.Ping(ctx, nil); err != nil {         log.Fatalf(\"MongoDB \u043d\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442: %v\", err)     }      log.Println(\"\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 MongoDB \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e\")     return client.Database(\"booksdb\") } <\/code><\/pre>\n<h3>\u041c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p><code>\/internal\/books\/model.go<\/code>:<\/p>\n<pre><code class=\"go\">package books  import \"go.mongodb.org\/mongo-driver\/bson\/primitive\"  type Book struct {     ID          primitive.ObjectID `bson:\"_id,omitempty\" json:\"id\"`     Title       string             `bson:\"title\" json:\"title\"`     Authors     []string           `bson:\"authors\" json:\"authors\"`     PublishDate string             `bson:\"publish_date\" json:\"publish_date\"`     Publisher   Publisher          `bson:\"publisher\" json:\"publisher\"` }  type Publisher struct {     Name    string `bson:\"name\" json:\"name\"`     Country string `bson:\"country\" json:\"country\"` } <\/code><\/pre>\n<h3>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/h3>\n<p><code>\/internal\/books\/repository.go<\/code>:<\/p>\n<pre><code class=\"go\">package books  import (     \"context\"     \"time\"      \"go.mongodb.org\/mongo-driver\/bson\"     \"go.mongodb.org\/mongo-driver\/mongo\" )  type Repository struct {     col *mongo.Collection }  func NewRepository(db *mongo.Database) *Repository {     return &amp;Repository{col: db.Collection(\"books\")} }  func (r *Repository) FindAll(ctx context.Context) ([]Book, error) {     var books []Book     cur, err := r.col.Find(ctx, bson.D{})     if err != nil {         return nil, err     }     defer cur.Close(ctx)      for cur.Next(ctx) {         var book Book         if err := cur.Decode(&amp;book); err != nil {             return nil, err         }         books = append(books, book)     }     return books, nil }  func (r *Repository) Insert(ctx context.Context, book Book) (*mongo.InsertOneResult, error) {     ctx, cancel := context.WithTimeout(ctx, 5*time.Second)     defer cancel()     return r.col.InsertOne(ctx, book) } <\/code><\/pre>\n<h3>HTTP-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438<\/h3>\n<p><code>\/internal\/books\/handler.go<\/code>:<\/p>\n<pre><code class=\"go\">package books  import (     \"context\"     \"encoding\/json\"     \"net\/http\"     \"time\" )  type Handler struct {     repo *Repository }  func NewHandler(repo *Repository) *Handler {     return &amp;Handler{repo: repo} }  func (h *Handler) GetBooks(w http.ResponseWriter, r *http.Request) {     ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)     defer cancel()      books, err := h.repo.FindAll(ctx)     if err != nil {         http.Error(w, err.Error(), http.StatusInternalServerError)         return     }      w.Header().Set(\"Content-Type\", \"application\/json\")     json.NewEncoder(w).Encode(books) }  func (h *Handler) CreateBook(w http.ResponseWriter, r *http.Request) {     var book Book     if err := json.NewDecoder(r.Body).Decode(&amp;book); err != nil {         http.Error(w, err.Error(), http.StatusBadRequest)         return     }      ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)     defer cancel()      _, err := h.repo.Insert(ctx, book)     if err != nil {         http.Error(w, err.Error(), http.StatusInternalServerError)         return     }      w.WriteHeader(http.StatusCreated) } <\/code><\/pre>\n<h3>\u0422\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430<\/h3>\n<p><code>\/cmd\/api\/main.go<\/code>:<\/p>\n<pre><code class=\"go\">package main  import (     \"log\"     \"net\/http\"      \"github.com\/gorilla\/mux\"     \"github.com\/username\/mongo-gorilla-api\/internal\/books\"     \"github.com\/username\/mongo-gorilla-api\/pkg\/db\" )  func main() {     database := db.Connect(\"mongodb:\/\/localhost:27017\")      repo := books.NewRepository(database)     handler := books.NewHandler(repo)      r := mux.NewRouter()     r.HandleFunc(\"\/books\", handler.GetBooks).Methods(\"GET\")     r.HandleFunc(\"\/books\", handler.CreateBook).Methods(\"POST\")      log.Println(\"\u0421\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u043d\u0430 :8080\")     log.Fatal(http.ListenAndServe(\":8080\", r)) } <\/code><\/pre>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 API<\/h3>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043d\u0438\u0433\u0438:<\/p>\n<pre><code class=\"bash\">curl -X POST http:\/\/localhost:8080\/books \\      -H \"Content-Type: application\/json\" \\      -d '{\"title\":\"Go in Action\",\"authors\":[\"William Kennedy\"],\"publish_date\":\"2015-11-26\",\"publisher\":{\"name\":\"Manning\",\"country\":\"US\"}}' <\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430:  <\/p>\n<pre><code class=\"bash\">curl http:\/\/localhost:8080\/books <\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439, \u043d\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0439 REST API \u043d\u0430 Go, \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441 MongoDB. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e, \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b\u0430.<\/p>\n<p>\u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043c\u043e\u0436\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0412\u044b\u043d\u0435\u0441\u0442\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0432 <code>.env<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>viper<\/code> \u0438\u043b\u0438 <code>envconfig<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (<code>zerolog<\/code>, <code>zap<\/code>);<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c graceful shutdown;<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c Swagger\/OpenAPI \u0434\u043b\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/935102\/\"> https:\/\/habr.com\/ru\/articles\/935102\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<figure class=\"full-width\">\n<div><figcaption>go lang<\/figcaption><\/div>\n<\/figure>\n<p><strong>\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/strong><\/p>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0430\u044f \u0438\u043d\u0442\u0435\u0433\u0440\u0430\u0446\u0438\u044f <strong>MongoDB<\/strong> \u0441 \u0432\u0435\u0431-\u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c \u043d\u0430 Go, \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u043d\u0430 \u0431\u0430\u0437\u0435 \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0442\u043e\u0440\u0430 <strong>Gorilla Mux<\/strong>. \u0426\u0435\u043b\u044c \u2014 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439, \u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u044b\u0439 REST API \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 CRUD-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c\u044e <em>Book<\/em>, \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u043e\u0431\u043b\u044e\u0434\u0430\u044f \u043b\u0443\u0447\u0448\u0438\u0435 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u043e\u0434\u0430.<\/p>\n<p>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b \u0440\u0430\u0441\u0441\u0447\u0438\u0442\u0430\u043d \u043d\u0430 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u043e\u0432, \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0445 \u0441 Go, HTTP API \u0438 \u043e\u0441\u043d\u043e\u0432\u0430\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0431\u0430\u0437\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p><strong>\u0412\u044b\u0431\u043e\u0440 \u0441\u0442\u0435\u043a\u0430<\/strong><\/p>\n<p>Go \u2014 \u043a\u043e\u043c\u043f\u0438\u043b\u0438\u0440\u0443\u0435\u043c\u044b\u0439 \u044f\u0437\u044b\u043a \u0441 \u043b\u0430\u043a\u043e\u043d\u0438\u0447\u043d\u044b\u043c \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u043e\u043c, \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u043e\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430 \u0438 \u0431\u043e\u0433\u0430\u0442\u043e\u0439 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u043e\u0439 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0441\u0435\u0442\u044c\u044e. \u042d\u0442\u0438 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0430 \u0434\u0435\u043b\u0430\u044e\u0442 \u0435\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u044b\u043c \u0432\u044b\u0431\u043e\u0440\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u043a\u0438 API-\u0441\u0435\u0440\u0432\u0438\u0441\u043e\u0432.<\/p>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 Gorilla Mux<\/h4>\n<p><a href=\"https:\/\/github.com\/gorilla\/mux\" rel=\"noopener noreferrer nofollow\">Gorilla Mux<\/a> \u2014 \u0437\u0440\u0435\u043b\u044b\u0439, \u0448\u0438\u0440\u043e\u043a\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 HTTP-\u0440\u043e\u0443\u0442\u0435\u0440 \u0434\u043b\u044f Go, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442:<\/p>\n<ul>\n<li>\n<p>\u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0438\u0437\u0430\u0446\u0438\u044e \u0441 \u0448\u0430\u0431\u043b\u043e\u043d\u0430\u043c\u0438 (<code>\/books\/{id}<\/code>);<\/p>\n<\/li>\n<li>\n<p>\u0444\u0438\u043b\u044c\u0442\u0440\u0430\u0446\u0438\u044e \u043f\u043e HTTP-\u043c\u0435\u0442\u043e\u0434\u0443;<\/p>\n<\/li>\n<li>\n<p>middleware.<\/p>\n<\/li>\n<\/ul>\n<h4>\u041f\u043e\u0447\u0435\u043c\u0443 MongoDB<\/h4>\n<p>MongoDB \u2014 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043d\u043e-\u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f NoSQL-\u0411\u0414, \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043d\u0435\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0438\u043b\u0438 \u0441\u043b\u0430\u0431\u043e \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u0434\u0430\u043d\u043d\u044b\u0445, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u0435\u0451 \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0435\u0439 \u0434\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u043f\u0440\u043e\u0442\u043e\u0442\u0438\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u0433\u0438\u0431\u043a\u0438\u0445 \u0441\u0445\u0435\u043c.<\/p>\n<h3>\u0410\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0430<\/h3>\n<p>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u043a\u0430\u0442\u0430\u043b\u043e\u0433\u043e\u0432 \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430:<\/p>\n<pre><code class=\"cpp\">\/cmd     \/api         main.go \/internal     \/books         handler.go         model.go         repository.go \/pkg     \/db         mongo.go go.mod <\/code><\/pre>\n<p>\u0422\u0430\u043a\u043e\u0435 \u0440\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0438\u0437\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u043c\u0435\u043d\u043d\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 (<code>internal\/books<\/code>) \u043e\u0442 \u0438\u043d\u0444\u0440\u0430\u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043d\u044b\u0445 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432 (<code>pkg\/db<\/code>).  <\/p>\n<p>\u041f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f<\/p>\n<p>\u0423\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 MongoDB (Linux, apt)  <\/p>\n<pre><code class=\"bash\">sudo apt update sudo apt install -y mongodb<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u0443\u0431\u0435\u0434\u0438\u0442\u0435\u0441\u044c, \u0447\u0442\u043e \u0441\u0435\u0440\u0432\u0438\u0441 \u0437\u0430\u043f\u0443\u0449\u0435\u043d:  <\/p>\n<pre><code class=\"bash\">systemctl status mongodb<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a shell:<\/p>\n<pre><code class=\"bash\">  mongo <\/code><\/pre>\n<p>C\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445 <code>booksdb<\/code> \u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e <code>books<\/code>:  <\/p>\n<pre><code class=\"go\">use booksdb db.books.insertOne({   title: \"A Song of Ice and Fire\",   authors: [\"George R.R. Martin\", \"Phyllis Eisenstein\"],   publish_date: ISODate(\"1996-08-01\"),   publisher: {     name: \"Bantam Books\",     country: \"US\"   } }) <\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/p>\n<pre><code class=\"bash\">go mod init github.com\/username\/mongo-gorilla-api go get github.com\/gorilla\/mux go get go.mongodb.org\/mongo-driver\/mongo go get go.mongodb.org\/mongo-driver\/mongo\/options <\/code><\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0447\u0430\u043d\u0438\u0435:<\/strong> \u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0435\u0433\u043e <code>mgo.v2<\/code>, \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 mongo-driver.<\/p>\n<p>\u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MongoDB    <\/p>\n<p><code>\/pkg\/db\/mongo.go<\/code>:  <\/p>\n<pre><code class=\"go\">package db  import (     \"context\"     \"log\"     \"time\"      \"go.mongodb.org\/mongo-driver\/mongo\"     \"go.mongodb.org\/mongo-driver\/mongo\/options\" )  func Connect(uri string) *mongo.Database {     ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)     defer cancel()      client, err := mongo.Connect(ctx, options.Client().ApplyURI(uri))     if err != nil {         log.Fatalf(\"\u041e\u0448\u0438\u0431\u043a\u0430 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f \u043a MongoDB: %v\", err)     }      if err := client.Ping(ctx, nil); err != nil {         log.Fatalf(\"MongoDB \u043d\u0435 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442: %v\", err)     }      log.Println(\"\u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 MongoDB \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e\")     return client.Database(\"booksdb\") } <\/code><\/pre>\n<h3>\u041c\u043e\u0434\u0435\u043b\u044c \u0434\u0430\u043d\u043d\u044b\u0445<\/h3>\n<p><code>\/internal\/books\/model.go<\/code>:<\/p>\n<pre><code class=\"go\">package books  import \"go.mongodb.org\/mongo-driver\/bson\/primitive\"  type Book struct {     ID          primitive.ObjectID `bson:\"_id,omitempty\" json:\"id\"`     Title       string             `bson:\"title\" json:\"title\"`     Authors     []string           `bson:\"authors\" json:\"authors\"`     PublishDate string             `bson:\"publish_date\" json:\"publish_date\"`     Publisher   Publisher          `bson:\"publisher\" json:\"publisher\"` }  type Publisher struct {     Name    string `bson:\"name\" json:\"name\"`     Country string `bson:\"country\" json:\"country\"` } <\/code><\/pre>\n<h3>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439<\/h3>\n<p><code>\/internal\/books\/repository.go<\/code>:<\/p>\n<pre><code class=\"go\">package books  import (     \"context\"     \"time\"      \"go.mongodb.org\/mongo-driver\/bson\"     \"go.mongodb.org\/mongo-driver\/mongo\" )  type Repository struct {     col *mongo.Collection }  func NewRepository(db *mongo.Database) *Repository {     return &amp;Repository{col: db.Collection(\"books\")} }  func (r *Repository) FindAll(ctx context.Context) ([]Book, error) {     var books []Book     cur, err := r.col.Find(ctx, bson.D{})     if err != nil {         return nil, err     }     defer cur.Close(ctx)      for cur.Next(ctx) {         var book Book         if err := cur.Decode(&amp;book); err != nil {             return nil, err         }         books = append(books, book)     }     return books, nil }  func (r *Repository) Insert(ctx context.Context, book Book) (*mongo.InsertOneResult, error) {     ctx, cancel := context.WithTimeout(ctx, 5*time.Second)     defer cancel()     return r.col.InsertOne(ctx, book) } <\/code><\/pre>\n<h3>HTTP-\u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438<\/h3>\n<p><code>\/internal\/books\/handler.go<\/code>:<\/p>\n<pre><code class=\"go\">package books  import (     \"context\"     \"encoding\/json\"     \"net\/http\"     \"time\" )  type Handler struct {     repo *Repository }  func NewHandler(repo *Repository) *Handler {     return &amp;Handler{repo: repo} }  func (h *Handler) GetBooks(w http.ResponseWriter, r *http.Request) {     ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)     defer cancel()      books, err := h.repo.FindAll(ctx)     if err != nil {         http.Error(w, err.Error(), http.StatusInternalServerError)         return     }      w.Header().Set(\"Content-Type\", \"application\/json\")     json.NewEncoder(w).Encode(books) }  func (h *Handler) CreateBook(w http.ResponseWriter, r *http.Request) {     var book Book     if err := json.NewDecoder(r.Body).Decode(&amp;book); err != nil {         http.Error(w, err.Error(), http.StatusBadRequest)         return     }      ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)     defer cancel()      _, err := h.repo.Insert(ctx, book)     if err != nil {         http.Error(w, err.Error(), http.StatusInternalServerError)         return     }      w.WriteHeader(http.StatusCreated) } <\/code><\/pre>\n<h3>\u0422\u043e\u0447\u043a\u0430 \u0432\u0445\u043e\u0434\u0430<\/h3>\n<p><code>\/cmd\/api\/main.go<\/code>:<\/p>\n<pre><code class=\"go\">package main  import (     \"log\"     \"net\/http\"      \"github.com\/gorilla\/mux\"     \"github.com\/username\/mongo-gorilla-api\/internal\/books\"     \"github.com\/username\/mongo-gorilla-api\/pkg\/db\" )  func main() {     database := db.Connect(\"mongodb:\/\/localhost:27017\")      repo := books.NewRepository(database)     handler := books.NewHandler(repo)      r := mux.NewRouter()     r.HandleFunc(\"\/books\", handler.GetBooks).Methods(\"GET\")     r.HandleFunc(\"\/books\", handler.CreateBook).Methods(\"POST\")      log.Println(\"\u0421\u0435\u0440\u0432\u0435\u0440 \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u043d\u0430 :8080\")     log.Fatal(http.ListenAndServe(\":8080\", r)) } <\/code><\/pre>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 API<\/h3>\n<p>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043a\u043d\u0438\u0433\u0438:<\/p>\n<pre><code class=\"bash\">curl -X POST http:\/\/localhost:8080\/books \\      -H \"Content-Type: application\/json\" \\      -d '{\"title\":\"Go in Action\",\"authors\":[\"William Kennedy\"],\"publish_date\":\"2015-11-26\",\"publisher\":{\"name\":\"Manning\",\"country\":\"US\"}}' <\/code><\/pre>\n<p>\u041f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0441\u043f\u0438\u0441\u043a\u0430:  <\/p>\n<pre><code class=\"bash\">curl http:\/\/localhost:8080\/books <\/code><\/pre>\n<h3>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h3>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u0438\u0441\u0442\u0438\u0447\u043d\u044b\u0439, \u043d\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u043c\u044b\u0439 REST API \u043d\u0430 Go, \u0438\u043d\u0442\u0435\u0433\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441 MongoDB. \u0418\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0434\u0440\u0430\u0439\u0432\u0435\u0440\u0430 \u0438 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u0430 \u043a \u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043b\u0435\u0433\u043a\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u044f\u0442\u044c \u0441\u0435\u0440\u0432\u0438\u0441, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u044f \u0432\u0430\u043b\u0438\u0434\u0430\u0446\u0438\u044e, \u0430\u0443\u0442\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0446\u0438\u044e \u0438 \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u044b\u0435 \u0431\u0438\u0437\u043d\u0435\u0441-\u043f\u0440\u0430\u0432\u0438\u043b\u0430.<\/p>\n<p>\u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043c\u043e\u0436\u043d\u043e:<\/p>\n<ul>\n<li>\n<p>\u0412\u044b\u043d\u0435\u0441\u0442\u0438 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0432 <code>.env<\/code> \u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <code>viper<\/code> \u0438\u043b\u0438 <code>envconfig<\/code>;<\/p>\n<\/li>\n<li>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 (<code>zerolog<\/code>, <code>zap<\/code>);<\/p>\n<\/li>\n<li>\n<p>\u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c graceful shutdown;<\/p>\n<\/li>\n<li>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c Swagger\/OpenAPI \u0434\u043b\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438.<\/p>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/935102\/\"> https:\/\/habr.com\/ru\/articles\/935102\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-469981","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/469981","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=469981"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/469981\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=469981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=469981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=469981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}