func runAPI(wg *sync.WaitGroup) { mux := goji.NewMux() // List all webhooks mux.HandleFuncC( pat.Get("/webhooks"), func(ctx context.Context, w http.ResponseWriter, r *http.Request) { ui := uhttp.UI{ctx, r, w} kase := usecase.ListWebhooks{ DB: &drivers.DB, Out: &ui, } if err := kase.Exec(); err != nil { fail(err) } }, ) // Create a new webhook mux.HandleFuncC( pat.Post("/webhooks"), func(ctx context.Context, w http.ResponseWriter, r *http.Request) {}, ) // Delete a webhook mux.HandleFuncC( pat.Delete("/webhooks/:id"), func(ctx context.Context, w http.ResponseWriter, r *http.Request) {}, ) http.ListenAndServe("localhost:8000", mux) wg.Done() }
// Delete registers a `DELETE /resource/:id` handler for the resource func (res *Resource) Delete(storage store.Delete) { res.HandleFuncC( pat.Delete(patID), func(ctx context.Context, w http.ResponseWriter, r *http.Request) { res.deleteHandler(ctx, w, r, storage) }, ) res.addRoute(delete, patID) }
func main() { session, err := mgo.Dial("localhost") if err != nil { panic(err) } defer session.Close() session.SetMode(mgo.Monotonic, true) ensureIndex(session) mux := goji.NewMux() mux.HandleFuncC(pat.Get("/books"), allBooks(session)) mux.HandleFuncC(pat.Post("/books"), addBook(session)) mux.HandleFuncC(pat.Get("/books/:isbn"), bookByISBN(session)) mux.HandleFuncC(pat.Put("/books/:isbn"), updateBook(session)) mux.HandleFuncC(pat.Delete("/books/:isbn"), deleteBook(session)) http.ListenAndServe("localhost:8080", mux) }
func API() *goji.Mux { mux := goji.SubMux() // We pass the routes as relative to the point where the API router // will be mounted. The super-router will strip any prefix off for us. mux.HandleFuncC(pat.Get("/people"), api.ListPeople) mux.HandleFuncC(pat.Post("/people"), api.CreatePerson) mux.HandleFuncC(pat.Get("/people/:person"), api.GetPerson) mux.HandleFuncC(pat.Delete("/people/:person"), api.DeletePerson) // Add default 'not found' route that responds with JSON mux.HandleFunc(pat.New("/*"), func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(404) fmt.Fprint(w, `{"error":"not found"}`) }) return mux }
var Routes = struct { CreateUser, GetUser, GetAnimals, ListMoods, SetMood, GetMood, DeleteMood, ListConversations, CreateConversation, GetConversation, DeleteConversation, CreateLine, GetLine, DeleteLine *pat.Pattern }{ CreateUser: pat.Post("/users"), GetUser: pat.Get("/users/:id"), GetAnimals: pat.Get("/animals"), ListMoods: pat.Get("/moods"), SetMood: pat.Put("/moods/:mood"), GetMood: pat.Get("/moods/:mood"), DeleteMood: pat.Delete("/moods/:mood"), ListConversations: pat.Get("/conversations"), CreateConversation: pat.Post("/conversations"), GetConversation: pat.Get("/conversations/:conversation"), DeleteConversation: pat.Delete("/conversations/:conversation"), CreateLine: pat.Post("/conversations/:conversation/lines"), GetLine: pat.Get("/conversations/:conversation/lines/:line"), DeleteLine: pat.Delete("/conversations/:conversation/lines/:line"), } // App encapsulates the handlers for the saypi API type App struct { srv http.Handler closers []io.Closer
// New creates an App for the given configuration. func New(config *Configuration) (*App, error) { var app App db, err := buildDB(config.DBDSN, config.DBMaxIdle, config.DBMaxOpen) if err != nil { defer app.Close() return nil, err } app.closers = append(app.closers, db) ipQuota := throttled.RateQuota{throttled.PerMin(config.IPPerMinute), config.IPRateBurst} ipLimiter, err := buildLimiter(ipQuota) authCtrl, err := auth.New(config.UserSecret) if err != nil { defer app.Close() return nil, err } sayCtrl, err := say.New(db) if err != nil { defer app.Close() return nil, err } app.closers = append(app.closers, sayCtrl) // TODO: Proper not found handler privMux := goji.NewMux() privMux.UseC(metrics.WrapSubmuxC) privMux.UseC(authCtrl.WrapC) privMux.HandleFuncC(Routes.GetAnimals, sayCtrl.GetAnimals) privMux.HandleFuncC(Routes.ListMoods, sayCtrl.ListMoods) privMux.HandleFuncC(Routes.SetMood, sayCtrl.SetMood) privMux.HandleFuncC(Routes.GetMood, sayCtrl.GetMood) privMux.HandleFuncC(Routes.DeleteMood, sayCtrl.DeleteMood) privMux.HandleFuncC(pat.Get("/conversations"), sayCtrl.ListConversations) privMux.HandleFuncC(pat.Post("/conversations"), sayCtrl.CreateConversation) privMux.HandleFuncC(pat.Get("/conversations/:conversation"), sayCtrl.GetConversation) privMux.HandleFuncC(pat.Delete("/conversations/:conversation"), sayCtrl.DeleteConversation) privMux.HandleFuncC(pat.Post("/conversations/:conversation/lines"), sayCtrl.CreateLine) privMux.HandleFuncC(pat.Get("/conversations/:conversation/lines/:line"), sayCtrl.GetLine) privMux.HandleFuncC(pat.Delete("/conversations/:conversation/lines/:line"), sayCtrl.DeleteLine) mainMux := goji.NewMux() mainMux.HandleFuncC(Routes.CreateUser, authCtrl.CreateUser) mainMux.HandleFuncC(Routes.GetUser, authCtrl.GetUser) mainMux.HandleC(pat.New("/*"), privMux) mainMux.UseC(reqlog.WrapC) mainMux.UseC(respond.WrapPanicC) mainMux.UseC(metrics.WrapC) mainMux.Use(ipLimiter.RateLimit) app.srv = mainMux return &app, nil }
} var Routes = struct { CreateUser, GetUser, GetAnimals, ListMoods, SetMood, GetMood, DeleteMood *pat.Pattern }{ CreateUser: pat.Post("/users"), GetUser: pat.Get("/users/:id"), GetAnimals: pat.Get("/animals"), ListMoods: pat.Get("/moods"), SetMood: pat.Put("/moods/:mood"), GetMood: pat.Get("/moods/:mood"), DeleteMood: pat.Delete("/moods/:mood"), } // App encapsulates the handlers for the saypi API type App struct { srv http.Handler closers []io.Closer } // Close cleans up any resources used by the app such as database connections. func (a *App) Close() error { return closeAll(a.closers) } func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) { a.srv.ServeHTTP(w, r)
func (m *Mux) Delete(pattern string, handler goji.Handler, doc *Doc) { muxMap(m, "delete", pattern, handler, doc) m.webmux.HandleC(pat.Delete(pattern), handler) }
func main() { log.Println("localhost:4242") // Connection a la base de donnee initdatabase() defer database.Close() gob.Register(UserData{}) gob.Register(InscriptionForm{}) mux := goji.NewMux() mux.HandleFunc(pat.Get("/"), home) // Creation d'un compte mux.HandleFunc(pat.Get("/inscription"), inscription) mux.HandleFunc(pat.Post("/users"), postUsers) // Connexion Deconnexion utilisateur mux.HandleFunc(pat.Post("/login"), login) mux.HandleFunc(pat.Get("/logout"), logout) mux.HandleFunc(pat.Get("/me"), connectedUser) // User's interests Road mux.HandleFunc(pat.Post("/users/me/interests/"), postUsersInterests) mux.HandleFunc(pat.Get("/users/me/interests/"), getUsersInterests) mux.HandleFuncC(pat.Delete("/users/me/interests/:interestid"), deleteUsersInterests) // User's age Road mux.HandleFunc(pat.Put("/users/me/age/"), postUsersAge) mux.HandleFunc(pat.Get("/users/me/age/"), getUsersAge) // User's username Road mux.HandleFunc(pat.Put("/users/me/username/"), postUsersUsername) mux.HandleFunc(pat.Get("/users/me/username/"), getUsersUsername) // User's firstname Road mux.HandleFunc(pat.Put("/users/me/firstname/"), postUsersFirstName) mux.HandleFunc(pat.Get("/users/me/firstname/"), getUsersFirstName) // User's lastname Road mux.HandleFunc(pat.Put("/users/me/lastname/"), postUsersLastName) mux.HandleFunc(pat.Get("/users/me/lastname/"), getUsersLastName) // User's Bio Road mux.HandleFunc(pat.Put("/users/me/bio/"), uptdateUsersBio) // Public Profile mux.HandleFuncC(pat.Get("/users/:id"), publicProfile) mux.HandleFuncC(pat.Put("/users/:id/like/"), likeAnUser) mux.HandleFuncC(pat.Put("/users/:id/unlike/"), unlikeAnUser) // Notifications mux.HandleFuncC(pat.Put("/notifications/:id"), setReadNotifications) mux.HandleFunc(pat.Get("/notifications/"), getNotifications) //Matches mux.HandleFunc(pat.Get("/users/me/matches/"), getUsersMatches) // Interests mux.HandleFunc(pat.Post("/interests/"), postInterests) // Chat mux.HandleFuncC(pat.Get("/chat/:chatname"), RootHandler) mux.Handle(pat.Get("/sock"), websocket.Handler(SockServer)) // Static Files mux.HandleFunc(pat.Get("/css/*"), staticCssFiles) mux.HandleFunc(pat.Get("/js/*"), staticJsFiles) log.Fatal(http.ListenAndServe(LISTEN_PORT, mux)) }