-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
133 lines (120 loc) · 3.06 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"./session"
"crypto/md5"
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
)
var globalSessions *session.Manager
func init() {
globalSessions, _ = session.NewManager("memory", "gosessionid", 3600)
go globalSessions.GC()
}
func sayHelloName(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
fmt.Println(r.Form)
fmt.Println("path", r.URL.Path)
fmt.Println("scheme", r.URL.Scheme)
fmt.Println(r.Form["url_long"])
for k, v := range r.Form {
fmt.Println("Key:", k)
fmt.Println("val:", strings.Join(v, ","))
}
cookie, err := r.Cookie("username")
if err != nil && cookie != nil {
fmt.Fprintf(w, "Hello %s!", cookie.Value)
} else {
fmt.Fprintln(w, "Hello welcome!")
}
}
func generateToken() string {
curTime := time.Now().Unix()
h := md5.New()
io.WriteString(h, strconv.FormatInt(curTime, 10))
token := fmt.Sprintf("%x", h.Sum(nil))
return token
}
// 登陆
func login(w http.ResponseWriter, r *http.Request) {
session := globalSessions.SessionStart(w, r)
if r.Method == "GET" {
token := generateToken()
t, err := template.ParseFiles("login.html")
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
t.Execute(w, token)
} else {
username := r.FormValue("username")
password := r.FormValue("password")
fmt.Println("username:", username)
fmt.Println("password", password)
session.Set("username", username)
expiration := time.Now()
expiration = expiration.AddDate(1, 0, 0)
cookie := http.Cookie{Name: "username", Value: username, Expires: expiration}
http.SetCookie(w, &cookie)
fmt.Fprintf(w, "welcome, %s", r.FormValue("username"))
}
}
func count(w http.ResponseWriter, r *http.Request) {
session := globalSessions.SessionStart(w, r)
createtime := session.Get("createtime")
if createtime == nil {
session.Set("createtime", time.Now().Unix())
} else if createtime.(int64)+360 < (time.Now().Unix()) {
globalSessions.SessionDestroy(w, r)
session = globalSessions.SessionStart(w, r)
}
ct := session.Get("countnum")
if ct == nil {
session.Set("countnum", 1)
} else {
session.Set("countnum", (ct.(int) + 1))
}
t, _ := template.ParseFiles("count.html")
w.Header().Set("Content-type", "text/html")
t.Execute(w, session.Get("countnum"))
}
// 文件上传
func upload(w http.ResponseWriter, r *http.Request) {
fmt.Println("method:", r.Method)
if r.Method == "GET" {
token := generateToken()
t, _ := template.ParseFiles("upload.html")
t.Execute(w, token)
} else {
r.ParseMultipartForm(32 << 20)
file, handler, err := r.FormFile("uploadfile")
if err != nil {
log.Println(err)
return
}
defer file.Close()
fmt.Fprintf(w, "%v", handler.Header)
f, err := os.OpenFile(handler.Filename, os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
log.Println(err)
return
}
defer f.Close()
io.Copy(f, file)
}
}
func main() {
http.HandleFunc("/say", sayHelloName)
http.HandleFunc("/login", login)
http.HandleFunc("/upload", upload)
err := http.ListenAndServe(":9090", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}