Created
April 17, 2016 10:00
-
-
Save nguyenvanduocit/2f7e5766dd91de9437b2615f4d454d3a to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"github.com/nguyenvanduocit/gohoro" | |
"net/http" | |
"github.com/gorilla/mux" | |
"log" | |
"encoding/json" | |
"flag" | |
"fmt" | |
) | |
type Response struct { | |
Success bool `json:"success"` | |
Content string `json:"content"` | |
} | |
func DailyHoro(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
sign := vars["sign"] | |
result, err := gohoro.GetHoroscope(sign) | |
if err != nil { | |
json.NewEncoder(w).Encode(Response{Success:false, Content:"Can not get data."}) | |
}else if(result == ""){ | |
errorMessage := Response{Success:false, Content:"Sign not found"} | |
json.NewEncoder(w).Encode(errorMessage) | |
}else{ | |
json.NewEncoder(w).Encode(Response{Success:true, Content:result}) | |
} | |
} | |
func main() { | |
var ip, port string | |
flag.StringVar(&ip, "ip", "", "ip") | |
flag.StringVar(&port, "port", "8080", "Port") | |
flag.Parse() | |
address := fmt.Sprintf("%s:%s", ip, port) | |
router := mux.NewRouter().StrictSlash(true) | |
router.HandleFunc("/{sign}/daily", DailyHoro) | |
log.Fatal(http.ListenAndServe(address, router)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment