Created
May 2, 2018 15:41
-
-
Save sasaken555/f440961dfd7a9a342ad6b754d2f2c48e to your computer and use it in GitHub Desktop.
Goで簡易Webアプリ作成&Dockerコンテナにまとめる ref: https://qiita.com/NobiNobiKen/items/284522ceb032a0431b97
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
"github.com/labstack/echo" | |
"github.com/labstack/echo/middleware" | |
"github.com/sasaken555/ponz_goecho_server/routes" | |
) | |
func main() { | |
/* Echoインスタンスの作成 */ | |
e := echo.New() | |
/* Root Level Middleware */ | |
// ログ出力は Apache Common Log Format っぽく設定すると読みやすい | |
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ | |
Format: "${host} [${time_rfc3339_nano}] \"${method} ${uri}\" ${status} ${bytes_in} ${bytes_out}\n", | |
})) | |
e.Use(middleware.Recover()) | |
/* ルーティングの設定 */ | |
// 第2引数の関数は別パッケージに外出しすると分かりやすい | |
e.GET("/users/:id", routes.GetUser) | |
e.GET("/users/json", routes.GetJSONUser) | |
e.Logger.Fatal(e.Start(":1323")) // ポート1323で起動。 | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package routes | |
import ( | |
"net/http" | |
"strconv" | |
"github.com/labstack/echo" | |
"github.com/sasaken555/ponz_goecho_server/util" | |
) | |
// GetUser ... Pathパラメータからユーザー(=ID)を取り出して返す | |
func GetUser(c echo.Context) error { | |
// User ID from Path Parameter `users/:id` | |
id := c.Param("id") | |
return c.String(http.StatusOK, id) | |
} | |
// Customer ... 顧客情報の構造体 | |
type Customer struct { | |
ID int64 `json:"id" xml:"id"` | |
Name string `json:"name" xml:"name"` | |
OrderNum int `json:"ordernum" xml:"ordernum"` | |
OrderProd string `json:"orderprod" xml:"orderprod"` | |
} | |
// GetJSONUser ... 顧客情報のJSONを返す | |
func GetJSONUser(c echo.Context) error { | |
userID, err := strconv.ParseInt(c.QueryParam("userId"), 10, 0) // strconvで文字列から整数に型変換 | |
userName := c.QueryParam("userName") | |
orderNum := util.GetRand(100) // 別のパッケージからインポートした指定桁数で乱数を返す関数を使う | |
// userIDが整数でない(=型変換できない)ならば500エラーを返す | |
if err != nil { | |
return echo.NewHTTPError(http.StatusInternalServerError, "You Should Provide userId as Integer!") | |
} | |
// 構造体のポインタを作成 | |
u := &Customer{ | |
ID: userID, | |
Name: userName, | |
OrderNum: orderNum, | |
OrderProd: "Blend Coffee", | |
} | |
return c.JSON(http.StatusOK, u) | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Full SDK version ... (1) | |
FROM golang:1.10-alpine AS build | |
RUN apk update && apk upgrade \ | |
&& apk add curl git | |
RUN curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh | |
WORKDIR /go/src/github.com/sasaken555/ponz_goecho_server | |
COPY . . | |
RUN dep ensure | |
RUN go build -o ponz_goecho_server | |
# Final Output ... (2) | |
FROM golang:1.10-alpine | |
COPY --from=build /go/src/github.com/sasaken555/ponz_goecho_server/ponz_goecho_server /bin/ponz_goecho_server | |
CMD /bin/ponz_goecho_server | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ docker image ls ponz_goecho_server | |
REPOSITORY TAG IMAGE ID CREATED SIZE | |
ponz_goecho_server light 24b61e6c4f83 7 seconds ago 386MB | |
ponz_goecho_server heavy 20bac15a0acf About a minute ago 908MB | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment