Sha256: 30a536accf83b611f01704e436c29780f774668b5ffc374959ea10ed0ef9cf1c

Contents?: true

Size: 1.18 KB

Versions: 4

Compression:

Stored size: 1.18 KB

Contents

module Template
  module Go
    def self.function(file_name)
      file_name.underscore.camelize
      <<~APP
        // Package p contains an HTTP Cloud Function.
        package p

        import (
          "encoding/json"
          "fmt"
          "html"
          "io"
          "log"
          "net/http"
        )

        // HelloWorld prints the JSON encoded "message" field in the body
        // of the request or "Hello, World!" if there isn't one.
        func #{file_name}(w http.ResponseWriter, r *http.Request) {
          var d struct {
            Message string `json:"message"`
          }

          if err := json.NewDecoder(r.Body).Decode(&d); err != nil {
            switch err {
            case io.EOF:
              fmt.Fprint(w, "Hello World!")
              return
            default:
              log.Printf("json.NewDecoder: %v", err)
              http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
              return
            }
          }

          if d.Message == "" {
            fmt.Fprint(w, "Hello World!")
            return
          }
          fmt.Fprint(w, html.EscapeString(d.Message))
        }

      APP
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
souls-1.19.4 lib/souls/cli/create/templates/go/function.rb
souls-1.19.3 lib/souls/cli/create/templates/go/function.rb
souls-1.19.2 lib/souls/cli/create/templates/go/function.rb
souls-1.19.1 lib/souls/cli/create/templates/go/function.rb