Skip to content

Instantly share code, notes, and snippets.

@watanabeyu
Last active March 30, 2021 04:47
Show Gist options
  • Save watanabeyu/78a714d6da3b9c72fe8aed4d547bead3 to your computer and use it in GitHub Desktop.
Save watanabeyu/78a714d6da3b9c72fe8aed4d547bead3 to your computer and use it in GitHub Desktop.
APIフレームワークとしてechoを使い、配列と構造体での吐き出し方法
package main
import (
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
"hoge/signup"
)
func main() {
e := echo.New()
e.Use(middleware.Logger())
e.Use(middleware.Recover())
/* routing */
e.GET("/", Index())
e.POST("/signup/array", signup.SignupArray())
e.POST("/signup/struct", signup.SignupStruct())
e.Logger.Fatal(e.Start(":3000"))
}
func Index() echo.HandlerFunc {
return func(c echo.Context) error {
jsonMap := map[string]string{
"hoge": "this is your domain",
}
return c.JSON(200, jsonMap)
}
}
package signup
import (
"github.com/labstack/echo"
"regexp"
)
func SignupArray() echo.HandlerFunc {
return func(c echo.Context) error {
username := c.FormValue("username")
email := c.FormValue("email")
password := c.FormValue("password")
var jsonMap map[string]interface{} = make(map[string]interface{})
var errors = make([]map[string]interface{}, 0)
var httpStatus = 200
/* username */
if username == "" {
errors = append(errors, map[string]interface{}{
"status": 400,
"param": "username",
"message": "invalid",
})
}
/* email */
if !regexp.MustCompile(`^.+@.$`).MatchString(email) {
errors = append(errors, map[string]interface{}{
"status": 400,
"param": "email",
"message": "invalid",
})
}
/* password */
if !regexp.MustCompile(`^[0-9a-zA-Z]{8,}$`).MatchString(password) {
errors = append(errors, map[string]interface{}{
"status": 400,
"param": "password",
"message": "invalid",
})
}
/* jsonMap */
if len(errors) > 0 {
jsonMap["errors"] = errors
httpStatus = 400
} else {
jsonMap = map[string]interface{}{
"links": map[string]string{
"self": "/signup",
},
"data": nil,
}
}
return c.JSON(httpStatus, jsonMap)
}
}
type Result struct {
Errors []Error `json:"errors"`
Links Links `json:"links"`
Data Data `json:"data"`
}
type Result_Success struct {
Links Links `json:"links"`
Data Data `json:"data"`
}
type Result_Error struct {
Errors []Error `json:"errors"`
}
type Error struct {
Status int `json:"status"`
Param string `json:"param"`
Message string `json:"message"`
}
type Links struct {
Self string `json:"self"`
Next interface{} `json:"next"`
Prev interface{} `json:"prev"`
}
type Data interface{}
func SignupStruct() echo.HandlerFunc {
return func(c echo.Context) error {
username := c.FormValue("username")
email := c.FormValue("email")
password := c.FormValue("password")
var httpStatus = 200
var errors = make([]Error, 0)
/* username */
if username == "" {
errors = append(errors, Error{
Status: 400,
Param: "username",
Message: "invalid",
})
}
/* email */
if !regexp.MustCompile(`^.+@.$`).MatchString(email) {
errors = append(errors, Error{
Status: 400,
Param: "email",
Message: "invalid",
})
}
/* password */
if !regexp.MustCompile(`^[0-9a-zA-Z]{8,}$`).MatchString(password) {
errors = append(errors, Error{
Status: 400,
Param: "password",
Message: "invalid",
})
}
// 構造体のフィールド名は削除できないので、
// 構造体でSignupArrayと同じことをしたいならば、
// 下記のように構造体自体を分けてあげる必要があり
if len(errors) > 0 {
res := Result_Error{}
res.Errors = errors
return c.JSON(httpStatus, res)
} else {
res := Result_Success{}
res.Links = Links{
Self: "/signup",
}
res.Data = nil
// 構造体を渡してもecho側でMarshalしてくれる
return c.JSON(httpStatus, res)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment