Skip to content

Commit

Permalink
Add sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ysakasin committed Aug 4, 2018
1 parent 639d4d3 commit 5880c63
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
sample-redigo
*.rdb
93 changes: 93 additions & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Gopkg.toml example
#
# Refer to https://golang.github.io/dep/docs/Gopkg.toml.html
# for detailed Gopkg.toml documentation.
#
# required = ["github.com/user/thing/cmd/thing"]
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
#
# [[constraint]]
# name = "github.com/user/project"
# version = "1.0.0"
#
# [[constraint]]
# name = "github.com/user/project2"
# branch = "dev"
# source = "github.com/myfork/project2"
#
# [[override]]
# name = "github.com/x/y"
# version = "2.4.0"
#
# [prune]
# non-go = false
# go-tests = true
# unused-packages = true

[prune]
go-tests = true
unused-packages = true
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2018 g0tiu5a

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
app:
go build
164 changes: 164 additions & 0 deletions app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
package main

// Document of redigo
// https://godoc.org/github.com/gomodule/redigo/redis

// Commands of redis
// https://redis.io/commands

import (
"fmt"
"html/template"
"io"
"log"
"net/http"
"os"
"strconv"
"time"

redis "github.com/gomodule/redigo/redis"
"github.com/labstack/echo"
)

var (
redisPool *redis.Pool
)

type Renderer struct {
templates *template.Template
}

func (r *Renderer) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return r.templates.ExecuteTemplate(w, name, data)
}

func redisIncr(key string) error {
redisConn := redisPool.Get()
defer redisConn.Close()

// 基本的にredisのコマンドをそのまま呼び出す
_, err := redisConn.Do("INCR", key)
return err
}

func redisSet(key string, val string) error {
redisConn := redisPool.Get()
defer redisConn.Close()

_, err := redisConn.Do("SET", key, val)
return err
}

func redisSetInt(key string, val int64) error {
redisConn := redisPool.Get()
defer redisConn.Close()

// 引数はstringにしておく必要がある
_, err := redisConn.Do("SET", key, strconv.FormatInt(val, 10))
return err
}

func redisGet(key string) string {
redisConn := redisPool.Get()
defer redisConn.Close()

// 値を取り出す時には redis.XXX を使う
str, err := redis.String(redisConn.Do("GET", key))
if err == nil {
// 正常
} else if err == redis.ErrNil {
// nilが返ってきた
return ""
} else {
log.Printf("something wrong: %v\n", str)
// panic()
return ""
}
return str
}

func redisGetInt(key string) int64 {
redisConn := redisPool.Get()
defer redisConn.Close()

res, err := redis.Int64(redisConn.Do("GET", key)) // keyがない時には0を返す
if err == nil {
// 正常
} else if err == redis.ErrNil {
// nilが返ってきた
} else {
log.Printf("something wrong: %v\n", res)
// panic()
return 0
}
return res
}

func getInitialize(c echo.Context) error {
redisConn := redisPool.Get()
defer redisConn.Close()

redisConn.Do("FLUSHALL")
return c.String(204, "")
}

func getObject(c echo.Context) error {
key := c.QueryParam("key")
str := redisGet(key)
return c.String(http.StatusOK, str)
}

func postObject(c echo.Context) error {
key := c.FormValue("key")
val := c.FormValue("val")
if err := redisSet(key, val); err != nil {
return c.String(http.StatusInternalServerError, "InternalServerError")
}
return c.String(http.StatusOK, "Success")
}

func postIncrObject(c echo.Context) error {
key := c.FormValue("key")
if err := redisIncr(key); err != nil {
return c.String(http.StatusInternalServerError, "InternalServerError")
}
return c.String(http.StatusOK, "Success")
}

func getIndex(c echo.Context) error {
return c.Render(http.StatusOK, "index", nil)
}

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

redisHost := os.Getenv("REDIS_HOST")
if redisHost == "" {
redisHost = "localhost"
}
// 接続を確立する
redisPool = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", fmt.Sprintf("%v:6379", redisHost))
if err != nil {
log.Fatalln("Can not connect to redis.")
return nil, err
}
return c, err
},
}

e.Renderer = &Renderer{
templates: template.Must(template.ParseGlob("views/*.html")),
}

e.GET("/initialize", getInitialize)
e.GET("/", getIndex)
e.GET("/get", getObject)
e.POST("/set", postObject)
e.POST("/increment", postIncrObject)

e.Start(":5000")
}
22 changes: 22 additions & 0 deletions views/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{{- define "index" -}}
<html>
<body>
<h2>/get</h2>
<form method="GET" action="/get">
Key:<input name="key" type="text">
<input type="submit" value="Submit">
</form>
<h2>/set</h2>
<form method="POST" action="/set">
Key:<input name="key" type="text">
Val:<input name="val" type="text">
<input type="submit" value="Submit">
</form>
<h2>/increment</h2>
<form method="POST" action="/increment">
Key:<input name="key" type="text">
<input type="submit" value="Submit">
</form>
</body>
</html>
{{- end -}}

0 comments on commit 5880c63

Please sign in to comment.