-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathhttphello.go
More file actions
executable file
·96 lines (73 loc) · 1.82 KB
/
Copy pathhttphello.go
File metadata and controls
executable file
·96 lines (73 loc) · 1.82 KB
1
2
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Golang HptpHello
*
* @package main
* @author @jeffotoni
* @size 05/2018
*/
package main
import (
"fmt"
"log"
"net/http"
)
const (
PORT = "8080"
Sufix = "\033[0m"
)
// handler hello
func Hello(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
name := r.PostFormValue("name")
w.WriteHeader(http.StatusOK)
if name != "" {
fmt.Fprintf(w, "\nOlá %s\n", name)
fmt.Println("Olá ", name)
} else {
msg := "Você pode tentar o comando abaixo:"
msg2 := "curl -X POST localhost:" + PORT + " -d \"name=jefferson\""
text := fmt.Sprintf("\033[0;36m%s%s", msg, Sufix)
text2 := fmt.Sprintf("\033[0;33m%s%s", msg2, Sufix)
msg3 := "\n####################################################"
msg4 := "\n " + text
msg5 := "\n####################################################"
msg6 := "\n\n-----------------------COMANDO----------------------"
msg7 := "\n" + text2
msg8 := "\n----------------------------------------------------\n\n"
fmt.Fprintf(w, msg3)
fmt.Fprintf(w, msg4)
fmt.Fprintf(w, msg5)
fmt.Fprintf(w, msg6)
fmt.Fprintf(w, msg7)
fmt.Fprintf(w, msg8)
// permitindo a saida
// para descarregar
// no log
fmt.Println(msg3)
fmt.Println(msg4)
fmt.Println(msg5)
fmt.Println(msg6)
fmt.Println(msg7)
fmt.Println(msg8)
}
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
http.Error(w, "POST only", http.StatusMethodNotAllowed)
// permitindo a saida
// para descarregar
// no log
fmt.Println("Error, Post only")
}
}
// nossa funcao
// principal
func main() {
// show msg
fmt.Println("Iniciando Hello...")
// declarando nosso endpoint
http.HandleFunc("/hello", Hello)
// apresentando um log na saida caso
// ocorra algo de errado
log.Fatal(http.ListenAndServe(":"+PORT, nil))
}