-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
110 lines (91 loc) · 2.38 KB
/
main.go
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"html/template"
"io/ioutil"
"log"
"net/http"
"os"
)
/*
func handleForm(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
http.Error(w, "Método não suportado", http.StatusMethodNotAllowed)
return
}
// Obter os valores dos campos de entrada
label1 := r.FormValue("label1")
label2 := r.FormValue("label2")
// Processar os valores dos labels
fmt.Println("Label 1:", label1)
fmt.Println("Label 2:", label2)
// Enviar uma resposta para o cliente
fmt.Fprintf(w, "Texto dos labels recebido com sucesso!")
}
*/
// Esse serve para página estática
func primeiraResposta(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
}
type Pagina struct {
Titulo string
Corpo string
}
func Erro(w http.ResponseWriter) {
fmt.Printf("Deu ruim")
fmt.Fprintf(w, `<h1>A página não foi encontrada.</hl>`)
return
}
// Modelo para página Template
func Modelo(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFiles("src/modelo.html")
if err != nil {
Erro(w)
}
/*
fmt.Printf("Deu ruim")
fmt.Fprintf(w, `<h1>A página não foi encontrada.</hl>`)
return
}
*/
pagina := Pagina{Titulo: "Titulo Poggers", Corpo: "Dados importantes"}
tmpl.Execute(w, pagina)
}
func Boleto(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "src/boleto.html")
}
/*
func Pasta(w http.ResponseWriter, r *http.Request) {
w.ResponseWriter(w, http.Dir("./src/"))
}
*/
func CSS(w http.ResponseWriter, r *http.Request) {
// Leia o conteúdo do arquivo CSS
cssData, _ := ioutil.ReadFile("src/estilo.css")
/*
if err != nil {
http.Error(w, "Erro ao ler o arquivo CSS", http.StatusInternalServerError)
return
}*/
// Defina o tipo de conteúdo como CSS
w.Header().Set("Content-Type", "text/css")
// Escreva o conteúdo CSS na resposta
w.Write(cssData)
}
func main() {
// Definir a porta do servidor
port := os.Getenv("PORT")
if port == "" {
port = "8080" // Porta padrão
}
http.HandleFunc("/estilo.css", CSS)
http.HandleFunc("/boleto", Boleto)
//http.HandleFunc("/", Pasta)
http.HandleFunc("/", Modelo)
// Iniciar o servidor HTTP
fmt.Println("Servidor iniciado na porta", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("Erro ao iniciar o servidor:", err)
}
}