main.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "os"
  7. "git.familybaby.top/flight/csdn/database"
  8. "git.familybaby.top/flight/csdn/handlers"
  9. "github.com/gorilla/mux"
  10. )
  11. func main() {
  12. // 初始化数据库
  13. err := database.InitDB()
  14. if err != nil {
  15. log.Fatal("Failed to initialize database:", err)
  16. }
  17. defer database.CloseDB()
  18. // 创建路由器
  19. r := mux.NewRouter()
  20. // 解析路由
  21. r.HandleFunc("/parse", handlers.CreateParse).Methods("POST")
  22. r.HandleFunc("/parse", handlers.GetParses).Methods("GET")
  23. r.HandleFunc("/parse/check", handlers.CheckUrlInvalid).Methods("GET")
  24. r.HandleFunc("/parse/{id}", handlers.GetParse).Methods("GET")
  25. r.HandleFunc("/parse/{id}", handlers.UpdateParse).Methods("PUT")
  26. r.HandleFunc("/parse/{id}", handlers.DeleteParse).Methods("DELETE")
  27. // 用户路由
  28. r.HandleFunc("/user", handlers.CreateUser).Methods("POST")
  29. r.HandleFunc("/user", handlers.GetUsers).Methods("GET")
  30. r.HandleFunc("/user/{id}", handlers.GetUser).Methods("GET")
  31. r.HandleFunc("/user/{id}", handlers.UpdateUser).Methods("PUT")
  32. r.HandleFunc("/user/{id}", handlers.DeleteUser).Methods("DELETE")
  33. // 静态文件服务 (用于Vue前端)
  34. fs := http.FileServer(http.Dir("./static"))
  35. r.PathPrefix("/").Handler(fs)
  36. // 启动服务器
  37. port := os.Getenv("PORT")
  38. if port == "" {
  39. port = "8080"
  40. }
  41. fmt.Println("Server starting on port", port)
  42. log.Fatal(http.ListenAndServe(":"+port, r))
  43. }