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/go/function.rb', line 3
def self.function(file_name)
" // Package p contains an HTTP Cloud Function.\n package p\n\n import (\n \"encoding/json\"\n \"fmt\"\n \"html\"\n \"io\"\n \"log\"\n \"net/http\"\n )\n\n // HelloWorld prints the JSON encoded \"message\" field in the body\n // of the request or \"Hello, World!\" if there isn't one.\n func \#{file_name}(w http.ResponseWriter, r *http.Request) {\n var d struct {\n Message string `json:\"message\"`\n }\n\n if err := json.NewDecoder(r.Body).Decode(&d); err != nil {\n switch err {\n case io.EOF:\n fmt.Fprint(w, \"Hello World!\")\n return\n default:\n log.Printf(\"json.NewDecoder: %v\", err)\n http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)\n return\n }\n }\n\n if d.Message == \"\" {\n fmt.Fprint(w, \"Hello World!\")\n return\n }\n fmt.Fprint(w, html.EscapeString(d.Message))\n }\n\n APP\nend\n"
|