-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from naohito-T/feature/huma
Feature/huma
- Loading branch information
Showing
13 changed files
with
356 additions
and
107 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
1.21.0 | ||
1.21.9 |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,11 @@ | ||
# Backend | ||
|
||
```sh | ||
make dev | ||
|
||
## memo | ||
curl -X GET http://localhost:6500/openapi.yaml | ||
``` | ||
|
||
- go vet | ||
- コンパイラでは発見できないバグを見つける | ||
- go testを走らせれば自動で実行される(Go 1.10から) | ||
## API Specifies | ||
|
||
|
||
要件 | ||
|
||
- go testを導入(これでgo vetもできる) | ||
- go lintは非推奨 | ||
- staticcheckを導入(VSCodeではデフォルト) | ||
see: <http://localhost:6500/docs> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,81 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/danielgtaylor/huma/v2" | ||
"github.com/danielgtaylor/huma/v2/adapters/humaecho" | ||
"github.com/danielgtaylor/huma/v2/humacli" | ||
"github.com/labstack/echo/v4" | ||
"github.com/naohito-T/tinyurl/backend/configs" | ||
"github.com/naohito-T/tinyurl/backend/internal/rest/middleware" | ||
"github.com/naohito-T/tinyurl/backend/internal/rest/router" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
const ( | ||
defaultPort = ":6500" | ||
) | ||
|
||
// ここはCLIからの引数を受け取るための構造体 | ||
type Options struct { | ||
Debug bool `doc:"Enable debug logging"` | ||
Host string `doc:"Hostname to listen on."` | ||
Port int `doc:"Port to listen on." short:"p" default:"8888"` | ||
} | ||
|
||
// publicにわける | ||
// user(ログイン必須) | ||
// private(管理者) | ||
|
||
func main() { | ||
// Echo instance | ||
e := echo.New() | ||
middleware.CustomMiddleware(e) | ||
router.NewRouter(e) | ||
// e.Startでエラーが発生した場合、Fatalでプログラムを終了する | ||
e.Logger.Fatal(e.Start(defaultPort)) | ||
var api huma.API | ||
|
||
cli := humacli.New(func(hooks humacli.Hooks, opts *Options) { | ||
fmt.Printf("Options are debug:%v host:%v port%v\n", opts.Debug, opts.Host, opts.Port) | ||
|
||
e := echo.New() | ||
// configを初期化 | ||
configs.NewAppEnvironment() | ||
config := huma.DefaultConfig(configs.OpenAPITitle, configs.OpenAPIVersion) | ||
// Openapiのserver設定 | ||
config.Servers = []*huma.Server{ | ||
{URL: "http://localhost:6500/api/v1"}, | ||
} | ||
|
||
config.Components.SecuritySchemes = map[string]*huma.SecurityScheme{ | ||
"bearer": { | ||
Type: "http", | ||
Scheme: "bearer", | ||
BearerFormat: "JWT", | ||
}, | ||
} | ||
config.DocsPath = "/docs" | ||
// ミドルウェアを適用(すべてのリクエストに対して) | ||
middleware.CustomMiddleware(e) | ||
// /api/v1/openapi.yaml | ||
// これgroup化したやつをnewUserRouterに渡す必要かも | ||
api = humaecho.NewWithGroup(e, e.Group("/api/v1"), config) | ||
router.NewPublicRouter(api) | ||
|
||
// 未定義のルート用のキャッチオールハンドラ | ||
e.Any("/*", func(c echo.Context) error { | ||
return c.JSON(http.StatusNotFound, map[string]string{"message": "route_not_found"}) | ||
}) | ||
|
||
hooks.OnStart(func() { | ||
e.Logger.Fatal(e.Start(defaultPort)) | ||
}) | ||
}) | ||
|
||
cli.Root().AddCommand(&cobra.Command{ | ||
Use: "openapi", | ||
Short: "Print the OpenAPI spec", | ||
Run: func(_ *cobra.Command, _ []string) { | ||
b, _ := api.OpenAPI().YAML() | ||
fmt.Println(string(b)) | ||
}, | ||
}) | ||
cli.Run() | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
package configs | ||
|
||
const ( | ||
ApplicationPort = ":6500" | ||
OpenAPITitle = "TinyURL" | ||
OpenAPIVersion = "1.0.0" | ||
) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,25 @@ | ||
package configs | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
|
||
"github.com/kelseyhightower/envconfig" | ||
) | ||
|
||
type AppEnvironment struct { | ||
Stage string `default:"local"` | ||
} | ||
|
||
var newOnceLogger = sync.OnceValue(func() *AppEnvironment { | ||
var ae AppEnvironment | ||
if err := envconfig.Process("", &ae); err != nil { | ||
panic(fmt.Sprintf("Failed to process environment config: %v", err)) | ||
} | ||
return &ae | ||
}) | ||
|
||
// SEE: https://pkg.go.dev/github.com/kelseyhightower/envconfig | ||
func NewAppEnvironment() *AppEnvironment { | ||
return newOnceLogger() | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package context | ||
|
||
// package humaecho | ||
|
||
// import ( | ||
// "context" | ||
|
||
// "github.com/danielgtaylor/huma/v2" | ||
// "github.com/labstack/echo/v4" | ||
// ) | ||
|
||
// // EchoHumaContext は huma.Context に echo.Context を追加します。 | ||
// type EchoHumaContext interface { | ||
// // これは継承らしい | ||
// huma.Context | ||
// EchoContext() echo.Context | ||
// } | ||
|
||
// // echoContextImpl は EchoHumaContext の実装です。 | ||
// type echoContextImpl struct { | ||
// humaCtx huma.Context | ||
// echoCtx echo.Context | ||
// } | ||
|
||
// func (e *echoContextImpl) Operation() *huma.Operation { | ||
// return e.humaCtx.Operation() | ||
// } | ||
|
||
// func (e *echoContextImpl) Context() context.Context { | ||
// return e.humaCtx.Context() | ||
// } | ||
|
||
// // 以下、huma.Context の他のメソッドも同様に実装します。 | ||
// // ... | ||
|
||
// // EchoContext は echo.Context を返します。 | ||
// func (e *echoContextImpl) EchoContext() echo.Context { | ||
// return e.echoCtx | ||
// } | ||
|
||
// // NewEchoHumaContext は新しい EchoHumaContext を作成します。 | ||
// func NewEchoHumaContext(hCtx huma.Context, eCtx echo.Context) EchoHumaContext { | ||
// return &echoContextImpl{ | ||
// humaCtx: hCtx, // huma.Context をセット | ||
// echoCtx: eCtx, // echo.Context をセット | ||
// } | ||
// } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,49 @@ | ||
package handler | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/labstack/echo/v4" | ||
"github.com/naohito-T/tinyurl/backend/domain/customerror" | ||
) | ||
|
||
// HealthCheckParams はヘルスチェックのパラメータを定義します | ||
type HealthCheckParams struct { | ||
// CheckDB *string `query:"check_db" validate:"required"` | ||
CheckDB *string `query:"check_db"` | ||
} | ||
|
||
func HealthHandler(c echo.Context) error { | ||
h := new(HealthCheckParams) | ||
c.Logger().Error("Binding...") | ||
if err := c.Bind(h); err != nil { | ||
return echo.NewHTTPError(http.StatusBadRequest, err.Error()) | ||
} | ||
c.Logger().Error("Validating...") | ||
if err := c.Validate(h); err != nil { | ||
return &customerror.ValidationError{Message: "this is wrapped", Err: err} | ||
} | ||
|
||
if h.CheckDB != nil { | ||
return c.JSON(http.StatusOK, map[string]string{"message": "check_db"}) | ||
} | ||
|
||
return c.JSON(http.StatusOK, map[string]string{"message": "ok3"}) | ||
} | ||
// import ( | ||
// "context" | ||
// "net/http" | ||
|
||
// "github.com/labstack/echo/v4" | ||
// "github.com/naohito-T/tinyurl/backend/domain/customerror" | ||
// ) | ||
|
||
// // HealthCheckParams はヘルスチェックのパラメータを定義します | ||
// // type HealthCheckParams struct { | ||
// // // CheckDB *string `query:"check_db" validate:"required"` | ||
// // CheckDB *string `query:"check_db"` | ||
// // } | ||
|
||
// func HealthHandler(c echo.Context) error { | ||
// h := new(HealthCheckParams) | ||
// c.Logger().Error("Binding...") | ||
// if err := c.Bind(h); err != nil { | ||
// return echo.NewHTTPError(http.StatusBadRequest, err.Error()) | ||
// } | ||
// c.Logger().Error("Validating...") | ||
// if err := c.Validate(h); err != nil { | ||
// return &customerror.ValidationError{Message: "this is wrapped", Err: err} | ||
// } | ||
|
||
// if h.CheckDB != nil { | ||
// return c.JSON(http.StatusOK, map[string]string{"message": "check_db"}) | ||
// } | ||
|
||
// return c.JSON(http.StatusOK, map[string]string{"message": "ok3"}) | ||
// } | ||
|
||
// type HealthCheckParams struct { | ||
// CheckDB *bool `json:"check_db"` | ||
// } | ||
|
||
// type HealthCheckParams2 struct { | ||
// Message string `json:"message"` | ||
// } | ||
|
||
// // HealthHandler2 は、リクエストを受け取り、適切なレスポンスを返します。 | ||
// func HealthHandler2(ctx context.Context, params *HealthCheckParams) (*HealthCheckParams2, error) { | ||
// if params.CheckDB != nil && *params.CheckDB { | ||
// return &HealthCheckParams2{Message: "check_db"}, nil | ||
// } | ||
// return &HealthCheckParams2{Message: "ok3"}, nil | ||
// } |
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
Oops, something went wrong.