-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (64 loc) · 1.93 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
package main
import (
"goBlog/conf"
"goBlog/logger"
"goBlog/repository"
"goBlog/router"
"log"
"net/http"
"time"
)
//配置文件
var config *conf.Config
//路由列表
var routers map[string]router.Handler
func init() {
config = conf.Conf
//todo
repository.InitDB(config.DbHost, config.DbPort, config.DbName, config.DbUsername, config.DbPassword)
logger.I("==%s started==", config.SiteName)
routers = map[string]router.Handler{
"/": &router.HomeHandler{},
"/category": &router.CategoryHandler{},
"/about": &router.AboutHandler{},
"/article": &router.PostHandler{},
"/api/category": &router.CategoryApiHandler{},
"/users": &router.UserHandler{},
"/oauth": &router.OauthHandler{},
"/login": &router.LoginHandler{},
"/qrlogin": &router.QrLoginHandler{},
"/register": &router.RegisterHandler{},
"/chats": &router.ChatHandler{},
"/account/": &router.AccountHandler{}, //reset_password reg_compete...
"/admin/": &router.AdminHandler{},
"/location": &router.LocationHandler{},
"/newpost": &router.NewPostHandler{},
}
}
func main() {
defer repository.CloseDB()
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
path := "https://127.0.0.1" + config.SitePortSSL + r.URL.Path
router.Redirect(w, r, path, http.StatusMovedPermanently)
})
err := http.ListenAndServe(config.SitePort, nil)
if err != nil {
log.Fatal("start server error!", err)
}
}()
r := router.NewRouter()
r.DefaultHandler(&router.DefaultHandler{})
for key, value := range routers {
r.Register(key, value)
}
server := &http.Server{
Addr: config.SitePortSSL,
Handler: r,
WriteTimeout: 8 * time.Second,
ReadTimeout: 8 * time.Second,
MaxHeaderBytes: 1 << 20,
}
log.Printf("https listen on %s%s", "https://127.0.0.1", config.SitePortSSL)
log.Fatal(server.ListenAndServeTLS("cert.pem", "key.pem"))
}