Skip to content

Instantly share code, notes, and snippets.

@vuongngo
Created September 4, 2017 10:42
Show Gist options
  • Save vuongngo/26d0ba540fc585deed853a853fb212d2 to your computer and use it in GitHub Desktop.
Save vuongngo/26d0ba540fc585deed853a853fb212d2 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"os"
"app/api"
"app/db"
"app/repo"
"app/worker"
"app/ws"
"github.com/gorilla/mux"
)
var (
svc *api.API
websocket *ws.WebSocket
)
func init() {
//ws hub
hub := ws.NewHub()
go hub.Run()
websocket = &ws.WebSocket{
Hub: hub,
}
// Initialize connetion pool with mongo
mongo := &db.MONGO{
Uri: os.Getenv("MONGO_URI"),
Database: os.Getenv("MONGO_DATABASE"),
}
mongo.Dial()
sharedRepo := &repo.Repo{
Mongo: mongo,
}
//NSQ
nSQ := workers.InitNSQ(os.Getenv("NSQ_URI"), hub, sharedRepo)
err := nSQ.CreateHandler(nSQ.JobMatchedHandler, "job_matched", "analyze")
if err != nil {
panic(err)
}
// API share the same connection pool
svc = &api.API{
Repo: sharedRepo,
Nsq: nSQ,
}
}
type Route struct {
Name string
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
type Routes []Route
func NewRouter() *mux.Router {
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
router.
Methods(route.Method).
Path(route.Pattern).
Name(route.Name).
Handler(route.HandlerFunc)
}
return router
}
var routes = Routes{
Route{
"WS",
"GET",
"/ws",
websocket.ServeWs,
},
Route{
"job",
"GET",
"/job/{id}",
svc.GetJob,
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment