承接上一篇go iris框架的文章, 最近想用它来写一个服务端。话不多说, 直接上代码。
首先创建main.go文件。
package main
import (
"database/sql"
"fmt"
"github.com/dgrijalva/jwt-go"
_ "github.com/go-sql-driver/mysql"
"github.com/iris-contrib/middleware/cors"
jwtmiddleware "github.com/iris-contrib/middleware/jwt"
"github.com/kataras/iris"
"time"
)
type Token struct {
Token string `json:"token"`
}
func myHandler(ctx iris.Context) {
user := ctx.Values().Get("jwt").(*jwt.Token)
ctx.JSON(user)
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}
func main() {
//打开数据库mytest
fmt.Println("open the database, myDatabase")
db, err := sql.Open("mysql", "数据库账号:数据库密码@tcp(数据库Ip:端口)/数据库名称?parseTime=true")
if err != nil {
fmt.Println(err)
}
//关闭数据库,db会被多个goroutine共享,可以不调用
defer db.Close()
//查询数据,指定字段名,返回sql.Rows结果集
rows, _ := db.Query("select USER_NAME,USER_PASS from taskusers")
id := 0
name := ""
for rows.Next() {
rows.Scan(&id, &name)
fmt.Println(id, name)
}
//查询数据,取所有字段
rows2, _ := db.Query("select * from taskusers")
//返回所有列
cols, _ := rows2.Columns()
//这里表示一行所有列的值,用[]byte表示
vals := make([][]byte, len(cols))
//这里表示一行填充数据
scans := make([]interface{}, len(cols))
//这里scans引用vals,把数据填充到[]byte里
for k, _ := range vals {
scans[k] = &vals[k]
}
i := 0
result := make(map[int]map[string]string)
for rows2.Next() {
//填充数据
rows2.Scan(scans...)
//每行数据
row := make(map[string]string)
//把vals中的数据复制到row中
for k, v := range vals {
key := cols[k]
//这里把[]byte数据转成string
row[key] = string(v)
}
//放入结果集
result[i] = row
i++
}
fmt.Println(result)
//查询一行数据
rows3 := db.QueryRow("select id,name from taskusers where id = ?", 1)
rows3.Scan(&id, &name)
fmt.Println(id, name)
app := iris.New()
crs := cors.New(cors.Options{
AllowedOrigins: []string{"*"}, // allows everything, use that to change the hosts.
AllowCredentials: true,
AllowedMethods: []string{"PUT", "PATCH", "GET", "POST", "OPTIONS"},
AllowedHeaders: []string{"Origin", "Authorization"},
ExposedHeaders: []string{"Accept", "Content-Type", "Content-Length", "Accept-Encoding", "X-CSRF-Token", "Authorization"},
})
v1 := app.Party("/api/v1", crs).AllowMethods(iris.MethodOptions) // <- important for the preflight.
{
v1.Post("/login", func(ctx iris.Context) {
token := jwt.New(jwt.SigningMethodHS256)
claims := make(jwt.MapClaims)
claims["exp"] = time.Now().Add(time.Hour * time.Duration(1)).Unix()
claims["iat"] = time.Now().Unix()
token.Claims = claims
tokenString, err := token.SignedString([]byte("My Secret"))
if err != nil {
}
response := Token{tokenString}
ctx.JSON(response)
})
v1.Get("/about", func(ctx iris.Context) {
ctx.JSON(result)
})
v1.Post("/send", func(ctx iris.Context) {
ctx.WriteString("sent")
})
v1.Put("/updated", func(ctx iris.Context) {
ctx.WriteString("updated")
})
v1.Delete("/deleted", func(ctx iris.Context) {
ctx.WriteString("deleted")
})
jwtHandler := jwtmiddleware.New(jwtmiddleware.Config{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return []byte("My Secret"), nil
},
SigningMethod: jwt.SigningMethodHS256,
})
v1.Use(jwtHandler.Serve)
v1.Get("/ping", myHandler)
}
app.Run(iris.Addr("localhost:3001"))
}
之后执在控制台执行go run main.go,执行的过程中可能会报找不到相应的包的错, 执行‘go get xxx’即可, 最后浏览器访问localhost:3001并加上相应路径即可。