Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenhan wang
pastebin
Commits
df2a78f0
Commit
df2a78f0
authored
Jan 17, 2023
by
chenhan wang
Browse files
本地调试1.0
parent
d13a1335
Changes
5
Hide whitespace changes
Inline
Side-by-side
backend/app/controller/foo.go
View file @
df2a78f0
...
@@ -90,7 +90,7 @@ func GetFileContentType(fileType string) string {
...
@@ -90,7 +90,7 @@ func GetFileContentType(fileType string) string {
}
}
// 设置cookie name sid, value link
// 设置cookie name sid, value link
func
SetCookie
(
c
echo
.
Context
,
cookie
*
http
.
Cookie
,
sid
string
,
maxAge
int
,
time_
time
.
Time
)
{
func
SetCookie
(
c
echo
.
Context
,
cookie
*
http
.
Cookie
,
sid
string
,
maxAge
int
,
time_
time
.
Time
)
error
{
cookie
.
Name
=
"User"
// 标识为user
cookie
.
Name
=
"User"
// 标识为user
//cookie.Value = string(uuid) // 通过uuid和数据库,确定user是谁
//cookie.Value = string(uuid) // 通过uuid和数据库,确定user是谁
cookie
.
Value
=
sid
cookie
.
Value
=
sid
...
@@ -110,6 +110,7 @@ func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time
...
@@ -110,6 +110,7 @@ func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time
}
}
// 设置cookie
// 设置cookie
c
.
SetCookie
(
cookie
)
c
.
SetCookie
(
cookie
)
return
nil
}
}
func
srcRead
(
c
echo
.
Context
,
info
*
File
)
(
src
multipart
.
File
,
err
error
)
{
func
srcRead
(
c
echo
.
Context
,
info
*
File
)
(
src
multipart
.
File
,
err
error
)
{
...
...
backend/app/controller/interface.go
View file @
df2a78f0
...
@@ -23,10 +23,6 @@ type File struct {
...
@@ -23,10 +23,6 @@ type File struct {
//Content string `json:"content"`
//Content string `json:"content"`
}
}
type
Text
struct
{
text
string
`json:"text"`
}
func
Ping
(
c
echo
.
Context
)
error
{
func
Ping
(
c
echo
.
Context
)
error
{
// just test
// just test
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"pong!"
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"pong!"
)
...
@@ -43,8 +39,8 @@ func RecvFile(c echo.Context) error {
...
@@ -43,8 +39,8 @@ func RecvFile(c echo.Context) error {
}
}
// 创建目标文件,就是我们打算把用户上传的文件保存到什么地方
// 创建目标文件,就是我们打算把用户上传的文件保存到什么地方
// info.Filename 参数指的是我们以用户上传的文件名,作为目标文件名,也就是服务端保存的文件名跟用户上传的文件名一样
// info.Filename 参数指的是我们以用户上传的文件名,作为目标文件名,也就是服务端保存的文件名跟用户上传的文件名一样
//
dst, filePath := dstCreate(info)
dst
,
filePath
:=
dstCreate
(
info
)
dst
,
_
:=
dstCreate
(
info
)
//
dst, _ := dstCreate(info)
defer
dst
.
Close
()
defer
dst
.
Close
()
if
!
overflow
(
dst
,
8
*
1024
*
1024
)
{
if
!
overflow
(
dst
,
8
*
1024
*
1024
)
{
...
@@ -58,11 +54,11 @@ func RecvFile(c echo.Context) error {
...
@@ -58,11 +54,11 @@ func RecvFile(c echo.Context) error {
}
}
// 更新数据库
// 更新数据库
//sid
, url := DBupdate(c, filePath, info)
_
,
url
:=
DBupdate
(
c
,
filePath
,
info
)
// 不分配uid
sid
:=
"fakesid"
//
//
sid := "fakesid" //
url
:=
"fakeurl"
//
//
url := "fakeurl" //
cookie
:=
new
(
http
.
Cookie
)
//
cookie := new(http.Cookie)
SetCookie
(
c
,
cookie
,
sid
,
info
.
Expiration
,
info
.
Time
)
return
response
.
SendResponse2
(
c
,
http
.
StatusOK
,
GetFileContentType
(
info
.
FileType
),
fmt
.
Sprintf
(
"文件上传成功: %s"
,
info
.
FileName
),
url
)
return
response
.
SendResponse2
(
c
,
http
.
StatusOK
,
GetFileContentType
(
info
.
FileType
),
fmt
.
Sprintf
(
"文件上传成功: %s"
,
info
.
FileName
),
url
)
}
}
...
@@ -94,3 +90,17 @@ func SendFile(c echo.Context) error {
...
@@ -94,3 +90,17 @@ func SendFile(c echo.Context) error {
c
.
JSON
(
http
.
StatusOK
,
info
)
c
.
JSON
(
http
.
StatusOK
,
info
)
return
response
.
SendResponse3
(
c
,
http
.
StatusOK
,
GetFileContentType
(
info
.
FileType
),
data
)
// 返回数据
return
response
.
SendResponse3
(
c
,
http
.
StatusOK
,
GetFileContentType
(
info
.
FileType
),
data
)
// 返回数据
}
}
// 申请一个包含uid的cookie
func
AskUid
(
c
echo
.
Context
)
error
{
nsid
:=
IdGen
(
8
)
info
:=
new
(
File
)
cookie
:=
new
(
http
.
Cookie
)
err
:=
SetCookie
(
c
,
cookie
,
nsid
,
info
.
Expiration
,
info
.
Time
)
if
err
!=
nil
{
return
err
}
else
{
return
c
.
HTML
(
http
.
StatusOK
,
"success"
)
}
}
backend/app/init.go
View file @
df2a78f0
...
@@ -5,6 +5,7 @@ import (
...
@@ -5,6 +5,7 @@ import (
//"net/http"
//"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
)
var
e
*
echo
.
Echo
var
e
*
echo
.
Echo
...
@@ -18,26 +19,35 @@ func InitWebFramework() {
...
@@ -18,26 +19,35 @@ func InitWebFramework() {
logrus
.
Info
(
"echo framework initialized"
)
logrus
.
Info
(
"echo framework initialized"
)
}
}
func
StartServer
()
{
func
StartServer
()
{
e
.
Logger
.
Fatal
(
e
.
Start
(
"127.0.0.1:80"
))
// 启动服务,注意默认端口80不能省略
e
.
Logger
.
Fatal
(
e
.
Start
(
getSetting
(
"testurl"
)))
// 启动服务,注意默认端口80不能省略
//e.Logger.Fatal(e.Start("pastebin:80")) // 启动服务,注意默认端口80不能省略,需要域名解析,config
//e.Logger.Fatal(e.Start("127.0.0.1:80")) // 启动服务,注意默认端口80不能省略
//e.Logger.Fatal(e.Start("http://xlab.zju.edu.cn/test/pastebin/group-1")) // 启动服务,注意默认端口80不能省略,需要域名解析,config
}
}
/*
/*
* 初始化logger设置
* 初始化logger设置
*/
*/
func
InitLogger
(){
func
InitLogger
()
{
//自定义日志格式
//自定义日志格式
logrus
.
SetFormatter
(
&
logrus
.
TextFormatter
{
logrus
.
SetFormatter
(
&
logrus
.
TextFormatter
{
ForceQuote
:
true
,
//键值对加引号
ForceQuote
:
true
,
//键值对加引号
TimestampFormat
:
"2006-01-02 15:04:05"
,
//时间格式
TimestampFormat
:
"2006-01-02 15:04:05"
,
//时间格式
FullTimestamp
:
true
,
FullTimestamp
:
true
,
})
})
logrus
.
SetReportCaller
(
true
)
logrus
.
SetReportCaller
(
true
)
}
}
func
getSetting
(
key
string
)
(
value
string
)
{
// config
viper
.
SetConfigName
(
"config"
)
viper
.
SetConfigType
(
"yaml"
)
viper
.
AddConfigPath
(
"./"
)
err
:=
viper
.
ReadInConfig
()
if
err
!=
nil
{
logrus
.
Panic
(
err
)
}
// connect to database
logInfo
:=
viper
.
GetStringMapString
(
"setting"
)
return
logInfo
[
key
]
}
backend/app/routes.go
View file @
df2a78f0
...
@@ -10,9 +10,11 @@ func addRoutes() {
...
@@ -10,9 +10,11 @@ func addRoutes() {
api
:=
e
.
Group
(
"api"
)
api
:=
e
.
Group
(
"api"
)
api
.
Use
(
middleware
.
Auth
)
api
.
Use
(
middleware
.
Auth
)
api
.
GET
(
"/ping"
,
controller
.
Ping
)
api
.
GET
(
"/ping"
,
controller
.
Ping
)
api
.
GET
(
"/ask/sid"
,
controller
.
AskUid
)
//api.POST("/passwd",controller.AskPasswd) // 密码,返回cookie
//api.POST("/passwd",controller.AskPasswd) // 密码,返回cookie
api
.
POST
(
"/file/recv"
,
controller
.
RecvFile
)
// 接收文件
api
.
POST
(
"/file/recv"
,
controller
.
RecvFile
)
// 接收文件
api
.
POST
(
"/file/send"
,
controller
.
SendFile
)
api
.
POST
(
"/file/send"
,
controller
.
SendFile
)
//api.POST("/text/recv",controller.RecvText)
//api.POST("/text/recv",controller.RecvText)
//api.POST("/text/send",controller.RecvText)
//api.POST("/text/send",controller.RecvText)
}
}
backend/mysql/mysql.ibd
0 → 100644
View file @
df2a78f0
File added
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