Skip to content

Commit

Permalink
Merge pull request #2 from ourgo/master
Browse files Browse the repository at this point in the history
Merged
  • Loading branch information
Jancd committed Aug 17, 2017
2 parents 698c233 + de134b7 commit 1eba458
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 9 deletions.
29 changes: 29 additions & 0 deletions commons/sha256/sha256.go
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
}
}
23 changes: 14 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,26 @@ package main
import (
"fmt"
"net/http"
"./router/user"
"./router/web"
)

// 展示首页
func showIndex(write http.ResponseWriter,req *http.Request){
// 解析路由参数
req.ParseForm()

//输出hello wego
fmt.Fprintf(write,"hello wego")

// 定义中间件(参数为http.HandlerFunc 不是 http.HandleFunc)
// 该函数返回一个http.Handler函数
//
func middleWeare(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter,r *http.Request){
// do somthings
fmt.Println("middle logs")
// 传递给下一个http接收(下一个路由处理函数)
next.ServeHTTP(w,r)
})
}

func main(){
/*main code*/
http.HandleFunc("/",showIndex)
http.Handle("/user",middleWeare(http.HandlerFunc(user.Router)))
http.Handle("/",middleWeare(http.HandlerFunc(web.Index)))
fmt.Println("服务启动在8000端口")
http.ListenAndServe(":8000",nil)

Expand Down
56 changes: 56 additions & 0 deletions model/user/user.go
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)
}
19 changes: 19 additions & 0 deletions router/user/user.go
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)
}
9 changes: 9 additions & 0 deletions router/web/index.go
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"))
}
26 changes: 26 additions & 0 deletions test/bson.go
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)
}
38 changes: 38 additions & 0 deletions test/http.go
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)
}

0 comments on commit 1eba458

Please sign in to comment.