Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
fengjie Lu
Pastebin_backend
Commits
d1058854
Commit
d1058854
authored
Dec 13, 2022
by
fengjie Lu
Browse files
final
parent
17b02594
Pipeline
#44
canceled with stages
Changes
4
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
app/controller/controller.go
View file @
d1058854
...
@@ -3,6 +3,7 @@ package controller
...
@@ -3,6 +3,7 @@ package controller
import
(
import
(
"Pastebin/app/response"
"Pastebin/app/response"
"Pastebin/model"
"Pastebin/model"
"errors"
"fmt"
"fmt"
"math/rand"
"math/rand"
"net/http"
"net/http"
...
@@ -11,6 +12,7 @@ import (
...
@@ -11,6 +12,7 @@ import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"gorm.io/gorm"
)
)
var
letters
=
[]
rune
(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
var
letters
=
[]
rune
(
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
)
...
@@ -24,56 +26,152 @@ func randStr(n int) string {
...
@@ -24,56 +26,152 @@ func randStr(n int) string {
return
string
(
b
)
return
string
(
b
)
}
}
func
Text
(
c
echo
.
Context
)
error
{
func
Text
(
c
echo
.
Context
)
error
{
rec
:=
new
(
LIST
)
var
rec
LIST
rec
.
Id
=
0
rec
.
Id
=
0
rec
.
Name
=
randStr
(
4
)
var
s
string
rec
.
Password
=
c
.
QueryParam
(
"Password"
)
for
{
rec
.
ClickTimes
,
_
=
strconv
.
Atoi
(
c
.
QueryParam
(
"ClickTimes"
))
s
=
randStr
(
4
)
rec
.
RemainTime
,
_
=
strconv
.
Atoi
(
c
.
QueryParam
(
"RemainTime"
))
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Where
(
"name = ?"
,
s
)
.
First
(
&
LIST
{})
.
Error
;
errors
.
Is
(
err
,
gorm
.
ErrRecordNotFound
)
{
rec
.
Txt
=
c
.
QueryParam
(
"Txt"
)
rec
.
Name
=
s
logrus
.
Error
(
"Genarating String Success!"
)
break
}
logrus
.
Info
(
"Genarating New String"
)
}
rec
.
Password
=
c
.
Request
()
.
PostFormValue
(
"Password"
)
rec
.
Clicktimes
,
_
=
strconv
.
Atoi
(
c
.
Request
()
.
PostFormValue
(
"Clicktimes"
))
rec
.
Remaintime
,
_
=
strconv
.
Atoi
(
c
.
Request
()
.
PostFormValue
(
"Remaintime"
))
rec
.
Txt
=
c
.
Request
()
.
PostFormValue
(
"Txt"
)
rec
.
Type
=
false
rec
.
Type
=
false
if
err
:=
model
.
DB
.
Debug
()
.
Create
(
&
rec
)
.
Error
;
err
!=
nil
{
tm
:=
time
.
Now
()
rec
.
Starttime
=
tm
.
Unix
()
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Debug
()
.
Create
(
&
rec
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
logrus
.
Error
(
err
)
}
}
println
(
"Insert TEXT success!"
)
println
(
"Insert TEXT success!"
)
s
:
=
fmt
.
Sprintf
(
"WebCode:%s"
,
rec
.
Name
)
s
=
fmt
.
Sprintf
(
"WebCode:%s"
,
rec
.
Name
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"Insert TEXT success!"
,
s
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"Insert TEXT success!"
,
s
)
}
}
func
File
(
c
echo
.
Context
)
error
{
func
File
(
c
echo
.
Context
)
error
{
rec
:=
new
(
LIST
)
var
rec
LIST
rec
.
Id
=
0
rec
.
Id
=
0
rec
.
Name
=
randStr
(
4
)
var
s
string
rec
.
Password
=
c
.
QueryParam
(
"Password"
)
for
{
rec
.
ClickTimes
,
_
=
strconv
.
Atoi
(
c
.
QueryParam
(
"ClickTimes"
))
s
=
randStr
(
4
)
rec
.
RemainTime
,
_
=
strconv
.
Atoi
(
c
.
QueryParam
(
"RemainTime"
))
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Where
(
"name = ?"
,
s
)
.
First
(
&
LIST
{})
.
Error
;
errors
.
Is
(
err
,
gorm
.
ErrRecordNotFound
)
{
rec
.
File
=
[]
byte
(
c
.
QueryParam
(
"File"
))
rec
.
Name
=
s
logrus
.
Error
(
"Genarating String Success!"
)
break
}
logrus
.
Info
(
"Genarating New String"
)
}
rec
.
Password
=
c
.
Request
()
.
PostFormValue
(
"Password"
)
rec
.
Clicktimes
,
_
=
strconv
.
Atoi
(
c
.
Request
()
.
PostFormValue
(
"Clicktimes"
))
rec
.
Remaintime
,
_
=
strconv
.
Atoi
(
c
.
Request
()
.
PostFormValue
(
"Remaintime"
))
tm
:=
time
.
Now
()
rec
.
Starttime
=
tm
.
Unix
()
if
err
:=
c
.
Request
()
.
ParseMultipartForm
(
100000
);
err
!=
nil
{
logrus
.
Error
(
err
)
}
file
,
fileHeader
,
err
:=
c
.
Request
()
.
FormFile
(
"File"
)
if
err
!=
nil
{
logrus
.
Error
(
err
)
}
defer
file
.
Close
()
fmt
.
Printf
(
"the uploaded file: name[%s], size[%d], header[%#v]
\n
"
,
fileHeader
.
Filename
,
fileHeader
.
Size
,
fileHeader
.
Header
)
data
:=
make
([]
byte
,
fileHeader
.
Size
)
_
,
err
=
file
.
Read
(
data
)
if
err
!=
nil
{
logrus
.
Error
(
err
)
}
rec
.
File
=
data
rec
.
Type
=
true
rec
.
Type
=
true
if
err
:=
model
.
DB
.
Debug
()
.
Create
(
&
rec
)
.
Error
;
err
!=
nil
{
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Debug
()
.
Create
(
&
rec
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
logrus
.
Error
(
err
)
}
}
println
(
"Insert FILE success!"
)
println
(
"Insert FILE success!"
)
s
:
=
fmt
.
Sprintf
(
"WebCode:%s"
,
rec
.
Name
)
s
=
fmt
.
Sprintf
(
"WebCode:%s"
,
rec
.
Name
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"Insert FILE success!"
,
s
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"Insert FILE success!"
,
s
)
}
}
/*
func
Session
(
c
echo
.
Context
)
error
{
func
Session
(
c
echo
.
Context
)
error
{
var
ses
SESSION
kie
:=
c
.
Request
()
.
PostFormValue
(
"Cookie"
)
if
err
:=
model
.
DB
.
Table
(
"sessions"
)
.
Debug
()
.
Where
(
"cookie = ?"
,
kie
)
.
First
(
&
ses
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
"Not find Session"
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"Status:false"
)
}
tm
:=
time
.
Now
()
if
tm
.
Unix
()
-
ses
.
Starttime
>=
int64
(
ses
.
Time
)
{
logrus
.
Info
(
"SESSION Time out"
)
if
err
:=
model
.
DB
.
Table
(
"sessions"
)
.
Debug
()
.
Where
(
"cookie = ?"
,
kie
)
.
Delete
(
&
ses
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
}
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"Status:false"
)
}
else
{
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"Status:true"
)
}
}
}
func Check(c echo.Context) error {
func
Check
(
c
echo
.
Context
)
error
{
var
rec
LIST
name
:=
c
.
Request
()
.
PostFormValue
(
"Name"
)
recPass
:=
c
.
Request
()
.
PostFormValue
(
"Password"
)
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Debug
()
.
Where
(
"name = ?"
,
name
)
.
First
(
&
rec
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
}
tm
:=
time
.
Now
()
if
tm
.
Unix
()
-
rec
.
Starttime
>=
int64
(
rec
.
Remaintime
)
||
rec
.
Clicktimes
==
0
{
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Debug
()
.
Where
(
"name = ?"
,
name
)
.
First
(
&
rec
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
}
logrus
.
Info
(
"LIST Time out or Click out"
)
return
response
.
SendResponse
(
c
,
http
.
StatusBadRequest
,
""
,
"Status:false"
)
}
if
rec
.
Password
==
recPass
{
CreateSession
(
c
.
Request
()
.
PostFormValue
(
"Cookie"
))
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Debug
()
.
Where
(
"name = ?"
,
name
)
.
Update
(
"Clicktimes"
,
rec
.
Clicktimes
-
1
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
}
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"Status:true"
)
}
else
{
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"Status:false"
)
}
}
}
func
Content
(
c
echo
.
Context
)
error
{
func
Content
(
c
echo
.
Context
)
error
{
var
rec
LIST
name
:=
c
.
Request
()
.
PostFormValue
(
"Name"
)
if
err
:=
model
.
DB
.
Table
(
"lists"
)
.
Debug
()
.
Where
(
"name = ?"
,
name
)
.
First
(
&
rec
)
.
Error
;
err
!=
nil
{
logrus
.
Error
(
err
)
return
response
.
SendResponse
(
c
,
http
.
StatusBadRequest
,
""
,
rec
)
}
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
rec
)
}
func
CreateSession
(
Cookie
string
)
{
tm
:=
time
.
Now
()
if
err
:=
model
.
DB
.
Table
(
"sessions"
)
.
Create
(
&
SESSION
{
0
,
Cookie
,
30
,
tm
.
Unix
()});
err
!=
nil
{
logrus
.
Error
(
err
)
}
println
(
"Session is Created"
)
}
}
*/
type
LIST
struct
{
type
LIST
struct
{
Id
int
`gorm:"primary_key;AUTO_INCREMENT"`
Id
int
`gorm:"primary_key;AUTO_INCREMENT"`
Name
string
Name
string
`json:"Name" form:"Name" query:"Name"`
Password
string
Password
string
`json:"Password" form:"Password" query:"Password"`
Type
bool
Type
bool
`json:"Type" form:"Type" query:"Type"`
Txt
string
Txt
string
`json:"Txt" form:"Txt" query:"Txt"`
File
[]
byte
`gorm:"type:MediumBlob"`
File
[]
byte
`gorm:"type:MediumBlob" json:"File" form:"File" query:"File"`
ClickTimes
int
Clicktimes
int
`json:"Clicktimes" form:"Clicktimes" query:"Clicktimes"`
RemainTime
int
Remaintime
int
`json:"Remaintime" form:"Remaintime" query:"Remaintime"`
Starttime
int64
}
type
SESSION
struct
{
Id
int
`gorm:"primary_key;AUTO_INCREMENT"`
Cookie
string
`json:"Cookie" form:"Cookie" query:"Cookie"`
Time
int
//剩余时间
Starttime
int64
}
}
app/route.go
View file @
d1058854
...
@@ -9,7 +9,7 @@ func AddRoutes() {
...
@@ -9,7 +9,7 @@ func AddRoutes() {
e
.
Use
(
middleware
.
Auth
)
e
.
Use
(
middleware
.
Auth
)
e
.
POST
(
"/submit/text"
,
controller
.
Text
)
e
.
POST
(
"/submit/text"
,
controller
.
Text
)
e
.
POST
(
"/submit/file"
,
controller
.
File
)
e
.
POST
(
"/submit/file"
,
controller
.
File
)
//e.GE
T("/query/session", controller.Session)
e
.
POS
T
(
"/query/session"
,
controller
.
Session
)
//e.GE
T("/query/check", controller.Check)
e
.
POS
T
(
"/query/check"
,
controller
.
Check
)
//e.GE
T("/query/content", controller.Content)
e
.
POS
T
(
"/query/content"
,
controller
.
Content
)
}
}
go.sum
View file @
d1058854
...
@@ -138,8 +138,6 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
...
@@ -138,8 +138,6 @@ github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pty
v1.1.1/go.mod h1:
pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty
v1.1.1/go.mod h1:
pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text
v0.1.0/go.mod h1:
4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text
v0.1.0/go.mod h1:
4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text
v0.2.0 h1:
5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text
v0.2.0 h1:
5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/labstack/echo
v3.3.10+incompatible h1:
pGRcYk231ExFAyoAjAfD85kQzRJCRI8bbnE7CX5OEgg=
github.com/labstack/echo
v3.3.10+incompatible/go.mod h1:
0INS7j/VjnFxD4E2wkz67b8cVwCLbBmJyDaka6Cmk1s=
github.com/labstack/echo/v4
v4.9.1 h1:
GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y=
github.com/labstack/echo/v4
v4.9.1 h1:
GliPYSpzGKlyOhqIbG8nmHBo3i1saKWFOgh41AN3b+Y=
github.com/labstack/echo/v4
v4.9.1/go.mod h1:
Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo=
github.com/labstack/echo/v4
v4.9.1/go.mod h1:
Pop5HLc+xoc4qhTZ1ip6C0RtP7Z+4VzRLWZZFKqbbjo=
github.com/labstack/gommon
v0.4.0 h1:
y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon
v0.4.0 h1:
y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
...
...
model/init.go
View file @
d1058854
...
@@ -15,6 +15,10 @@ func DBinit() {
...
@@ -15,6 +15,10 @@ func DBinit() {
if
err
!=
nil
{
if
err
!=
nil
{
logrus
.
Fatal
(
err
)
logrus
.
Fatal
(
err
)
}
}
err
=
DB
.
AutoMigrate
(
&
SESSION
{})
if
err
!=
nil
{
logrus
.
Fatal
(
err
)
}
println
(
"Connect Success!"
)
println
(
"Connect Success!"
)
}
}
...
@@ -34,6 +38,7 @@ func ConnectDatabase() {
...
@@ -34,6 +38,7 @@ func ConnectDatabase() {
"?charset=utf8mb4&parseTime=True&loc=Local"
"?charset=utf8mb4&parseTime=True&loc=Local"
var
err
error
var
err
error
DB
,
err
=
gorm
.
Open
(
mysql
.
Open
(
dbArgs
),
&
gorm
.
Config
{})
DB
,
err
=
gorm
.
Open
(
mysql
.
Open
(
dbArgs
),
&
gorm
.
Config
{})
DB
.
AllowGlobalUpdate
=
true
if
err
!=
nil
{
if
err
!=
nil
{
logrus
.
Fatal
(
err
)
logrus
.
Fatal
(
err
)
}
}
...
@@ -48,6 +53,13 @@ type LIST struct {
...
@@ -48,6 +53,13 @@ type LIST struct {
Type
bool
Type
bool
Txt
string
Txt
string
File
[]
byte
`gorm:"type:MediumBlob"`
File
[]
byte
`gorm:"type:MediumBlob"`
ClickTimes
int
Clicktimes
int
RemainTime
int
Remaintime
int
Starttime
int64
}
type
SESSION
struct
{
Id
int
`gorm:"primary_key;AUTO_INCREMENT"`
Cookie
string
`json:"Cookie" form:"Cookie" query:"Cookie"`
Time
int
//剩余时间
Starttime
int64
}
}
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