11package main
22
33import (
4- "errors"
5-
64 "github.com/gin-gonic/gin"
7- "golang.org/x/crypto/bcrypt"
85 "gorm.io/driver/sqlite"
96 "gorm.io/gorm"
7+ "pdnode.com/website/models"
8+ "pdnode.com/website/routes"
109)
1110
1211// Setup JWT Secret Key
13- var jwtKey = []byte ("your_secret_key_pdnode" )
1412
15- // HashPassword 1. 加密密码(注册时使用)
16- func HashPassword (password string ) (string , error ) {
17- // 强度系数默认为 10,数值越大越慢(越安全)
18- combined := append ([]byte (password ), GetSuperuserToken ()... )
19- bytes , err := bcrypt .GenerateFromPassword (combined , bcrypt .DefaultCost )
20- return string (bytes ), err
21- }
22-
23- // CheckPasswordHash 2. 比对密码(登录时使用)
24- func CheckPasswordHash (password , hash string ) bool {
25- combined := append ([]byte (password ), GetSuperuserToken ()... )
26- err := bcrypt .CompareHashAndPassword ([]byte (hash ), combined )
27- return err == nil
28- }
13+ // HashPassword 改进版(带 Pre-hash)
2914
3015func main () {
3116
@@ -36,149 +21,13 @@ func main() {
3621 panic ("failed to connect database" )
3722 }
3823
39- err = db .AutoMigrate (& Announcement {}, & User {})
24+ err = db .AutoMigrate (& models. Announcement {}, & models. User {})
4025 if err != nil {
4126 panic ("failed to migrate database: " + err .Error ())
4227 }
4328
4429 gin .ForceConsoleColor ()
45- r := gin .Default ()
46-
47- r .GET ("/" , func (c * gin.Context ) {
48- c .JSON (200 , gin.H {
49- "msg" : "Pdnode Website API is running" ,
50- })
51- })
52-
53- r .GET ("/announcements/:id" , func (c * gin.Context ) {
54- id := c .Param ("id" )
55-
56- var a Announcement
57-
58- result := db .First (& a , id )
59-
60- if result .Error != nil {
61-
62- if errors .Is (result .Error , gorm .ErrRecordNotFound ) {
63- c .JSON (404 , gin.H {
64- "message" : "Announcement not found" ,
65- })
66- }
67-
68- c .JSON (500 , gin.H {
69- "message" : "Something went wrong" ,
70- })
71-
72- println ("[Server Error] Query Error: " + result .Error .Error ())
73-
74- return
75- }
76-
77- c .JSON (200 , a )
78- return
79-
80- })
81-
82- r .GET ("/announcements" , func (c * gin.Context ) {
83- var a []Announcement
84- result := db .Order ("created_at desc" ).Find (& a )
85- if result .Error != nil {
86- c .JSON (500 , gin.H {
87- "error" : result .Error .Error (),
88- })
89- return
90- }
91-
92- c .JSON (200 , a )
93-
94- })
95-
96- r .POST ("/login" , func (c * gin.Context ) {
97- var input LoginRequest
98-
99- if err := c .ShouldBind (& input ); err != nil {
100- c .JSON (400 , gin.H {
101- "error" : err .Error (),
102- })
103- return
104- }
105-
106- var user User
107- const fakeBCryptHash = "$2a$10$N9qo8uLOickgx2ZMRZoMyeIjZAgNIhp.qfTMvYeJLvAbpE52EPIvG"
108-
109- result := db .Where ("email = ?" , input .Email ).First (& user )
110-
111- if result .Error != nil {
112- CheckPasswordHash (input .Password , fakeBCryptHash )
113- c .JSON (404 , gin.H {"error" : "Incorrect credentials" })
114- return
115- }
116-
117- isMatch := CheckPasswordHash (input .Password , user .Password )
118-
119- if ! isMatch {
120- c .JSON (401 , gin.H {"error" : "Incorrect credentials" })
121- return
122- }
123-
124- token , err := GenerateToken (user .ID )
125- if err != nil {
126- c .JSON (500 , gin.H {"error" : "Your credentials are correct, but the access key generation is incorrect." })
127- return
128- }
129-
130- c .JSON (200 , gin.H {
131- "message" : "success" ,
132- "token" : token , // 把这个发给前端
133- })
134-
135- })
136-
137- r .POST ("/register" , func (c * gin.Context ) {
138- var input RegisterRequest
139-
140- if err := c .ShouldBind (& input ); err != nil {
141- c .JSON (400 , gin.H {
142- "error" : err .Error (),
143- })
144- return
145- }
146-
147- superUserToken := c .GetHeader ("X-Super-Token" )
148-
149- if superUserToken == "" {
150- c .JSON (403 , gin.H {"error" : "Missing token" })
151- return
152- }
153-
154- if string (GetSuperuserToken ()) != superUserToken {
155- c .JSON (403 , gin.H {"error" : "Invalid token" })
156- return
157- }
158-
159- var count int64
160- // 只统计数量,不查询具体内容,速度更快
161- db .Model (& User {}).Where ("email = ?" , input .Email ).Count (& count )
162-
163- if count > 0 {
164- c .JSON (400 , gin.H {"error" : "Email already taken" })
165- return
166- }
167-
168- newUser := User {
169- Name : input .Name ,
170- Email : input .Email ,
171- Password : input .Password , // 记得用你之前的加密逻辑
172- }
173-
174- db .Create (& newUser )
175-
176- c .JSON (201 , gin.H {
177- "message" : "success" ,
178- })
179- return
180-
181- })
30+ r := routes .SetupRouter (db )
18231
18332 SetUpSuperuser ()
18433
0 commit comments