터미널에서 Ctrl+C, Ctrl+V 와 같은 복사/붙여넣기 단축키

|

터미널에서 Ctrl+C, Ctrl+V 와 같은 복사/붙여넣기 단축키

터미널에서 복사하기와 붙여넣기 단축키는 다음과 같습니다.

  • 복사하기 : Shift + Ctrl + C
  • 붙여넣기 : Shift + Ctrl + V

dep 설치

|

dep

dep는 Go언어에서 종속성(Dependency)를 관리하는 툴입니다. Go언어 1.9 버전 이상에서 지원되며 기업 등에서도 마음껏 사용할 수 있는 툴입니다.

터미널에서 다음 명령어를 입력하면 설치를 할 수 있습니다.

$ curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh

간단한 Chat App 구현하기 (3) - 인증 처리(로그인)

|

로그인 처리

로그인 화면 작성

/templates/login.tmpl

<html>
<head>
    <title>Login</title>
</head>
<body>
    <h3>먼저 로그인을 해주세요.</h3>
    <div>
        <ul>
            <li>
                <a href="/auth/login/google">Google</a>
            </li>
        </ul>
    </div>
</body>
</html>


main.go

package main

import (
	"github.com/unrolled/render"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"github.com/goincremental/negroni-sessions/cookiestore"
	"github.com/goincremental/negroni-sessions"
	"github.com/urfave/negroni"
)

var renderer *render.Render

func init() {
	renderer = render.New()
}

const (
	sessionKey    = "simple-chat-app-session"
	sessionSecret = "simple-chat-app-session-secret"
)

func main() {
	router := httprouter.New()

	router.GET("/", func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
		renderer.HTML(w, http.StatusOK, "index", map[string]string{"title": "Simple Chat App"})
	})

	router.GET("/login", func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
		renderer.HTML(w, http.StatusOK, "login", nil)
	})

	n := negroni.Classic()

	store := cookiestore.New([]byte(sessionSecret))
	n.Use(sessions.Sessions(sessionKey, store))

	n.UseHandler(router)

	n.Run(":3000")
}

간단한 Chat App 구현하기 (2) - 인증 처리(세션 관리)

|

세션 관리

다음 패키지들을 이용해서 세션을 관리합니다.

각 패키지 설치는 다음 명령어를 이용해서 할 수 있습니다.

$ go get github.com/goincremental/negroni-sessions

$ go get github.com/stretchr/gomniauth

$ go get github.com/stretchr/gomniauth/providers/google


main.go

세션 관리를 위해 세션 핸들러를 등록합니다.

package main

import (
	"github.com/unrolled/render"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"github.com/urfave/negroni"
	"github.com/goincremental/negroni-sessions/cookiestore"
	"github.com/goincremental/negroni-sessions"
)

var renderer *render.Render

func init() {
	renderer = render.New()
}

const (
	sessionKey    = "simple-chat-app-session"
	sessionSecret = "simple-chat-app-session-secret"
)

func main() {
	router := httprouter.New()

	router.GET("/", func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
		renderer.HTML(w, http.StatusOK, "index", map[string]string{"title": "Simple Chat App"})
	})

	n := negroni.Classic()
	store := cookiestore.New([]byte(sessionSecret))
	n.Use(sessions.Sessions(sessionKey, store))

	n.UseHandler(router)

	n.Run(":3000")
}


session.go

package main

import (
	"time"
	"net/http"
	"github.com/goincremental/negroni-sessions"
	"encoding/json"
)

const (
	currentUserKey  = "oauth2_current_user"
	sessionDuration = time.Hour
)

type User struct {
	Uid       string    `json:"uid"`
	Name      string    `json:"name"`
	Email     string    `json:"user"`
	AvatarUrl string    `json:"avatar_url"`
	Expired   time.Time `json:"expired"'`
}

func (u *User) Valid() bool {
	return u.Expired.Sub(time.Now()) > 0
}

func (u *User) Refresh() {
	u.Expired = time.Now().Add(sessionDuration)
}
func GetCurrentUser(r *http.Request) *User {
	s := sessions.GetSession(r)

	if s.Get(currentUserKey) == nil {
		return nil
	}

	data := s.Get(currentUserKey).([]byte)

	var u User
	json.Unmarshal(data, &u)

	return &u
}

func SetCurrentUser(r *http.Request, u *User) {
	if u != nil {
		u.Refresh()
	}

	s := sessions.GetSession(r)
	val, _ := json.Marshal(u)
	s.Set(currentUserKey, val)
}

간단한 Chat App 구현하기 (1) - 간단한 웹 서버 구동

|

간단한 웹 서버 구동

다음 패키지들을 이용해서 간단한 웹 서버를 구동시키는 예제입니다.

각 패키지 설치는 다음 명령어를 이용해서 할 수 있습니다.

$ go get github.com/julienschmidt/httprouter

$ go get https://github.com/urfave/negroni


main.go

package main

import (
	"github.com/unrolled/render"
	"github.com/julienschmidt/httprouter"
	"net/http"
	"github.com/urfave/negroni"
)

var renderer *render.Render

func init() {
	renderer = render.New()
}

func main() {
	router := httprouter.New()

	router.GET("/", func(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
		renderer.HTML(w, http.StatusOK, "index", map[string]string{"title": "Simple Chat App"})
	})

	n := negroni.Classic()

	n.UseHandler(router)

	n.Run(":3000")
}


/templates/index.tmpl

<!DOCTYPE html>
<html lang="en">
<body>
    <h1></h1>
</body>
</html>