Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

93 рядки
2.6 KiB

  1. // Copyright 2016 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package logadmin_test
  15. import (
  16. "bytes"
  17. "context"
  18. "flag"
  19. "fmt"
  20. "html/template"
  21. "log"
  22. "net/http"
  23. "cloud.google.com/go/logging"
  24. "cloud.google.com/go/logging/logadmin"
  25. "google.golang.org/api/iterator"
  26. )
  27. var (
  28. client *logadmin.Client
  29. projectID = flag.String("project-id", "", "ID of the project to use")
  30. )
  31. func ExampleClient_Entries_pagination() {
  32. // This example demonstrates how to iterate through items a page at a time
  33. // even if each successive page is fetched by a different process. It is a
  34. // complete web server that displays pages of log entries. To run it as a
  35. // standalone program, rename both the package and this function to "main".
  36. ctx := context.Background()
  37. flag.Parse()
  38. if *projectID == "" {
  39. log.Fatal("-project-id missing")
  40. }
  41. var err error
  42. client, err = logadmin.NewClient(ctx, *projectID)
  43. if err != nil {
  44. log.Fatalf("creating logging client: %v", err)
  45. }
  46. http.HandleFunc("/entries", handleEntries)
  47. log.Print("listening on 8080")
  48. log.Fatal(http.ListenAndServe(":8080", nil))
  49. }
  50. var pageTemplate = template.Must(template.New("").Parse(`
  51. <table>
  52. {{range .Entries}}
  53. <tr><td>{{.}}</td></tr>
  54. {{end}}
  55. </table>
  56. {{if .Next}}
  57. <a href="/entries?pageToken={{.Next}}">Next Page</a>
  58. {{end}}
  59. `))
  60. func handleEntries(w http.ResponseWriter, r *http.Request) {
  61. ctx := context.Background()
  62. filter := fmt.Sprintf(`logName = "projects/%s/logs/testlog"`, *projectID)
  63. it := client.Entries(ctx, logadmin.Filter(filter))
  64. var entries []*logging.Entry
  65. nextTok, err := iterator.NewPager(it, 5, r.URL.Query().Get("pageToken")).NextPage(&entries)
  66. if err != nil {
  67. http.Error(w, fmt.Sprintf("problem getting the next page: %v", err), http.StatusInternalServerError)
  68. return
  69. }
  70. data := struct {
  71. Entries []*logging.Entry
  72. Next string
  73. }{
  74. entries,
  75. nextTok,
  76. }
  77. var buf bytes.Buffer
  78. if err := pageTemplate.Execute(&buf, data); err != nil {
  79. http.Error(w, fmt.Sprintf("problem executing page template: %v", err), http.StatusInternalServerError)
  80. }
  81. if _, err := buf.WriteTo(w); err != nil {
  82. log.Printf("writing response: %v", err)
  83. }
  84. }