func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"http://foo.com"}, }) handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"hello\": \"world\"}")) }) http.ListenAndServe(":8080", c.Handler(handler)) }
func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"http://foo.com"}, }) goji.Use(c.Handler) goji.Get("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"hello\": \"world\"}")) }) goji.Serve() }
func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE"}, AllowCredentials: true, }) h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"hello\": \"world\"}")) }) http.ListenAndServe(":8080", c.Handler(h)) }
func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"http://foo.com"}, }) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"hello\": \"world\"}")) }) chain := alice.New(c.Handler).Then(mux) http.ListenAndServe(":8080", chain) }
func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"http://foo.com"}, }) m := martini.Classic() m.Use(render.Renderer()) m.Use(c.HandlerFunc) m.Get("/", func(r render.Render) { r.JSON(200, map[string]interface{}{"hello": "world"}) }) m.Run() }
func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"http://foo.com"}, }) mux := http.NewServeMux() mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") w.Write([]byte("{\"hello\": \"world\"}")) }) n := negroni.Classic() n.Use(c) n.UseHandler(mux) n.Run(":3000") }
func main() { c := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedHeaders: []string{"*"}, AllowedMethods: []string{"GET", "POST", "OPTIONS"}, Debug: true, }) // Using BoltDB to store the users db, err := bolt.Open("../usersdb", 0600, &bolt.Options{}) if err != nil { log.Fatalln("Can not open the database", err) } defer db.Close() boltStore, err := store.NewBoltStore(db, "users") if err != nil { log.Fatalln("Can not create bolt store", err) } // check github.com/dgrijalva/jwt-go for the JWT options options := jwt.Options{ SigningMethod: "RS256", PrivateKey: Private, // $ openssl genrsa -out app.rsa keysize PublicKey: Public, // $ openssl rsa -in app.rsa -pubout > app.rsa.pub Expiration: 600 * time.Minute, } mgoSession := getSession() uc := controllers.NewUserController(mgoSession) authRoute := auth.NewAuthRoute(boltStore, options) // authMicroservice authMicro := httprouter.New() authMicro.HandlerFunc("POST", "/login", authRoute.Login) authMicro.HandlerFunc("POST", "/signin", authRoute.Signin) n := negroni.Classic() n.Use(c) n.UseHandler(authMicro) go n.Run(":1211") // API pc := controllers.NewPublicationController(mgoSession) sc := controllers.NewSubscriptionController(mgoSession) router := mux.NewRouter() router.HandleFunc("/", controllers.HomeHandler) router.HandleFunc("/users", uc.CreateUser).Methods("POST") router.HandleFunc("/users", uc.ListUsers).Methods("GET") router.HandleFunc("/users/find", uc.FindUser).Methods("GET") router.HandleFunc("/users/{id}", uc.GetUser).Methods("GET") router.HandleFunc("/users/{id}", uc.RemoveUser).Methods("DELETE") router.HandleFunc("/publications", pc.ListPublications).Methods("GET") router.HandleFunc("/publications", pc.CreatePublication).Methods("POST") router.HandleFunc("/publications/{pid}", pc.GetPublication).Methods("GET") router.HandleFunc("/users/{id}/publications", pc.CreatePublication).Methods("POST") router.HandleFunc("/users/{id}/publications", pc.ListUserPublications).Methods("GET") router.HandleFunc("/users/{id}/publications/{pid}", pc.GetPublication).Methods("GET") router.HandleFunc("/users/{id}/publications/{pid}", pc.UpdatePublication).Methods("PUT") router.HandleFunc("/users/{id}/publications/{pid}", pc.RemovePublication).Methods("DELETE") router.HandleFunc("/users/{id}/subscriptions", sc.CreateSubscription).Methods("POST") router.HandleFunc("/users/{id}/subscriptions", sc.ListSubscriptions).Methods("GET") router.HandleFunc("/users/{id}/subscriptions/{sid}", sc.GetSubscription).Methods("GET") router.HandleFunc("/users/{id}/subscriptions/{sid}", sc.RemoveSubscription).Methods("DELETE") app := negroni.Classic() // Use the middleware to protect this app app.Use(c) app.Use(negroni.HandlerFunc(authRoute.AuthMiddleware)) // Add routes app.UseHandler(router) app.Run(":7000") }