Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
liang hong
CommentSever
Commits
44ba2b60
Commit
44ba2b60
authored
May 08, 2025
by
CodeFever888
Browse files
'sever'
parents
Changes
4
Hide whitespace changes
Inline
Side-by-side
.gitignore
0 → 100644
View file @
44ba2b60
*.yaml
\ No newline at end of file
go.mod
0 → 100644
View file @
44ba2b60
module ChatSever
go 1.24.2
require
gorm.io/driver/postgres
v1.5.11
require (
github.com/fsnotify/fsnotify
v1.8.0 // indirect
github.com/go-viper/mapstructure/v2
v2.2.1 // indirect
github.com/pelletier/go-toml/v2
v2.2.3 // indirect
github.com/sagikazarmark/locafero
v0.7.0 // indirect
github.com/sourcegraph/conc
v0.3.0 // indirect
github.com/spf13/afero
v1.12.0 // indirect
github.com/spf13/cast
v1.7.1 // indirect
github.com/spf13/pflag
v1.0.6 // indirect
github.com/subosito/gotenv
v1.6.0 // indirect
go.uber.org/atomic
v1.9.0 // indirect
go.uber.org/multierr
v1.9.0 // indirect
golang.org/x/sys
v0.29.0 // indirect
gopkg.in/yaml.v3
v3.0.1 // indirect
)
require (
github.com/jackc/pgpassfile
v1.0.0 // indirect
github.com/jackc/pgservicefile
v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5
v5.5.5 // indirect
github.com/jackc/puddle/v2
v2.2.1 // indirect
github.com/jinzhu/inflection
v1.0.0 // indirect
github.com/jinzhu/now
v1.1.5 // indirect
github.com/spf13/viper
v1.20.1
golang.org/x/crypto
v0.32.0 // indirect
golang.org/x/sync
v0.10.0 // indirect
golang.org/x/text
v0.21.0 // indirect
gorm.io/gorm
v1.25.10 // indirect
)
go.sum
0 → 100644
View file @
44ba2b60
github.com/davecgh/go-spew
v1.1.0/go.mod h1:
J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew
v1.1.1/go.mod h1:
J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify
v1.8.0 h1:
dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
github.com/fsnotify/fsnotify
v1.8.0/go.mod h1:
8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
github.com/go-viper/mapstructure/v2
v2.2.1 h1:
ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2
v2.2.1/go.mod h1:
oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/jackc/pgpassfile
v1.0.0 h1:
/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile
v1.0.0/go.mod h1:
CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile
v0.0.0-20221227161230-091c0ba34f0a h1:
bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile
v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:
5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5
v5.5.5 h1:
amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
github.com/jackc/pgx/v5
v5.5.5/go.mod h1:
ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
github.com/jackc/puddle/v2
v2.2.1 h1:
RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2
v2.2.1/go.mod h1:
vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jackc/puddle/v2
v2.2.2 h1:
PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2
v2.2.2/go.mod h1:
vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jinzhu/inflection
v1.0.0 h1:
K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection
v1.0.0/go.mod h1:
h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now
v1.1.5 h1:
/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now
v1.1.5/go.mod h1:
d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/pelletier/go-toml/v2
v2.2.3 h1:
YmeHyLY8mFWbdkNWwpr+qIL2bEqT0o95WSdkNHvL12M=
github.com/pelletier/go-toml/v2
v2.2.3/go.mod h1:
MfCQTFTvCcUyyvvwm1+G6H/jORL20Xlb6rzQu9GuUkc=
github.com/pmezard/go-difflib
v1.0.0/go.mod h1:
iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sagikazarmark/locafero
v0.7.0 h1:
5MqpDsTGNDhY8sGp0Aowyf0qKsPrhewaLSsFaodPcyo=
github.com/sagikazarmark/locafero
v0.7.0/go.mod h1:
2za3Cg5rMaTMoG/2Ulr9AwtFaIppKXTRYnozin4aB5k=
github.com/sourcegraph/conc
v0.3.0 h1:
OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc
v0.3.0/go.mod h1:
Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero
v1.12.0 h1:
UcOPyRBYczmFn6yvphxkn9ZEOY65cpwGKb5mL36mrqs=
github.com/spf13/afero
v1.12.0/go.mod h1:
ZTlWwG4/ahT8W7T0WQ5uYmjI9duaLQGy3Q2OAl4sk/4=
github.com/spf13/cast
v1.7.1 h1:
cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast
v1.7.1/go.mod h1:
ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/pflag
v1.0.6 h1:
jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag
v1.0.6/go.mod h1:
McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/spf13/viper
v1.20.1 h1:
ZMi+z/lvLyPSCoNtFCpqjy0S4kPbirhpTMwl8BkW9X4=
github.com/spf13/viper
v1.20.1/go.mod h1:
P9Mdzt1zoHIG8m2eZQinpiBjo6kCmZSKBClNNqjJvu4=
github.com/stretchr/objx
v0.1.0/go.mod h1:
HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify
v1.3.0/go.mod h1:
M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify
v1.7.0/go.mod h1:
6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv
v1.6.0 h1:
9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv
v1.6.0/go.mod h1:
Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
go.uber.org/atomic
v1.9.0 h1:
ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic
v1.9.0/go.mod h1:
fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr
v1.9.0 h1:
7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
go.uber.org/multierr
v1.9.0/go.mod h1:
X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ=
golang.org/x/crypto
v0.17.0 h1:
r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto
v0.17.0/go.mod h1:
gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/crypto
v0.32.0 h1:
euUpcYgM8WcP71gNpTqQCn6rC2t6ULUPiOzfWaXVVfc=
golang.org/x/crypto
v0.32.0/go.mod h1:
ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
golang.org/x/sync
v0.1.0 h1:
wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync
v0.1.0/go.mod h1:
RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync
v0.10.0 h1:
3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
golang.org/x/sync
v0.10.0/go.mod h1:
Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys
v0.29.0 h1:
TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys
v0.29.0/go.mod h1:
/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text
v0.14.0 h1:
ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text
v0.14.0/go.mod h1:
18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text
v0.21.0 h1:
zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text
v0.21.0/go.mod h1:
4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1
v0.0.0-20161208181325-20d25e280405/go.mod h1:
Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3
v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:
K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3
v3.0.1 h1:
fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3
v3.0.1/go.mod h1:
K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres
v1.5.11 h1:
ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
gorm.io/driver/postgres
v1.5.11/go.mod h1:
DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm
v1.25.10 h1:
dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm
v1.25.10/go.mod h1:
hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
main.go
0 → 100644
View file @
44ba2b60
package
main
import
(
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/spf13/viper"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type
Response
struct
{
Code
int
`json:"code"`
Msg
string
`json:"msg"`
Data
any
`json:"data"`
}
type
Comment
struct
{
ID
int
`json:"id" gorm:"primaryKey"`
Name
string
`json:"name"`
Content
string
`json:"content"`
}
var
db
*
gorm
.
DB
func
init
()
{
viper
.
SetConfigName
(
"config"
)
viper
.
SetConfigType
(
"yaml"
)
viper
.
AddConfigPath
(
"."
)
viper
.
ReadInConfig
()
host
:=
viper
.
GetString
(
"db.host"
)
user
:=
viper
.
GetString
(
"db.user"
)
password
:=
viper
.
GetString
(
"db.password"
)
dbname
:=
viper
.
GetString
(
"db.dbname"
)
port
:=
viper
.
GetString
(
"db.port"
)
sslmode
:=
viper
.
GetString
(
"db.sslmode"
)
timezone
:=
viper
.
GetString
(
"db.timezone"
)
dsn
:=
fmt
.
Sprintf
(
"host=%s user=%s password=%s dbname=%s port=%s sslmode=%s TimeZone=%s"
,
host
,
user
,
password
,
dbname
,
port
,
sslmode
,
timezone
)
var
err
error
db
,
err
=
gorm
.
Open
(
postgres
.
Open
(
dsn
),
&
gorm
.
Config
{})
if
err
!=
nil
{
panic
(
"failed to connect database"
)
}
// 自动迁移 Comment 表
if
err
:=
db
.
AutoMigrate
(
&
Comment
{});
err
!=
nil
{
panic
(
"failed to migrate database"
)
}
}
func
enableCors
(
w
http
.
ResponseWriter
)
{
w
.
Header
()
.
Set
(
"Access-Control-Allow-Origin"
,
"*"
)
}
func
getHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
enableCors
(
w
)
page
:=
r
.
URL
.
Query
()
.
Get
(
"page"
)
size
:=
r
.
URL
.
Query
()
.
Get
(
"size"
)
// Convert page and size to integers
pageInt
,
err
:=
strconv
.
Atoi
(
page
)
if
err
!=
nil
||
pageInt
<
1
{
pageInt
=
1
}
sizeInt
,
err
:=
strconv
.
Atoi
(
size
)
if
err
!=
nil
||
sizeInt
<
1
{
sizeInt
=
10
}
// Fetch comments from the database with pagination
var
comments
[]
Comment
var
total
int64
db
.
Model
(
&
Comment
{})
.
Count
(
&
total
)
db
.
Offset
((
pageInt
-
1
)
*
sizeInt
)
.
Limit
(
sizeInt
)
.
Find
(
&
comments
)
response
:=
Response
{
Code
:
0
,
Msg
:
"success"
,
Data
:
map
[
string
]
interface
{}{
"total"
:
total
,
"comments"
:
comments
,
},
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
if
err
:=
json
.
NewEncoder
(
w
)
.
Encode
(
response
);
err
!=
nil
{
http
.
Error
(
w
,
"Failed to encode response"
,
http
.
StatusInternalServerError
)
return
}
}
func
addHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
var
comment
Comment
json
.
NewDecoder
(
r
.
Body
)
.
Decode
(
&
comment
)
if
comment
.
Name
==
""
||
comment
.
Content
==
""
{
return
}
db
.
Create
(
&
comment
)
response
:=
Response
{
Code
:
0
,
Msg
:
"success"
,
Data
:
comment
,
}
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
}
func
deleteHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
id
:=
r
.
URL
.
Query
()
.
Get
(
"id"
)
idInt
,
err
:=
strconv
.
Atoi
(
id
)
if
err
!=
nil
{
http
.
Error
(
w
,
"Invalid ID"
,
http
.
StatusBadRequest
)
return
}
db
.
Delete
(
&
Comment
{},
idInt
)
response
:=
Response
{
Code
:
0
,
Msg
:
"success"
,
Data
:
nil
,
}
w
.
Header
()
.
Set
(
"Content-Type"
,
"application/json"
)
json
.
NewEncoder
(
w
)
.
Encode
(
response
)
}
func
main
()
{
http
.
HandleFunc
(
"/comment/get"
,
getHandler
)
http
.
HandleFunc
(
"/comment/add"
,
addHandler
)
http
.
HandleFunc
(
"/comment/delete"
,
deleteHandler
)
http
.
ListenAndServe
(
":8080"
,
nil
)
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment