설치
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"))
}