From 5880c63a55087a96a6a7b2d7f6abbe64d63f77d2 Mon Sep 17 00:00:00 2001 From: SAKATA Sinji Date: Sat, 4 Aug 2018 17:17:37 +0900 Subject: [PATCH] Add sample --- .gitignore | 3 + Gopkg.lock | 93 +++++++++++++++++++++++++++ Gopkg.toml | 29 +++++++++ LICENSE | 21 ++++++ Makefile | 2 + app.go | 164 +++++++++++++++++++++++++++++++++++++++++++++++ views/index.html | 22 +++++++ 7 files changed, 334 insertions(+) create mode 100644 .gitignore create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 app.go create mode 100644 views/index.html diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b11aaa8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +vendor/ +sample-redigo +*.rdb diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..ccf601f --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,93 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + digest = "1:38ec74012390146c45af1f92d46e5382b50531247929ff3a685d2b2be65155ac" + name = "github.com/gomodule/redigo" + packages = [ + "internal", + "redis", + ] + pruneopts = "UT" + revision = "9c11da706d9b7902c6da69c592f75637793fe121" + version = "v2.0.0" + +[[projects]] + digest = "1:26971a24734a1b911111711f981ffc7463361b36483456f33da623570b7d214c" + name = "github.com/labstack/echo" + packages = ["."] + pruneopts = "UT" + revision = "6d227dfea4d2e52cb76856120b3c17f758139b4e" + version = "3.3.5" + +[[projects]] + digest = "1:faee5b9f53eb1ae4eb04708c040c8c4dd685ce46509671e57a08520a15c54368" + name = "github.com/labstack/gommon" + packages = [ + "color", + "log", + ] + pruneopts = "UT" + revision = "d6898124de917583f5ff5592ef931d1dfe0ddc05" + version = "0.2.6" + +[[projects]] + digest = "1:c658e84ad3916da105a761660dcaeb01e63416c8ec7bc62256a9b411a05fcd67" + name = "github.com/mattn/go-colorable" + packages = ["."] + pruneopts = "UT" + revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" + version = "v0.0.9" + +[[projects]] + digest = "1:d4d17353dbd05cb52a2a52b7fe1771883b682806f68db442b436294926bbfafb" + name = "github.com/mattn/go-isatty" + packages = ["."] + pruneopts = "UT" + revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39" + version = "v0.0.3" + +[[projects]] + branch = "master" + digest = "1:c468422f334a6b46a19448ad59aaffdfc0a36b08fdcc1c749a0b29b6453d7e59" + name = "github.com/valyala/bytebufferpool" + packages = ["."] + pruneopts = "UT" + revision = "e746df99fe4a3986f4d4f79e13c1e0117ce9c2f7" + +[[projects]] + branch = "master" + digest = "1:268b8bce0064e8c057d7b913605459f9a26dcab864c0886a56d196540fbf003f" + name = "github.com/valyala/fasttemplate" + packages = ["."] + pruneopts = "UT" + revision = "dcecefd839c4193db0d35b88ec65b4c12d360ab0" + +[[projects]] + branch = "master" + digest = "1:35aff791260b519f5766ebbd85eb439f488bd1af407c785738fb951105b2adbd" + name = "golang.org/x/crypto" + packages = [ + "acme", + "acme/autocert", + ] + pruneopts = "UT" + revision = "56440b844dfe139a8ac053f4ecac0b20b79058f4" + +[[projects]] + branch = "master" + digest = "1:9ec6ad2659635ba0974dd7e55bf84233523eb4e7535c9a2fddaefc4cc62a3eac" + name = "golang.org/x/sys" + packages = ["unix"] + pruneopts = "UT" + revision = "0ffbfd41fbef8ffcf9b62b0b0aa3a5873ed7a4fe" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + input-imports = [ + "github.com/gomodule/redigo/redis", + "github.com/labstack/echo", + ] + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..2765dc9 --- /dev/null +++ b/Gopkg.toml @@ -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 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..23b039d --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7ba82f3 --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +app: + go build diff --git a/app.go b/app.go new file mode 100644 index 0000000..fd2c66f --- /dev/null +++ b/app.go @@ -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") +} diff --git a/views/index.html b/views/index.html new file mode 100644 index 0000000..66c5519 --- /dev/null +++ b/views/index.html @@ -0,0 +1,22 @@ +{{- define "index" -}} + + +

/get

+
+ Key: + +
+

/set

+
+ Key: + Val: + +
+

/increment

+
+ Key: + +
+ + +{{- end -}}