Skip to content

Instantly share code, notes, and snippets.

@nguyenvanduocit
Created April 17, 2016 10:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nguyenvanduocit/2f7e5766dd91de9437b2615f4d454d3a to your computer and use it in GitHub Desktop.
Save nguyenvanduocit/2f7e5766dd91de9437b2615f4d454d3a to your computer and use it in GitHub Desktop.
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