123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- package main
- import (
- "fmt"
- "log"
- "net/http"
- "os"
- "git.familybaby.top/flight/csdn/database"
- "git.familybaby.top/flight/csdn/handlers"
- "github.com/gorilla/mux"
- )
- func main() {
- // 初始化数据库
- err := database.InitDB()
- if err != nil {
- log.Fatal("Failed to initialize database:", err)
- }
- defer database.CloseDB()
- // 创建路由器
- r := mux.NewRouter()
- // 解析路由
- r.HandleFunc("/parse", handlers.CreateParse).Methods("POST")
- r.HandleFunc("/parse", handlers.GetParses).Methods("GET")
- r.HandleFunc("/parse/check", handlers.CheckUrlInvalid).Methods("GET")
- r.HandleFunc("/parse/{id}", handlers.GetParse).Methods("GET")
- r.HandleFunc("/parse/{id}", handlers.UpdateParse).Methods("PUT")
- r.HandleFunc("/parse/{id}", handlers.DeleteParse).Methods("DELETE")
- // 用户路由
- r.HandleFunc("/user", handlers.CreateUser).Methods("POST")
- r.HandleFunc("/user", handlers.GetUsers).Methods("GET")
- r.HandleFunc("/user/{id}", handlers.GetUser).Methods("GET")
- r.HandleFunc("/user/{id}", handlers.UpdateUser).Methods("PUT")
- r.HandleFunc("/user/{id}", handlers.DeleteUser).Methods("DELETE")
- // 静态文件服务 (用于Vue前端)
- fs := http.FileServer(http.Dir("./static"))
- r.PathPrefix("/").Handler(fs)
- // 启动服务器
- port := os.Getenv("PORT")
- if port == "" {
- port = "8080"
- }
- fmt.Println("Server starting on port", port)
- log.Fatal(http.ListenAndServe(":"+port, r))
- }
|