Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
际垚 王
ShortLink
Commits
2561ee4a
Commit
2561ee4a
authored
Dec 16, 2022
by
Xiaokang Shen
💛
Browse files
updated on 1216, complete the controller
parent
92a1804a
Changes
8
Hide whitespace changes
Inline
Side-by-side
app/controller/foo.go
0 → 100644
View file @
2561ee4a
package
controller
import
(
"go-svc-tpl/utils"
"net/http"
"time"
"github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
)
// set token into cookie
func
setToken
(
c
echo
.
Context
,
token
string
)
{
cookie
:=
new
(
http
.
Cookie
)
cookie
.
Name
=
"token"
cookie
.
Value
=
token
cookie
.
Expires
=
time
.
Now
()
.
Add
(
24
*
time
.
Hour
)
cookie
.
Path
=
"/"
// set the path, or it will be /user
c
.
SetCookie
(
cookie
)
logrus
.
Info
(
"set token "
+
token
)
}
// get token from cookie
// return "" if not found
func
GetToken
(
c
echo
.
Context
)
string
{
cookie
,
_
:=
c
.
Cookie
(
"token"
)
// 没有错误处理(?)
if
cookie
==
nil
{
return
""
}
return
cookie
.
Value
}
// get the username by token
// return username "default" if no user is found
func
getName
(
c
echo
.
Context
)
string
{
token
:=
GetToken
(
c
)
if
token
==
""
{
return
"default"
}
claims
,
_
:=
utils
.
ParseToken
(
token
)
if
claims
==
nil
{
return
"default"
}
return
claims
.
Username
}
app/controller/handler.go
View file @
2561ee4a
...
...
@@ -30,7 +30,6 @@ func Login(c echo.Context) error {
email
:=
c
.
FormValue
(
"email"
)
pwd
:=
c
.
FormValue
(
"pwd"
)
data
:=
make
(
map
[
string
]
interface
{})
var
msg
string
var
code
int
...
...
@@ -46,13 +45,13 @@ func Login(c echo.Context) error {
}
else
{
code
=
0
msg
=
"generate token successfully"
data
[
"token"
]
=
token
setToken
(
c
,
token
)
}
}
else
{
code
=
1
msg
=
"user not exist"
}
return
response
.
SendResponse
(
c
,
code
,
msg
,
data
)
return
response
.
SendResponse
(
c
,
code
,
msg
,
""
)
}
// by frontend ?
...
...
@@ -176,10 +175,3 @@ func Pause(c echo.Context) error {
}
return
response
.
SendResponse
(
c
,
0
,
"pause succesfully"
,
""
)
}
// get the username by token
func
getName
(
c
echo
.
Context
)
string
{
token
:=
c
.
FormValue
(
"token"
)
claims
,
_
:=
utils
.
ParseToken
(
token
)
return
claims
.
Username
}
app/controller/model.go
deleted
100644 → 0
View file @
92a1804a
package
controller
type
User
struct
{
Username
string
`json:"username"`
Password
string
`json:"password"`
}
app/midware/init.go
View file @
2561ee4a
package
midware
import
(
"go-svc-tpl/app/controller"
"go-svc-tpl/app/response"
"go-svc-tpl/utils"
"net/http"
...
...
@@ -13,14 +14,14 @@ import (
// validate token
func
Auth
(
next
echo
.
HandlerFunc
)
echo
.
HandlerFunc
{
return
func
(
c
echo
.
Context
)
error
{
token
:=
c
.
FormValue
(
"t
oken
"
)
token
:=
c
ontroller
.
GetT
oken
(
c
)
var
code
int
var
msg
string
if
token
==
""
{
code
=
0
// no login but can use
msg
=
"not login yet"
logrus
.
Info
(
"not login yet"
)
}
else
{
claims
,
err
:=
utils
.
ParseToken
(
token
)
if
err
!=
nil
{
...
...
app/response/response.go
View file @
2561ee4a
...
...
@@ -9,7 +9,7 @@ import (
type
Response
struct
{
Code
int
`json:"code"`
Msg
string
`json:"msg"`
Data
interface
{}
`json:"
msg
"`
Data
interface
{}
`json:"
data
"`
}
func
SendResponse
(
c
echo
.
Context
,
code
int
,
msg
string
,
data
...
interface
{})
error
{
...
...
app/routes.go
View file @
2561ee4a
...
...
@@ -2,13 +2,15 @@ package app
import
(
"go-svc-tpl/app/controller"
"go-svc-tpl/app/midware"
"go-svc-tpl/app/response"
"github.com/labstack/echo/v4"
echoSwagger
"github.com/swaggo/echo-swagger"
)
func
ping
(
c
echo
.
Context
)
error
{
return
c
.
String
(
200
,
"p
ong!"
)
return
response
.
SendResponse
(
c
,
0
,
"ok"
,
"P
ong!"
)
}
func
addRoutes
()
{
...
...
@@ -18,13 +20,15 @@ func addRoutes() {
user
:=
e
.
Group
(
"user"
)
user
.
POST
(
"/register"
,
controller
.
Register
)
user
.
Use
(
midware
.
Auth
)
user
.
POST
(
"/login"
,
controller
.
Login
)
user
.
POST
(
"/logout"
,
controller
.
Logout
)
user
.
POST
(
"/info"
,
controller
.
Info
)
user
.
POST
(
"/record/get"
,
controller
.
GetRecord
)
user
.
POST
(
"/url/get"
,
controller
.
GetUrl
)
user
.
POST
(
"/info"
,
controller
.
Info
,
midware
.
Auth
)
user
.
POST
(
"/record/get"
,
controller
.
GetRecord
,
midware
.
Auth
)
user
.
POST
(
"/url/get"
,
controller
.
GetUrl
,
midware
.
Auth
)
url
:=
e
.
Group
(
"url"
)
url
.
Use
(
midware
.
Auth
)
url
.
POST
(
"/create"
,
controller
.
Create
)
url
.
POST
(
"/query"
,
controller
.
Query
)
url
.
POST
(
"/update"
,
controller
.
Update
)
...
...
model/fo.go
View file @
2561ee4a
package
model
import
"fmt"
// 用于controller测试
type
User
struct
{
Email
string
`json:"email"`
Email
string
`json:"email"`
Password
string
`json:"password"`
}
type
ShortLink
struct
{
Origin
string
`
json
:"origin"`
Short
string
`
json
:"short"`
Comment
string
`
json
:"comment"`
StartTime
int64
`
json
:"startTime`
ExpireTime
int64
`
json
:"expireTime`
Origin
string
`
form
:"origin"`
Short
string
`
form
:"short"`
Comment
string
`
form
:"comment"`
StartTime
int64
`
form
:"startTime`
ExpireTime
int64
`
form
:"expireTime`
}
// 创建的那五个参数设置form tag
type
UpdateData
struct
{
Origin
string
`
json
:"origin"`
Comment
string
`
json
:"comment"`
StartTime
int64
`
json
:"startTime`
ExpireTime
int64
`
json
:"expireTime`
Origin
string
`
form
:"origin"`
Comment
string
`
form
:"comment"`
StartTime
int64
`
form
:"startTime`
ExpireTime
int64
`
form
:"expireTime`
}
// 修改的那四个参数设置form tag
func
RegisterUser
(
name
,
email
,
pwd
string
)
error
{
...
...
@@ -27,7 +30,7 @@ func CheckAuth(email, pwd string) bool { // 检查是否有这个用户,密码
// 这三个返回一些信息的类型看数据库实现
func
GetUser
(
name
string
)
(
User
,
error
)
{
return
User
{
"1234@zju.edu.cn"
},
nil
return
User
{
"1234@zju.edu.cn"
,
"114514"
},
nil
}
func
GetRecord
(
name
string
)
(
interface
{},
error
)
{
return
ShortLink
{
...
...
@@ -44,9 +47,11 @@ func GetUrl(name string) (interface{}, error) {
// 意义显然x
func
CreateLink
(
name
string
,
link
ShortLink
)
(
int
,
error
)
{
fmt
.
Println
(
name
)
return
1
,
nil
}
func
QueryLink
(
name
string
,
id
int
)
(
ShortLink
,
error
)
{
fmt
.
Printf
(
"%s%d
\n
"
,
name
,
id
)
return
ShortLink
{
Origin
:
"1145141919"
,
Short
:
"114"
,
...
...
@@ -56,12 +61,15 @@ func QueryLink(name string, id int) (ShortLink, error) {
},
nil
}
func
UpdateLink
(
name
string
,
data
UpdateData
)
error
{
fmt
.
Printf
(
"%s%+v
\n
"
,
name
,
data
)
return
nil
}
func
DeleteLink
(
name
string
,
id
int
)
error
{
fmt
.
Printf
(
"%s%d
\n
"
,
name
,
id
)
return
nil
}
func
PauseLink
(
name
string
,
id
int
)
error
{
fmt
.
Printf
(
"%s%d
\n
"
,
name
,
id
)
return
nil
}
...
...
utils/token.go
View file @
2561ee4a
...
...
@@ -36,7 +36,7 @@ func GenToken(username, password string) (string, error) {
// take token into Claims{}, and then can get username and password
func
ParseToken
(
token
string
)
(
*
Claims
,
error
)
{
tokenClaims
,
err
:=
jwt
.
ParseWithClaims
(
token
,
Claims
{},
func
(
t
*
jwt
.
Token
)
(
interface
{},
error
)
{
tokenClaims
,
err
:=
jwt
.
ParseWithClaims
(
token
,
&
Claims
{},
func
(
t
*
jwt
.
Token
)
(
interface
{},
error
)
{
return
jwtSecret
,
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