Пример #1
0
func main() {
	// Host map
	hosts := make(Hosts)

	//-----
	// API
	//-----

	api := echo.New()
	api.Use(mw.Logger())
	api.Use(mw.Recover())

	hosts["api.localhost:1323"] = api

	api.Get("/", func(c *echo.Context) error {
		return c.String(http.StatusOK, "API")
	})

	//------
	// Blog
	//------

	blog := echo.New()
	blog.Use(mw.Logger())
	blog.Use(mw.Recover())

	hosts["blog.localhost:1323"] = blog

	blog.Get("/", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Blog")
	})

	//---------
	// Website
	//---------

	site := echo.New()
	site.Use(mw.Logger())
	site.Use(mw.Recover())

	hosts["localhost:1323"] = site

	site.Get("/", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Welcome!")
	})

	http.ListenAndServe(":1323", hosts)
}
Пример #2
0
func main() {
	// Echo instance
	e := echo.New()

	// Debug mode
	e.Debug()

	//------------
	// Middleware
	//------------

	// Logger
	e.Use(mw.Logger())

	// Recover
	e.Use(mw.Recover())

	// Basic auth
	e.Use(mw.BasicAuth(func(usr, pwd string) bool {
		if usr == "joe" && pwd == "secret" {
			return true
		}
		return false
	}))

	// Gzip
	e.Use(mw.Gzip())

	// Routes
	e.Get("/", hello)

	// Start server
	e.Run(":1323")
}
Пример #3
0
func main() {
	e := echo.New()

	e.Use(mw.Logger())
	e.Use(mw.Recover())

	e.Static("/", "public")
	e.WebSocket("/ws", func(c *echo.Context) (err error) {
		ws := c.Socket()
		msg := ""

		for {
			if err = websocket.Message.Send(ws, "Hello, Client!"); err != nil {
				return
			}
			if err = websocket.Message.Receive(ws, &msg); err != nil {
				return
			}
			fmt.Println(msg)
		}
		return
	})

	e.Run(":1323")
}
Пример #4
0
func main() {
	// Setup
	e := echo.New()
	e.Get("/", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Sue sews rose on slow jor crows nose")
	})

	graceful.ListenAndServe(e.Server(":1323"), 5*time.Second)
}
Пример #5
0
func main() {
	// Setup
	e := echo.New()
	e.Get("/", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Six sick bricks tick")
	})

	gracehttp.Serve(e.Server(":1323"))
}
Пример #6
0
func main() {
	e := echo.New()
	e.Use(mw.Logger())
	e.Use(mw.Recover())

	e.Static("/", "public")
	e.Post("/upload", upload)

	e.Run(":1323")
}
Пример #7
0
func main() {
	// Echo instance
	e := echo.New()

	// Middleware
	e.Use(mw.Logger())
	e.Use(mw.Recover())

	// Routes
	e.Get("/", hello)

	// Start server
	e.Run(":1323")
}
Пример #8
0
// Test using httptest's Recorder.s
func TestAddHandlerUsingRecord(t *testing.T) {
	// Configuring the http router.
	e := echo.New()
	e.Post(entryPoint, AddHandler(InMemoryStore()))

	// Issuing request.
	w := httptest.NewRecorder()
	r, _ := http.NewRequest("POST", entryPoint, jsonBuffer(todoItem))
	r.Header.Set("Content-Type", cType)
	e.ServeHTTP(w, r)

	// Asserting on results.
	assert.Equal(t, w.Code, http.StatusCreated)
	assert.Contains(t, w.Body.String(), jsonString(todoItem))
}
Пример #9
0
func main() {
	e := echo.New()
	e.Get("/", func(c *echo.Context) error {
		c.Response().Header().Set(echo.ContentType, echo.ApplicationJSON)
		c.Response().WriteHeader(http.StatusOK)
		for _, l := range locations {
			if err := json.NewEncoder(c.Response()).Encode(l); err != nil {
				return err
			}
			c.Response().Flush()
			time.Sleep(1 * time.Second)
		}
		return nil
	})
	e.Run(":1323")
}
Пример #10
0
// Endo to end test of the API server. This tests brings up a httptest
// server as a blackbox component and invokes API methods using standard
// HTTP calls.
func TestEnd2End(t *testing.T) {
	s := InMemoryStore()
	e := echo.New()
	e.Post(entryPoint, AddHandler(s))
	e.Get(entryPoint, GetHandler(s))

	srv := httptest.NewServer(e)
	defer srv.Close()
	addr := fmt.Sprintf("%s%s", srv.URL, entryPoint)

	// Issuing requests many times concurrently TSAND and VSAND we can detect
	// race conditions.
	// Care to check, please run go test -race :)
	var wg sync.WaitGroup
	for i := 0; i < 50; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			// Add.
			func() {
				r, _ := http.Post(addr, cType, jsonBuffer(todoItem))
				defer r.Body.Close()
				assert.Equal(t, r.StatusCode, http.StatusCreated)
			}()
			// Add with failed precondition.
			func() {
				r, _ := http.Post(addr, cType, bytes.NewBufferString(""))
				defer r.Body.Close()
				assert.Equal(t, r.StatusCode, http.StatusPreconditionFailed)
			}()
			// Get.
			func() {
				r, _ := http.Get(addr)
				defer r.Body.Close()
				if assert.Equal(t, r.StatusCode, http.StatusOK) {
					b, _ := ioutil.ReadAll(r.Body)
					assert.Contains(t, string(b), jsonString(todoItem))
				}
			}()
		}()
	}
	wg.Wait()
}
Пример #11
0
Файл: main.go Проект: erivan/vvt
func main() {
	flag.Parse()
	// Configuring server.
	e := echo.New()
	e.Use(mw.Logger())
	e.Use(mw.Recover())

	// Creating new todo store.
	s := todo.InMemoryStore()

	// Configuring API handlers.
	e.Post("/todo", todo.AddHandler(s))
	e.Get("/todo", todo.GetHandler(s))

	// Configuring HTTP handlers.
	e.Favicon("public/favicon.ico")
	e.Index("public/index.html")

	fmt.Printf("Server listening at localhost:%d\n", *port)
	e.Run(fmt.Sprintf(":%d", *port))
}
Пример #12
0
func main() {
	e := echo.New()

	// Middleware
	e.Use(mw.Logger())
	e.Use(mw.Recover())
	e.Use(mw.Gzip())

	//------------------------
	// Third-party middleware
	//------------------------

	// https://github.com/rs/cors
	e.Use(cors.Default().Handler)

	// https://github.com/thoas/stats
	s := stats.New()
	e.Use(s.Handler)
	// Route
	e.Get("/stats", func(c *echo.Context) error {
		return c.JSON(http.StatusOK, s.Data())
	})

	// Serve index file
	e.Index("public/index.html")

	// Serve favicon
	e.Favicon("public/favicon.ico")

	// Serve static files
	e.Static("/scripts", "public/scripts")

	//--------
	// Routes
	//--------

	e.Post("/users", createUser)
	e.Get("/users", getUsers)
	e.Get("/users/:id", getUser)

	//-----------
	// Templates
	//-----------

	t := &Template{
		// Cached templates
		templates: template.Must(template.ParseFiles("public/views/welcome.html")),
	}
	e.SetRenderer(t)
	e.Get("/welcome", welcome)

	//-------
	// Group
	//-------

	// Group with parent middleware
	a := e.Group("/admin")
	a.Use(func(c *echo.Context) error {
		// Security middleware
		return nil
	})
	a.Get("", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Welcome admin!")
	})

	// Group with no parent middleware
	g := e.Group("/files", func(c *echo.Context) error {
		// Security middleware
		return nil
	})
	g.Get("", func(c *echo.Context) error {
		return c.String(http.StatusOK, "Your files!")
	})

	// Start server
	e.Run(":1323")
}