-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
40 lines (32 loc) · 860 Bytes
/
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
package main
import (
"fmt"
"github.com/Xarth-Mai/iBlogGo/database"
"github.com/Xarth-Mai/iBlogGo/handlers"
"log"
"net/http"
)
func main() {
// 初始化数据库连接
db, err := database.InitDB()
if err != nil {
fmt.Println("Failed to connect to database:", err)
return
}
// HTML模板
http.HandleFunc("/", handlers.PageHandler)
// 静态资源
fs := http.FileServer(http.Dir("assets"))
http.Handle("/assets/", http.StripPrefix("/assets/", fs))
// 文章列表JSON
http.HandleFunc("/list", func(w http.ResponseWriter, r *http.Request) {
handlers.PostListHandler(w, r, db)
})
// 博客文章JSON
http.HandleFunc("/post/", func(w http.ResponseWriter, r *http.Request) {
handlers.PostHandler(w, r, db)
})
// 启动服务器
println("Server started at http://localhost:8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}