-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
66 lines (50 loc) · 1.21 KB
/
time.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
package main
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"net/http"
"time"
)
func main() {
http.HandleFunc("/time", timeHandler)
http.HandleFunc("/auth", authHandler)
fmt.Println("Starting on port :9876")
http.ListenAndServe(":9876", nil)
}
func timeHandler(w http.ResponseWriter, r *http.Request) {
t := time.Now().UTC()
fmt.Fprintf(w, "%v", t.Unix())
}
func authHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
call := r.FormValue("call")
if call != "publish" {
fmt.Fprintf(w, "Cool")
fmt.Println("Cool Play")
return
}
secret := r.FormValue("secret")
if len(secret) == 0 {
http.Error(w, "Go away", 500) // Code 403 causes the client to try more requests
fmt.Println("No secret")
return
}
db, err := sql.Open("mysql", "gign:gignmiku@/gign")
defer db.Close()
err = db.Ping()
if err != nil {
http.Error(w, "Impossible to connect to the DB: "+err.Error(), 500)
fmt.Println("DB Error")
return
}
var username string
err = db.QueryRow("SELECT pseudo FROM users WHERE secret=?", secret).Scan(&username)
if err != nil {
http.Error(w, "Go away: "+err.Error(), 500)
fmt.Println("Bad Guy")
return
}
fmt.Fprintf(w, "Cool")
fmt.Println("Cool")
}