Archived
1
0

Merge files into main.go

This commit is contained in:
nemunaire 2016-10-19 20:37:01 +02:00
parent 8ded814424
commit 4f553f6022
4 changed files with 16 additions and 33 deletions

11
home.go
View File

@ -1,11 +0,0 @@
package main
import (
"log"
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
log.Printf("Serve home page to %s: %s [%s]\n", r.Header.Get("X-Forwarded-For"), r.URL.Path, r.UserAgent())
http.ServeFile(w, r, "home.html")
}

View File

@ -12,7 +12,7 @@
Aidez-moi à améliorer ce cours en donnant votre avis.
</p>
<form method="post" action="/vote">
<form method="post" action="vote">
<div class="form-group">
<h3>Quel est le cours/TP que vous avez préféré ?</h3>

16
main.go
View File

@ -7,13 +7,27 @@ import (
"net/http"
)
func Home(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
log.Printf("Handling new vote from %s: %s [%s]\n", r.Header.Get("X-Forwarded-For"), r.URL.Path, r.UserAgent())
for k, v := range r.PostForm {
log.Println(k + ":", v)
}
http.ServeFile(w, r, "thanks.html")
} else {
log.Printf("Serve home page to %s: %s [%s]\n", r.Header.Get("X-Forwarded-For"), r.URL.Path, r.UserAgent())
http.ServeFile(w, r, "home.html")
}
}
func main() {
var bind = flag.String("bind", "0.0.0.0:8081", "Bind port/socket")
flag.Parse()
log.Println("Registering handlers...")
http.HandleFunc("/", Home)
http.HandleFunc("/vote", Vote)
log.Println(fmt.Sprintf("Ready, listening on %s", *bind))
if err := http.ListenAndServe(*bind, nil); err != nil {
log.Fatal("Unable to listen and serve: ", err)

20
vote.go
View File

@ -1,20 +0,0 @@
package main
import (
"log"
"net/http"
)
func Vote(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
log.Printf("Handling new vote from %s: %s [%s]\n", r.Header.Get("X-Forwarded-For"), r.URL.Path, r.UserAgent())
for k, v := range r.PostForm {
log.Println(k + ":", v)
}
http.ServeFile(w, r, "thanks.html")
} else {
http.NotFound(w, r)
}
}