func main() { // middleware middle := interpose.New() middle.Use(adaptors.FromNegroni(cors.New(cors.Options{ // CORS AllowedOrigins: []string{"*"}, }))) // router router := pat.New() middle.UseHandler(router) router.Get("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { url := r.URL.Query().Get("url") if url != "" { encoded := imgbase64.FromRemote(url) fmt.Fprint(w, encoded) } else { fmt.Fprint(w, "/?url=<your-image-url> returns the base64 data-URI of that image.") } })) // listen port := os.Getenv("PORT") if port == "" { port = "5000" } log.Print("listening...") http.ListenAndServe(":"+port, middle) }
func main() { var err error db, err = neoism.Connect(os.Getenv("NEO4J_URL")) if err != nil { log.Fatal(err) } // middleware middle := interpose.New() middle.Use(adaptors.FromNegroni(cors.New(cors.Options{ // CORS AllowedOrigins: []string{"*"}, }))) // router router := mux.NewRouter() router.StrictSlash(true) // redirects '/path' to '/path/' middle.UseHandler(router) // ~ // > routes router.HandleFunc("/rel/", func(w http.ResponseWriter, r *http.Request) { handlers.CreateRelationship(db, w, r) }).Methods("POST") router.HandleFunc("/eql/", func(w http.ResponseWriter, r *http.Request) { handlers.CreateEquality(db, w, r) }).Methods("POST") router.HandleFunc("/btw/", func(w http.ResponseWriter, r *http.Request) { handlers.RelationshipsBetween(db, w, r) }).Methods("GET") router.HandleFunc("/cluster.svg", func(w http.ResponseWriter, r *http.Request) { handlers.ViewRelationships(db, w, r) }).Methods("GET") // ~ // static files router.PathPrefix("/").Handler(http.FileServer(http.Dir("./static/"))) // ~ port := os.Getenv("PORT") if port == "" { port = "5000" } log.Print("listening...") http.ListenAndServe(":"+port, middle) }
func main() { router := mux.NewRouter() router.HandleFunc("/{user}", func(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "Welcome to the home page, %s!", mux.Vars(req)["user"]) }) mw := interpose.New() // Use logrus x := negroni.Handler(negronilogrus.NewMiddleware()) mw.Use(adaptors.FromNegroni(x)) // Apply the router. By adding it last, all of our other middleware will be // executed before the router, allowing us to modify headers before any // output has been generated. mw.UseHandler(router) // Launch and permit graceful shutdown, allowing up to 10 seconds for existing // connections to end graceful.Run(":3001", 10*time.Second, mw) }
func NegroniGzip(compression int) func(http.Handler) http.Handler { return adaptors.FromNegroni(gzip.Gzip(compression)) }
func NegroniLogrus() func(http.Handler) http.Handler { return adaptors.FromNegroni(negronilogrus.NewMiddleware()) }