-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.go
68 lines (59 loc) · 1.5 KB
/
server.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
/**
* 启动服务器
*/
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/itwarcraft/myschedule/router"
"log"
"net/http"
"os"
)
type App struct {
//端口
Port string
}
var app App
//在init函数中初始化
func init() {
f, err := os.Open("config.json")
if err != nil {
app.Port = "7890"
fmt.Println("读取配置文件config.json错误!采用默认7890端口")
} else {
defer f.Close()
var bs bytes.Buffer
buf := make([]byte, 1024)
for {
n, _ := f.Read(buf)
if 0 == n {
break
bs.Write(buf[:n])
}
}
er := json.Unmarshal(bs.Bytes(), &app)
if er != nil {
app.Port = "7890"
}
}
}
func main() {
m := mux.NewRouter()
m.HandleFunc("/", router.IndexHandler).Methods("GET")
m.HandleFunc("/login", router.LoginHandler).Methods("POST")
m.HandleFunc("/login", router.LoginHandler).Methods("GET")
m.HandleFunc("/list", router.TodoListHandler)
m.HandleFunc("/todo/add", router.TodoAddHandler).Methods("POST")
m.HandleFunc("/todo/list/{userid:[0-9]+}", router.TodoUserListHandler).Methods("GET")
m.HandleFunc("/todo/finish/{todoid:[0-9]+}", router.TodoFinishHandler)
m.HandleFunc("/todo/finishlist/{userid:[0-9]+}", router.TodoUserFinishListHandler).Methods("GET")
http.Handle("/public/", http.FileServer(http.Dir(".")))
http.Handle("/", m)
url := fmt.Sprintf(":%s", app.Port)
//http.ListenAndServe(url, nil)
fmt.Printf("服务器启动成功,地址:%s\n", url)
log.Fatal("ListenAndServe: ", http.ListenAndServe(url, nil))
}