使用golang的http模块构建redis读写查api
//xiaorui.ccpackage main
import(
"fmt"
"net/http"
"io/ioutil"
"log"
"time"
"redis"
)
//xiaorui.cc
const AddForm = `
Name:
Age:
`
const setform = `
key:
value:
`
func Handler( w http.ResponseWriter,r *http.Request ){
path := r.URL.Path
if path == "favicon.ico"{
http.NotFound(w, r)
return
}
if path == ""{
path = "index.html"
}
contents,err:= ioutil.ReadFile( path )
if err !=nil{
fmt.Fprintf( w,"404" )
return
}
fmt.Fprintf( w,"%s\n",contents )
}
func Add( w http.ResponseWriter,r *http.Request ){
name := r.FormValue("name")
age := r.FormValue("age")
if name == "" || age == "" {
fmt.Fprint(w, AddForm)
return
}
fmt.Fprintf(w, "Save : Your name is%s , You age is %s",name,age)
}
func redisset( w http.ResponseWriter,r *http.Request ){
key := r.FormValue("key")
value := r.FormValue("value")
if key == "" || value == "" {
fmt.Fprint(w, setform)
return
}
spec := redis.DefaultSpec().Db(0).Password("");
client, e := redis.NewSynchClientWithSpec (spec);
if e != nil { log.Println ("服务器连接有异常", e); return }
inva := []byte(value)
client.Set(key, inva);
fmt.Fprintf(w, "哥们,你输入的key%s 和value%s 已经插入到redis里面了",key,key)
}
func redisget( w http.ResponseWriter,r *http.Request ){
key := r.FormValue("key")
if key == "" {
fmt.Fprint(w, setform)
return
}
spec := redis.DefaultSpec().Db(0).Password("");
client, e := redis.NewSynchClientWithSpec (spec);
if e != nil { log.Println ("服务器连接有异常", e); return }
value, e := client.Get(key);
fmt.Fprintf(w, "哥们,你要查询的key%s 和value%s ",key,value)
}
func valueget(w http.ResponseWriter, r *http.Request) {
params := r.URL.Query()
user := params.Get("user")
fmt.Fprintf(w, "you are get user %s", user)
}
func main(){
http.HandleFunc( "/",Handler)
http.HandleFunc( "/add",Add)
http.HandleFunc( "/redisset",redisset)
http.HandleFunc( "/redisget",redisget)
http.HandleFunc( "/valueget",valueget)
s := &http.Server{
Addr: ":80",
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
MaxHeaderBytes: 1
页:
[1]