func main() { mux := http.NewServeMux() mux.HandleFunc("/favicon.ico", iconHandler) mux.HandleFunc("/", index) mux.HandleFunc("/message", message) n := negroni.New() n.Use(negroni.HandlerFunc(middlewareFirst)) n.Use(negroni.HandlerFunc(middlewareSecond)) n.UseHandler(mux) n.Run(":8080") }
func RunFunc() { r := render.New() mux, store := InitServerMux(r) n := negroni.Classic() n.Use(negroni.HandlerFunc(CookieMiddleware(store))) n.Use(negroni.HandlerFunc(RecoveryErr())) timeourHandler := http.TimeoutHandler(context.ClearHandler(mux), time.Duration(3*time.Second), timeoutErrMessage) n.UseHandler(timeourHandler) //All the middlerware we used l := log.New(os.Stdout, "[negroni] ", 0) l.Printf("listening on :80") server := http.Server{Addr: ":80", Handler: n} server.SetKeepAlivesEnabled(true) go MainMonitor() InitSchoolDB() InitCourseMap() InitSchoolStructs() l.Fatal(server.ListenAndServe()) }
func Create(port string, env string) http.Handler { serverPort = port r := mux.NewRouter().PathPrefix("/v1").Subrouter() r.Methods("GET").Path("/ws/app/{key}").HandlerFunc(controllers.Join) appsRouter := mux.NewRouter().PathPrefix("/v1/apps/{appId}").Subrouter() appsRouter.Methods("POST").Path("/events").HandlerFunc(controllers.CreateEvent) r.PathPrefix("/apps/{appId}").Handler(negroni.New( negroni.HandlerFunc(AuthMiddleware), negroni.Wrap(appsRouter), )) n := negroni.New(negroni.HandlerFunc(BaseMiddleware)) if env == "dev" { n.Use(negroni.HandlerFunc(LoggerMiddleware)) } n.UseHandler(r) return n }
func LimitHandler(limiter *config.Limiter) negroni.HandlerFunc { return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) { httpError := tollbooth.LimitByRequest(limiter, r) if httpError != nil { w.Header().Add("Content-Type", limiter.MessageContentType) /* RHMOD Fix for error "http: multiple response.WriteHeader calls" Reverse the sequence of the functions calls w.WriteHeader() and w.Write() */ w.WriteHeader(httpError.StatusCode) w.Write([]byte(httpError.Message)) return } else { next(w, r) } }) }
func NewServer(sh chan bool) { serverOnce.Do(func() { shutdownCh = sh flag.Parse() go hub.run() r := mux.NewRouter() var buffer bytes.Buffer buffer.WriteString(":") buffer.WriteString(core.NewConfig().HttpConfig.Port) jwtMiddleware := jwtmiddleware.New(jwtmiddleware.Options{ ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) { return []byte(core.NewConfig().JWTConfig.Secret), nil }, Debug: true, Extractor: jwtmiddleware.FromFirst(jwtmiddleware.FromAuthHeader, func(r *http.Request) (string, error) { c, err := r.Cookie("Authentication") if err != nil { return "", err } authHeaderParts := strings.Split(c.Value, " ") if len(authHeaderParts) != 2 || strings.ToLower(authHeaderParts[0]) != "bearer" { return "", fmt.Errorf("Authorization header format must be Bearer {token}") } return authHeaderParts[1], nil }), SigningMethod: jwt.SigningMethodHS256, }) n := negroni.Classic() for _, v := range routes { if v.auth { r.Handle(v.path, negroni.New( negroni.HandlerFunc(jwtMiddleware.HandlerWithNext), negroni.Wrap(http.HandlerFunc(v.handleFunc)), )).Methods(v.method) } else { r.HandleFunc(v.path, v.handleFunc).Methods(v.method) } } n.UseHandler(r) server = &graceful.Server{ Timeout: 10 * time.Second, Server: &http.Server{ Addr: buffer.String(), Handler: n, }, BeforeShutdown: func() bool { log.Println("bye") return true }, } err := server.ListenAndServe() if err != nil { log.Fatal("ListenAndServe: ", err) } }) }