/
main.go
59 lines (55 loc) · 1.66 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
package main
import (
"fmt"
"html/template"
"log"
"net/http"
"time"
"token/controller"
"token/utils"
)
/**
* 404
* [NotFoundHandler description]
* @param {[type]} w http.ResponseWriter [description]
* @param {[type]} r *http.Request [description]
*/
func NotFoundHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
http.Redirect(w, r, "login", http.StatusFound) //302
}
t, err := template.ParseFiles("views/404.html")
utils.CheckError(err)
t.Execute(w, nil)
data := utils.OutputJsonData(404, "请求的方法不存在!")
fmt.Fprintln(w, data)
}
//token认证服务
func main() {
fmt.Println("服务开始启动.....")
fmt.Println("......启动时间:", time.Now())
//访问静态资源
go http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static/"))))
//访问静态资源
go http.Handle("/views/", http.StripPrefix("/views/", http.FileServer(http.Dir("./views/"))))
go http.HandleFunc("/userList", controller.UserList)
go http.HandleFunc("/getToken", controller.GetToken)
go http.HandleFunc("/token", controller.ValidateToken)
go http.HandleFunc("/destroyToken", controller.DestroyToken)
go http.HandleFunc("/login", controller.Login)
go http.HandleFunc("/", NotFoundHandler)
services := &http.Server{
Addr: ":8899",
// Handler: myHandler,
ReadTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Fatal(services.ListenAndServe())
fmt.Println("......启动完成:", time.Now())
// err := http.ListenAndServe(":8899", nil)
// fmt.Println("......启动完成:", time.Now())
// if err != nil {
// log.Println("ListenAndServe: ", err)
// }
}