go echo 배우기

golang : 2021. 12. 23. 17:14
반응형

시작

mkdir gopoll

 

echo 설치

$ go get -u github.com/labstack/echo

$ go get github.com/mattn/go-sqlite3

 

package main

import (
	// "database/sql"
	"database/sql"
	"fmt"

	"github.com/labstack/echo"
	"github.com/labstack/echo/middleware"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	e := echo.New()

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

	// Initialize the database
	db := initDB("storage.db")
	migrate(db)

	// Define the HTTP routes
	e.GET("/polls", func(c echo.Context) error {
		return c.JSON(200, "GET Polls")
	})

	e.PUT("/polls", func(c echo.Context) error {
		return c.JSON(200, "PUT Polls")
	})

	e.PUT("/polls/:id", func(c echo.Context) error {
		return c.JSON(200, "UPDATE Poll "+c.Param("id"))
	})

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

func initDB(filepath string) *sql.DB {
	db, err := sql.Open("sqlite3", filepath)

	if err != nil {
		fmt.Println(err)
	}

	if db == nil {
		panic("db nil")
	}

	return db
}

func migrate(db *sql.DB) {
	sql := `
	CREATE TABLE IF NOT EXISTS polls(
			id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
			name VARCHAR NOT NULL,
			topic VARCHAR NOT NULL,
			src VARCHAR NOT NULL,
			upvotes INTEGER NOT NULL,
			downvotes INTEGER NOT NULL,
			UNIQUE(name)
	);
	INSERT OR IGNORE INTO polls(name, topic, src, upvotes, downvotes) VALUES('Angular','Awesome Angular', 'https://cdn.colorlib.com/wp/wp-content/uploads/sites/2/angular-logo.png', 1, 0);
	INSERT OR IGNORE INTO polls(name, topic, src, upvotes, downvotes) VALUES('Vue', 'Voguish Vue','https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Vue.js_Logo.svg/400px-Vue.js_Logo.svg.png', 1, 0);
	INSERT OR IGNORE INTO polls(name, topic, src, upvotes, downvotes) VALUES('React','Remarkable React','https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/React-icon.svg/1200px-React-icon.svg.png', 1, 0);
	INSERT OR IGNORE INTO polls(name, topic, src, upvotes, downvotes) VALUES('Ember','Excellent Ember','https://cdn-images-1.medium.com/max/741/1*9oD6P0dEfPYp3Vkk2UTzCg.png', 1, 0);
	INSERT OR IGNORE INTO polls(name, topic, src, upvotes, downvotes) VALUES('Knockout','Knightly Knockout','https://images.g2crowd.com/uploads/product/image/social_landscape/social_landscape_1489710848/knockout-js.png', 1, 0);
`
	_, err := db.Exec(sql)

	if err != nil {
		panic(err)
	}
}

 

$ go mod init ex_echo
go: creating new go.mod: module ex_echo
go: to add module requirements and sums:
go mod tidy

 

 

$ go mod tidy
go: finding module for package github.com/labstack/echo/middleware
go: finding module for package github.com/labstack/echo
go: found github.com/labstack/echo in github.com/labstack/echo v3.3.10+incompatible
go: found github.com/labstack/echo/middleware in github.com/labstack/echo v3.3.10+incompatible
go: finding module for package github.com/labstack/gommon/color
go: finding module for package github.com/labstack/gommon/random
go: finding module for package github.com/dgrijalva/jwt-go
go: finding module for package github.com/labstack/gommon/bytes
go: finding module for package github.com/labstack/gommon/log
go: finding module for package golang.org/x/crypto/acme/autocert
go: finding module for package github.com/valyala/fasttemplate
go: finding module for package github.com/stretchr/testify/assert
go: downloading github.com/stretchr/testify v1.7.0
go: downloading github.com/dgrijalva/jwt-go v3.2.0+incompatible
go: found github.com/labstack/gommon/color in github.com/labstack/gommon v0.3.1
go: found github.com/labstack/gommon/log in github.com/labstack/gommon v0.3.1
go: found golang.org/x/crypto/acme/autocert in golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3
go: found github.com/dgrijalva/jwt-go in github.com/dgrijalva/jwt-go v3.2.0+incompatible
go: found github.com/labstack/gommon/bytes in github.com/labstack/gommon v0.3.1
go: found github.com/labstack/gommon/random in github.com/labstack/gommon v0.3.1
go: found github.com/valyala/fasttemplate in github.com/valyala/fasttemplate v1.2.1
go: found github.com/stretchr/testify/assert in github.com/stretchr/testify v1.7.0
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

 

$ go run ./poll.go 

   ____    __
  / __/___/ /  ___
 / _// __/ _ \/ _ \
/___/\__/_//_/\___/ v3.3.10-dev
High performance, minimalist Go web framework
https://echo.labstack.com
____________________________________O/_______
                                    O\
⇨ http server started on [::]:9000
^Csignal: interrupt

 

 

반응형

'golang' 카테고리의 다른 글

go echo 배우기  (0) 2021.12.23
Posted by Real_G

댓글을 달아 주세요