본문으로 바로가기

echo 기초

category Go/Go in Web(jin, etc...) 2020. 12. 4. 00:14

echo.labstack.com/

 

Echo - High performance, minimalist Go web framework

Echo is a high performance, extensible, minimalist web framework for Go (Golang).

echo.labstack.com

 

설치

 

go get github.com/labstack/echo

 

 

Hello World

 

middleware, route, handler, listening 등 express를 다뤄보았다면 친숙한 개념들이 등장합니다. (server면 사실 다들 쓰는 거긴 합니다)

package main

import (
	"net/http"

	"github.com/labstack/echo"
)

// Handler
func handleHome(c echo.Context) error {
	return c.String(http.StatusOK, "Hello, World!")
}

func main() {
	// Echo instance
	e := echo.New()

	// Routes
	e.GET("/", handleHome)

	// Start server
	e.Logger.Fatal(e.Start(":3000"))

}

 

 

간단한 Get, Post와 템플릿 렌더링

package main

import (
	"os"
	"strings"

	"github.com/DarrenKwonDev/learnGo/scrapper"
	"github.com/labstack/echo"
)

// Handler
func handleHome(c echo.Context) error {
	return c.File("home.html")
}

// Handler
const fileName = "jobs.csv"
func handleScrape(c echo.Context) error {

	term := strings.ToLower(scrapper.CleanString(c.FormValue("term")))
	scrapper.Scrape(term)

	defer os.Remove(fileName)
	// Attachment(뭘 줄 것인가, 무슨 이름으로 다운로드?)
	return c.Attachment(fileName, fileName)
}

func main() {
	// Echo instance
	e := echo.New()

	// Routes
	e.GET("/", handleHome)

	e.POST("/scrape", handleScrape)

	// Start server
	e.Logger.Fatal(e.Start(":3000"))
}

darren, dev blog
블로그 이미지 DarrenKwonDev 님의 블로그
VISITOR 오늘 / 전체