Created
September 4, 2017 10:50
-
-
Save vuongngo/88402572dae15021e1ebfe94cbae74cb 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 api | |
import ( | |
"log" | |
"encoding/json" | |
"net/http" | |
"app/repo" | |
"app/worker" | |
"app/model" | |
"github.com/asaskevich/govalidator" | |
"github.com/gorilla/mux" | |
"github.com/mholt/binding" | |
) | |
type API struct { | |
Repo *repo.Repo | |
Nsq *workers.NSQ | |
} | |
func (api *API) PublishNSQMes(topic string, mes []byte) { | |
producer, err := api.Nsq.CreateProducer() | |
if err != nil { | |
log.Println(err) | |
} | |
err = producer.Publish(topic, mes) | |
if err != nil { | |
log.Println(err) | |
} | |
producer.Stop() | |
} | |
/** | |
* GET /api/v1/job/:id | |
*/ | |
func (api *API) GetJob(w http.ResponseWriter, r *http.Request) { | |
vars := mux.Vars(r) | |
jobId := vars["id"] | |
job, err := api.Repo.GetJob(jobId) | |
if err != nil { | |
w.WriteHeader(http.StatusNotFound) | |
w.Write([]byte(err.Error())) | |
return | |
} | |
w.Header().Set("Content-type", "application/json") | |
json.NewEncoder(w).Encode(job) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment