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
1898bea4
Commit
1898bea4
authored
Feb 14, 2023
by
chenhan wang
Browse files
feat:read settings from config and save in a struct
parent
e76be213
Changes
6
Show whitespace changes
Inline
Side-by-side
backend/app/controller/foo.go
View file @
1898bea4
...
...
@@ -2,8 +2,11 @@ package controller
import
(
"backend/model"
"io"
"math/rand"
"mime/multipart"
"net/http"
"strconv"
"time"
"github.com/labstack/echo/v4"
...
...
@@ -97,6 +100,40 @@ func SetCookie(c echo.Context, cookie *http.Cookie, sid string, maxAge int, time
return
nil
}
func
FileRead
(
file
*
multipart
.
FileHeader
)
(
string
,
int
,
error
)
{
// 打开用户上传的文件
src
,
err
:=
file
.
Open
()
if
err
!=
nil
{
logrus
.
Println
(
err
)
return
""
,
0
,
err
}
defer
src
.
Close
()
//读取文件内容
var
data
[]
byte
size
:=
0
buf
:=
make
([]
byte
,
1024
)
for
{
n
,
err
:=
src
.
Read
(
buf
)
if
err
!=
nil
&&
err
!=
io
.
EOF
{
logrus
.
Panic
(
err
)
return
""
,
0
,
err
}
//说明读取结束
if
n
==
0
{
break
}
//读取到最终的缓冲区中
data
=
append
(
data
,
buf
[
:
n
]
...
)
size
=
size
+
n
if
size
>
Settings
.
MaxSize
+
1024
*
2
{
break
// 超过大小
}
}
str
:=
string
(
data
)
return
str
,
size
,
nil
}
/*
* 判断文件大小是否超过阈值(threshold,单位B)
*/
...
...
@@ -124,7 +161,7 @@ func DBupdate(c echo.Context, info *Upload) (string, string) {
sid
,
_
=
newAuthenticate
(
sid
,
url
,
url
,
info
.
Expiration
,
true
)
if
info
.
MaxView
==
0
{
//num,_=strconv.ParseUint(GetSetting("maxDefaultAccess"), 10, 64)
info
.
MaxView
=
30
// 设置最大默认可访问次数
info
.
MaxView
=
uint
(
Settings
.
MaxDefaultView
)
// 设置最大默认可访问次数
}
//model.Savetext(info.Content, info.MaxView, info.Passwd, info.Expiration, url, info.Type, info.Name,info.HighLight)
return
sid
,
url
...
...
@@ -144,8 +181,8 @@ func timeAssign(oriTime time.Time) time.Time {
// ===========sumdry=======================
// 从config读取数据
func
GetSetting
(
key
string
)
(
value
string
)
{
// config
// 从配置文件里面读取设置
func
InitSettings
()
{
viper
.
SetConfigName
(
"config"
)
viper
.
SetConfigType
(
"yaml"
)
viper
.
AddConfigPath
(
"./"
)
...
...
@@ -155,7 +192,18 @@ func GetSetting(key string) (value string) {
}
// connect to database
logInfo
:=
viper
.
GetStringMapString
(
"setting"
)
return
logInfo
[
key
]
Settings
.
Url
=
logInfo
[
"url"
]
if
Settings
.
Url
==
""
{
logrus
.
Fatal
(
"can not get domain name from config"
)
}
Settings
.
MaxDefaultView
,
err
=
strconv
.
Atoi
(
logInfo
[
"maxdefaultview"
])
if
err
!=
nil
{
logrus
.
Fatal
(
err
)
}
Settings
.
MaxSize
,
err
=
strconv
.
Atoi
(
logInfo
[
"maxsize"
])
if
err
!=
nil
{
logrus
.
Fatal
(
err
)
}
}
/*
...
...
backend/app/controller/
interface
.go
→
backend/app/controller/
handler
.go
View file @
1898bea4
...
...
@@ -11,30 +11,6 @@ import (
"github.com/sirupsen/logrus"
)
type
Upload
struct
{
Content
string
`json:"Content" form:"Content" query:"Content"`
Passwd
string
`json:"Passwd" form:"Passwd" query:"Passwd"`
Name
string
`json:"Name" form:"Name" query:"Name"`
Type
string
`json:"Type" form:"Type" query:"Type"`
HighLight
bool
`json:"HighLight" form:"HighLight" query:"HighLight"`
Expiration
time
.
Time
`json:"Expiration" form:"Expiration" query:"Expiration"`
MaxAge
int
`json:"MaxAge" form:"MaxAge" query:"MaxAge"`
// 用户指定的时间期限
MaxView
uint
`json:"MaxView" form:"MaxView" query:"MaxView"`
// 文件最大可访问次数
//Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"`
}
type
Download
struct
{
Passwd
string
`json:"Passwd" form:"Passwd" query:"Passwd"`
Url
string
`json:"Url" form:"Url" query:"Url"`
}
type
Msg
struct
{
Name
string
Type
string
Content
string
}
func
Ping
(
c
echo
.
Context
)
error
{
// just test
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
""
,
"pong!"
)
...
...
@@ -86,16 +62,25 @@ func FileUp(c echo.Context) error {
return
err
}
info
.
Name
=
file
.
Filename
info
.
Content
,
err
=
FileRead
(
file
)
data
:=
new
(
Msg2
)
if
overflow
(
info
.
Content
,
8
*
1024
*
1024
)
{
info
.
Content
,
data
.
Size
,
_
=
FileRead
(
file
)
info
.
MaxView
=
uint
(
Settings
.
MaxDefaultView
)
if
data
.
Size
>
8
*
1024
*
1024
{
return
response
.
SendResponse
(
c
,
http
.
StatusForbidden
,
"error:文件上传失败: 文件大小超过8MB."
,
""
)
}
// 更新数据库
_
,
url
:=
DBupdate
(
c
,
info
)
// 不分配uid
_
,
data
.
Url
=
DBupdate
(
c
,
info
)
// 不分配uid
data
.
Name
=
info
.
Name
dataj
,
err
:=
json
.
Marshal
(
data
)
if
err
!=
nil
{
logrus
.
Println
(
err
)
return
err
}
//return response.SendResponse2(c, http.StatusOK, "文件上传成功", info.TextName, GetTextContentType(info.TextType), url)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"文件上传成功"
,
url
)
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
"文件上传成功"
,
string
(
dataj
)
)
}
/*
...
...
@@ -132,8 +117,8 @@ func Down(c echo.Context) error {
Data
.
Type
=
model
.
Find1
(
info
.
Url
,
"TextType"
)
// 文件类型
Data
.
Type
=
GetFileContentType
(
Data
.
Type
)
Data
.
Name
=
model
.
Find1
(
info
.
Url
,
"TextName"
)
data
,
_
:=
json
.
Marshal
(
Data
)
// 文件名
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
cookieMsg
+
"success"
,
data
)
// 返回数据
data
j
,
_
:=
json
.
Marshal
(
Data
)
// 文件名
return
response
.
SendResponse
(
c
,
http
.
StatusOK
,
cookieMsg
+
"success"
,
string
(
data
j
)
)
// 返回数据
case
2
:
return
response
.
SendResponse
(
c
,
http
.
StatusGone
,
cookieMsg
+
"error:内容过期"
,
""
)
//410
case
3
:
...
...
backend/app/controller/struct.go
0 → 100644
View file @
1898bea4
package
controller
import
(
"time"
)
type
Upload
struct
{
Content
string
`json:"Content" form:"Content" query:"Content"`
Passwd
string
`json:"Passwd" form:"Passwd" query:"Passwd"`
Name
string
`json:"Name" form:"Name" query:"Name"`
Type
string
`json:"Type" form:"Type" query:"Type"`
HighLight
bool
`json:"HighLight" form:"HighLight" query:"HighLight"`
Expiration
time
.
Time
`json:"Expiration" form:"Expiration" query:"Expiration"`
MaxAge
int
`json:"MaxAge" form:"MaxAge" query:"MaxAge"`
// 用户指定的时间期限
MaxView
uint
`json:"MaxView" form:"MaxView" query:"MaxView"`
// 文件最大可访问次数
//Expiry time.Time `json:"expiry"` // 有效期
//Content string `json:"content"`
}
type
Download
struct
{
Passwd
string
`json:"Passwd" form:"Passwd" query:"Passwd"`
Url
string
`json:"Url" form:"Url" query:"Url"`
}
type
Msg
struct
{
Name
string
Type
string
Content
string
}
type
Msg2
struct
{
Url
string
Name
string
Size
int
}
// 从config文件获取
type
Setting
struct
{
Url
string
// 配置的域名
MaxDefaultView
int
// 单文件默认最大可访问次数
MaxSize
int
// 文件最大可上传大小(单位B)
}
var
Settings
Setting
backend/app/init.go
View file @
1898bea4
...
...
@@ -21,7 +21,7 @@ func InitWebFramework() {
}
func
StartServer
()
{
e
.
Logger
.
Fatal
(
e
.
Start
(
controller
.
Get
Setting
(
"url"
)
))
// 启动服务,注意默认端口80不能省略
e
.
Logger
.
Fatal
(
e
.
Start
(
controller
.
Setting
s
.
Url
))
// 启动服务,注意默认端口80不能省略
//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")) // 启动服务,注意默认端口80不能省略,需要域名解析,config
}
...
...
backend/app/response/reponse.go
View file @
1898bea4
...
...
@@ -7,26 +7,9 @@ import (
)
type
Response
struct
{
Code
int
`json:"code"`
Msg
string
`json:"msg"`
Type
string
`json:"type"`
Data
interface
{}
`json:"data"`
}
type
Response2
struct
{
Code
int
`json:"code"`
Msg
string
`json:"msg"`
Name
string
`json:"fileName"`
Type
string
`json:"fileType"`
Url
string
`json:"url"`
}
type
Response3
struct
{
Code
int
`json:"code"`
Msg
string
`json:"msg"`
Name
string
`json:"name"`
Type
string
`json:"type"`
Data
interface
{}
`json:"data"`
Code
int
`json:"Code"`
Msg
string
`json:"Msg"`
Data
interface
{}
`json:"Data"`
}
func
SendResponse
(
c
echo
.
Context
,
code
int
,
msg
string
,
data
...
interface
{})
error
{
...
...
@@ -37,26 +20,6 @@ func SendResponse(c echo.Context, code int, msg string, data ...interface{}) err
})
}
func
SendResponse2
(
c
echo
.
Context
,
code
int
,
msg
string
,
name
string
,
typ
string
,
url
string
,
data
...
interface
{})
error
{
return
c
.
JSON
(
http
.
StatusOK
,
Response2
{
Code
:
code
,
Msg
:
msg
,
Name
:
name
,
Type
:
typ
,
Url
:
url
,
})
}
func
SendResponse3
(
c
echo
.
Context
,
code
int
,
msg
string
,
name
string
,
typ
string
,
data
...
interface
{})
error
{
return
c
.
JSON
(
http
.
StatusOK
,
Response3
{
Code
:
code
,
Msg
:
msg
,
Name
:
name
,
Type
:
typ
,
Data
:
data
,
})
}
// 返回err
func
SendResponse4
(
c
echo
.
Context
,
err
error
)
error
{
return
c
.
JSON
(
http
.
StatusBadRequest
,
err
.
Error
())
...
...
backend/main.go
View file @
1898bea4
...
...
@@ -7,11 +7,13 @@ package main
import
(
"backend/app"
"backend/app/controller"
"backend/model"
)
func
main
()
{
app
.
InitLogger
()
// 初始化logger设置
controller
.
InitSettings
()
// 从配置文件中获取设置
model
.
Init
()
app
.
InitWebFramework
()
app
.
StartServer
()
...
...
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