Module: Template::Go

Defined in:
lib/souls/cli/create/templates/functions/go/go.rb,
lib/souls/cli/create/templates/functions/go/function.rb

Class Method Summary collapse

Class Method Details

.function(file_name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/souls/cli/create/templates/functions/go/function.rb', line 3

def self.function(file_name)
  <<~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.underscore.camelize}(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

.go(_args) ⇒ Object



3
4
5
6
7
# File 'lib/souls/cli/create/templates/functions/go/go.rb', line 3

def self.go(_args)
  <<~APP
    module example.com/cloudfunction
  APP
end