-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ourgo/master
Merged
- Loading branch information
Showing
7 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package sha256 | ||
|
||
import ( | ||
"encoding/hex" | ||
"crypto/sha256" | ||
) | ||
|
||
// 加密密码 | ||
func ToHash(s string,k string) string { | ||
hanher := sha256.New() | ||
// 写入字符串 | ||
hanher.Write([]byte(s)) | ||
// 加盐 | ||
hashed := hanher.Sum([]byte(k)) | ||
// 返回16进位字符串 | ||
return hex.EncodeToString(hashed) | ||
} | ||
|
||
// 密码对比 | ||
func ReturnBool(s ,k ,p string) bool { | ||
// 加密 | ||
hashed := ToHash(s,k) | ||
// 返回 | ||
if hashed==p { | ||
return true | ||
}else{ | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package user | ||
|
||
import ( | ||
"labix.org/v2/mgo" | ||
"labix.org/v2/mgo/bson" | ||
) | ||
|
||
type User struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
|
||
|
||
func FindOne(m map[string]string) (bson.M,map[string]string) { | ||
|
||
dialInfo := &mgo.DialInfo{ | ||
Addrs:[]string{"127.0.0.1:27027"}, | ||
Database:"test", | ||
} | ||
|
||
// 连接数据库 | ||
//Client,_ := mgo.Dial("mongodb://ourgo:[email protected]:27027/ourgo") | ||
Client,_ := mgo.DialWithInfo(dialInfo) | ||
// 关闭数据库 | ||
defer Client.Close() | ||
// 设置连接模式 | ||
//Client.SetMode(mgo.Monotonic,true) | ||
// 切换连接的数据库DB()和集合C() | ||
users := Client.DB("ourgo").C("users") | ||
// 定义接收 | ||
result := &User{} | ||
// 查找数据 | ||
err := users.Find(m).One(result) | ||
// 错误处理 | ||
if err != nil{ | ||
merr := make(map[string]string) | ||
merr["code"]="0" | ||
merr["msg"]="没有该数据" | ||
return nil,merr | ||
} | ||
// 定义数据转换结构 | ||
mm := bson.M{} | ||
// 转换数据 | ||
data,_ := bson.Marshal(result) | ||
// 转换为map | ||
bson.Unmarshal(data,mm) | ||
// 返回数据 | ||
return mm,nil | ||
} | ||
|
||
func user() { | ||
mm := make(map[string]string) | ||
mm["username"]="xiaoli" | ||
FindOne(mm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package user | ||
|
||
import ( | ||
"net/http" | ||
"../../model/user" | ||
"fmt" | ||
) | ||
|
||
func Router(w http.ResponseWriter,r *http.Request) { | ||
// 向页面输出 | ||
w.Write([]byte("hello wego")) | ||
// 定义map数据(模拟查询) | ||
m := make(map[string]string) | ||
m["username"]="xiaoli" | ||
// 查询并得到返回数据 | ||
mm,_ := user.FindOne(m) | ||
// 输出返回数据 | ||
fmt.Println(mm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package web | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func Index(w http.ResponseWriter,r *http.Request) { | ||
w.Write([]byte("hello index")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package main | ||
|
||
import ( | ||
//"labix.org/v2/mgo/bson" | ||
"fmt" | ||
mapToStruct "github.com/goinggo/mapstructure" | ||
"labix.org/v2/mgo/bson" | ||
) | ||
|
||
type user struct { | ||
Username string | ||
Password string | ||
} | ||
|
||
func main() { | ||
|
||
mm := make(map[string]string) | ||
mm["username"]="xiaoli" | ||
|
||
u := &user{} | ||
bm := bson.M{} | ||
mapToStruct.Decode(mm,u) | ||
bs1,_ := bson.Marshal(u) | ||
bson.Unmarshal(bs1,bm) | ||
fmt.Println(bm) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"fmt" | ||
) | ||
|
||
// 中间件 | ||
func logMiddle(next http.Handler) http.Handler{ | ||
return http.HandlerFunc(func(w http.ResponseWriter,r *http.Request){ | ||
fmt.Println("middle log") | ||
next.ServeHTTP(w,r) | ||
}) | ||
} | ||
// 中间件2 | ||
func hook(next http.Handler) http.Handler{ | ||
return http.HandlerFunc(func(w http.ResponseWriter,r *http.Request) { | ||
fmt.Println("hook log",r.URL.Path) | ||
next.ServeHTTP(w,r) | ||
}) | ||
} | ||
|
||
// 首页 | ||
func index(w http.ResponseWriter,r *http.Request){ | ||
if r.URL.Path != "/"{ | ||
return | ||
} | ||
w.Write([]byte("hello")) | ||
fmt.Println("index log") | ||
} | ||
|
||
func main() { | ||
|
||
http.Handle("/api",logMiddle(hook(http.HandlerFunc(index)))) | ||
http.Handle("/",hook(http.HandlerFunc(index))) | ||
|
||
http.ListenAndServe(":8000",nil) | ||
} |