Skip to content

Commit b8512cd

Browse files
committed
update git ignore Readme.md
1 parent 9e4275d commit b8512cd

File tree

7 files changed

+40
-65
lines changed

7 files changed

+40
-65
lines changed

README.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ A guide for creating RESTful API with Golang and MySQL and Gin.
55
## Build and Usage
66

77
```bash
8-
go get "github.com/gin-gonic/gin"
9-
go get "github.com/go-sql-driver/mysql"
10-
go get "gopkg.in/yaml.v2"
11-
go get "github.com/appleboy/gin-jwt/v2"
8+
go mod tidy
9+
go mod verify
1210
go build
1311
```
1412

@@ -31,10 +29,18 @@ mysql:
3129
user: "root"
3230
pwd: "root"
3331
db: "test"
32+
enable_auth: "true"
3433
auth_table: "auth"
3534
auth_name: "username"
3635
auth_pwd: "passwd"
3736
```
37+
for security all insert / delete / update operation must login
38+
so if you want to insert / delete / update data please enable auth
39+
40+
you have no permission to read or modify any data from ```auth_table```
41+
42+
TODO: Make the fields corresponding to ```auth_name``` and ```auth_pwd``` configurable
43+
3844

3945
## Features
4046

auth/jwt-auth.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,13 @@ var identityKey = "username"
2424
func GetJWTMiddleware() *jwt.GinJWTMiddleware {
2525
if jwtMiddleware == nil {
2626
jwtMiddleware = &jwt.GinJWTMiddleware{
27-
Realm: "testzone",
28-
Key: []byte("secretkey"),
29-
Timeout: time.Hour,
30-
MaxRefresh: time.Hour,
31-
SendCookie: true,
32-
CookieName: "jwt",
33-
SigningAlgorithm: "RS256",
34-
PrivKeyFile: "jwtRS256.key",
35-
PubKeyFile: "jwtRS256.key.pub",
36-
IdentityKey: identityKey,
27+
Realm: "testzone",
28+
Key: []byte("secretkey"),
29+
Timeout: time.Hour,
30+
MaxRefresh: time.Hour,
31+
SendCookie: true,
32+
CookieName: "jwt",
33+
IdentityKey: identityKey,
3734
PayloadFunc: func(data interface{}) jwt.MapClaims {
3835
if v, ok := data.(string); ok {
3936
return jwt.MapClaims{

conf.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mysql:
44
user: "root"
55
pwd: "root"
66
db: "huginn"
7+
enable_auth: "true"
78
auth_table: "auth"
89
auth_name: "username"
910
auth_pwd: "passwd"

conf/conf.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ type Conf struct {
1111
MYSQLCONF MysqlConf `yaml:"mysql"`
1212
}
1313
type MysqlConf struct {
14-
Host string `yaml:"host,omitempty"`
15-
User string `yaml:"user,omitempty"`
16-
Pwd string `yaml:"pwd,omitempty"`
17-
DB string `yaml:"db,omitempty"`
18-
Port string `yaml:"port,omitempty"`
19-
AuthTable string `yaml:"auth_table,omitempty"`
20-
AuthName string `yaml:"auth_name,omitempty"`
21-
AuthPwd string `yaml:"auth_pwd,omitempty"`
14+
Host string `yaml:"host,omitempty"`
15+
User string `yaml:"user,omitempty"`
16+
Pwd string `yaml:"pwd,omitempty"`
17+
DB string `yaml:"db,omitempty"`
18+
Port string `yaml:"port,omitempty"`
19+
EnableAuth string `yaml:"enable_auth,omitempty"`
20+
AuthTable string `yaml:"auth_table,omitempty"`
21+
AuthName string `yaml:"auth_name,omitempty"`
22+
AuthPwd string `yaml:"auth_pwd,omitempty"`
2223
}
2324

2425
var conf *Conf = nil
@@ -52,6 +53,9 @@ func GetAuthTableName() string {
5253
func GetAuthName() string {
5354
return GetConf().MYSQLCONF.AuthName
5455
}
56+
func GetEnableAuth() bool {
57+
return GetConf().MYSQLCONF.EnableAuth == "true"
58+
}
5559
func GetAuthPwd() string {
5660
return GetConf().MYSQLCONF.AuthPwd
5761
}

jwtRS256.key

Lines changed: 0 additions & 27 deletions
This file was deleted.

jwtRS256.key.pub

Lines changed: 0 additions & 9 deletions
This file was deleted.

mysql-restful-server.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ func main() {
1818
authMiddleware := auth.GetJWTMiddleware()
1919
router.GET("/api/v1/:table/:id", query.QueryDetail)
2020
router.GET("/api/v1/:table", query.QueryList)
21-
router.POST("/login", authMiddleware.LoginHandler)
22-
auths := router.Group("/api")
23-
auths.Use(authMiddleware.MiddlewareFunc())
24-
{
25-
auths.DELETE("/v1/:table/:id", query.DeleteDetail)
26-
auths.POST("/v1/:table", query.NewData)
27-
auths.GET("/refresh_token", authMiddleware.RefreshHandler)
21+
if conf.GetEnableAuth() {
22+
router.POST("/login", authMiddleware.LoginHandler)
23+
auths := router.Group("/api")
24+
auths.Use(authMiddleware.MiddlewareFunc())
25+
{
26+
auths.DELETE("/v1/:table/:id", query.DeleteDetail)
27+
auths.POST("/v1/:table", query.NewData)
28+
auths.GET("/refresh_token", authMiddleware.RefreshHandler)
29+
}
2830
}
31+
2932
router.Run(":8989")
3033

3134
}

0 commit comments

Comments
 (0)