Skip to content

Commit

Permalink
Merge pull request #12 from naohito-T/feature/private-api
Browse files Browse the repository at this point in the history
Add: redirect api
  • Loading branch information
naohito-T authored Sep 15, 2024
2 parents b342675 + 0635494 commit f120aeb
Show file tree
Hide file tree
Showing 29 changed files with 589 additions and 225 deletions.
4 changes: 4 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CONTRIBUTING

## Coding

https://go.dev/doc/effective_go

## Backend

### Setup
Expand Down
2 changes: 1 addition & 1 deletion backend/.go-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.21.9
1.22.7
3 changes: 3 additions & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,7 @@ dynamo.admin:
export PORT="4005" && \
npx dynamodb-admin

openapi:
docker run -it --rm -p 9000:8080 -v $(pwd)/api/openapi.yaml:/usr/share/nginx/html/api/openapi.yaml -e API_URL=http://localhost:9000/api/openapi.yaml swaggerapi/swagger-ui:latest

.PHONY: dev test sec errcheck staticcheck format lint-all lint build build-openapi dynamo.admin
4 changes: 4 additions & 0 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ curl -X GET http://localhost:6500/openapi.yaml
## API Specifies

see: <http://localhost:6500/docs>

## Thank you

<https://github.com/bxcodec/go-clean-arch>
170 changes: 135 additions & 35 deletions backend/api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,38 @@
components:
schemas:
CreateTinyURLBodyBody:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- http://localhost:6500/api/v1/schemas/CreateTinyURLBodyBody.json
format: uri
readOnly: true
type: string
url:
description: URL to shorten
examples:
- http://example.com
type: string
required:
- url
type: object
CreateTinyURLResponseBody:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- http://localhost:6500/api/v1/schemas/CreateTinyURLResponseBody.json
format: uri
readOnly: true
type: string
id:
type: string
required:
- id
type: object
ErrorDetail:
additionalProperties: false
properties:
Expand Down Expand Up @@ -57,38 +90,38 @@ components:
format: uri
type: string
type: object
GreetingOutput3Body:
GetInfoTinyURLResponseBody:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- http://localhost:6500/api/v1/schemas/GreetingOutput3Body.json
- http://localhost:6500/api/v1/schemas/GetInfoTinyURLResponseBody.json
format: uri
readOnly: true
type: string
message:
description: Greeting message
examples:
- Hello, world!
created_at:
type: string
id:
type: string
original_url:
type: string
required:
- message
- id
- original_url
- created_at
type: object
HealthCheckParams2Body:
HealthCheckResponseBody:
additionalProperties: false
properties:
$schema:
description: A URL to the JSON Schema for this object.
examples:
- http://localhost:6500/api/v1/schemas/HealthCheckParams2Body.json
- http://localhost:6500/api/v1/schemas/HealthCheckResponseBody.json
format: uri
readOnly: true
type: string
message:
description: Greeting message
examples:
- Hello, world!
type: string
required:
- message
Expand All @@ -99,62 +132,129 @@ components:
scheme: bearer
type: http
info:
title: TinyURL
title: TinyURL API
version: 1.0.0
openapi: 3.1.0
paths:
/greeting/{name}:
/health:
get:
description: Get a greeting for a person by name.
operationId: get-greeting
description: Check the health of the service.
operationId: health
parameters:
- description: Name to greet
example: world
in: path
name: name
required: true
- description: Optional database check parameter
explode: false
in: query
name: q
schema:
description: Name to greet
examples:
- world
maxLength: 30
type: string
description: Optional database check parameter
type: boolean
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/HealthCheckResponseBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Health Check
tags:
- Public
/urls:
post:
description: Create a short URL.
operationId: create-tinyurl
requestBody:
content:
application/json:
schema:
$ref: "#/components/schemas/CreateTinyURLBodyBody"
required: true
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/GreetingOutput3Body"
$ref: "#/components/schemas/CreateTinyURLResponseBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Get a greeting
summary: Create a short URL
tags:
- Greetings
/health:
- Public
/urls/:id:
get:
description: Check the health of the service.
operationId: health
operationId: get-tinyurl-with-redirect
parameters:
- description: ID of the short URL
in: path
name: id
required: true
schema:
type: string
- in: path
name: id
required: true
schema:
type: string
responses:
"204":
description: No Content
headers:
Location:
schema:
type: string
"301":
description: Redirect to original URL
headers:
Location:
description: Location of the original URL
schema:
format: uri
type: string
"404":
content:
text/plain:
schema:
type: string
description: Short URL not found
summary: Redirect to original URL
tags:
- Public
/urls/info/:id:
get:
description: Get Info tinyurl
operationId: info-tinyurl
parameters:
- in: path
name: id
required: true
schema:
type: string
responses:
"200":
content:
application/json:
schema:
$ref: "#/components/schemas/HealthCheckParams2Body"
$ref: "#/components/schemas/GetInfoTinyURLResponseBody"
description: OK
default:
content:
application/problem+json:
schema:
$ref: "#/components/schemas/ErrorModel"
description: Error
summary: Health Check
summary: Get Info tinyurl
tags:
- Greetings
- Public
servers:
- url: http://localhost:6500/api/v1

36 changes: 8 additions & 28 deletions backend/cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import (
"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/infrastructure"
"github.com/naohito-T/tinyurl/backend/internal/rest/middleware"
"github.com/naohito-T/tinyurl/backend/internal/rest/router"
appSchema "github.com/naohito-T/tinyurl/backend/schema/api"
"github.com/spf13/cobra"
)

Expand All @@ -25,44 +27,22 @@ type Options struct {
Port int `doc:"Port to listen on." short:"p" default:"8888"`
}

// /api/v1/openapi.yaml
// initHuma: humaのconfigを初期化
func initHuma() huma.Config {
config := huma.DefaultConfig(configs.OpenAPITitle, configs.OpenAPIVersion)
config.Servers = []*huma.Server{
{URL: configs.OpenAPIDocServerPath},
}

config.Components.SecuritySchemes = map[string]*huma.SecurityScheme{
"bearer": {
Type: "http",
Scheme: "bearer",
BearerFormat: "JWT",
},
}
config.DocsPath = "/docs"
return config
}

// publicにわける
// public(誰でもアクセス可能)
// user(ログイン必須)
// private(管理者)

func main() {
var api huma.API
var c configs.AppEnvironment
logger := infrastructure.NewLogger()

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()
// ミドルウェアを適用(すべてのリクエストに対して)
middleware.CustomMiddleware(e)
// これgroup化したやつをnewUserRouterに渡す必要かも
api = humaecho.NewWithGroup(e, e.Group("/api/v1"), initHuma())
router.NewPublicRouter(api)
c = configs.NewAppEnvironment()

middleware.CustomMiddleware(e, c)
api = router.NewPublicRouter(humaecho.NewWithGroup(e, e.Group("/api/v1"), appSchema.NewHumaConfig()), logger)
// 未定義のルート用のキャッチオールハンドラ
e.Any("/*", func(c echo.Context) error {
return c.JSON(http.StatusNotFound, map[string]string{"message": "route_not_found"})
Expand Down
26 changes: 0 additions & 26 deletions backend/configs/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,3 @@ const (
// OpenAPIServerPath is the base URL for the OpenAPI spec.
OpenAPIDocServerPath = "http://localhost:6500/api/v1"
)

// OperationID: このAPI操作の一意の識別子。これは、API内で操作を参照する際に使用されます。
// Method: HTTPメソッドを指定します。この例では http.MethodGet が使われており、これはHTTPのGETリクエストを示します。
// Path: エンドポイントのURLパスを指定します。ここでは "/greeting/{name}" となっており、{name} はパスパラメータを表しています。
// Summary: 短い説明文です。APIのドキュメントに表示され、APIの目的を簡潔に説明します。
// Description: APIエンドポイントの詳細な説明です。ここでは操作の詳細や動作についての追加情報を提供します。
// Tags: このAPI操作に関連付けられたタグのリストです。これにより、APIドキュメント内で類似の操作をグループ化することができます。

// huma.Register(app, huma.Operation{
// OperationID: "health",
// Method: http.MethodGet,
// Path: Router.Health,
// Summary: "Health Check",
// Description: "Check the health of the service.",
// Tags: []string{"Public"},
// }, func(_ context.Context, _ *HealthCheckParams) (*HealthCheckQuery, error) {
// resp := &HealthCheckQuery{
// Body: struct{
// Message string `json:"message,omitempty" example:"Hello, world!" doc:"Greeting message"`
// }{
// Message: "ok",
// },
// }
// fmt.Printf("Health Check: %v\n", resp.Body.Message)
// return resp, nil
// })
Loading

0 comments on commit f120aeb

Please sign in to comment.