Skip to content

Commit

Permalink
Merge pull request #242 from traPtitech/feature/#200-response-version
Browse files Browse the repository at this point in the history
Feature/#200 response version
  • Loading branch information
fuji8 authored Dec 7, 2021
2 parents b803450 + 628f231 commit 652d6cd
Show file tree
Hide file tree
Showing 13 changed files with 82 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/develop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set KNOQ_VERSION env
run: echo "KNOQ_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV
- name: Set KNOQ_REVISION env
run: echo "KNOQ_REVISION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
Expand All @@ -28,5 +32,8 @@ jobs:
context: .
push: true
platforms: linux/amd64,linux/arm64
build-args: |
KNOQ_VERSION=${{ env.KNOQ_VERSION }}
KNOQ_REVISION=${{ env.KNOQ_REVISION }}
tags: |
ghcr.io/traptitech/${{ env.IMAGE_NAME }}:beta
7 changes: 7 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ jobs:
- name: Set IMAGE_TAG env
run: echo "IMAGE_TAG=$(echo ${GITHUB_REF:11})" >> $GITHUB_ENV
- uses: actions/checkout@v2
- name: Set KNOQ_VERSION env
run: echo "KNOQ_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_ENV
- name: Set KNOQ_REVISION env
run: echo "KNOQ_REVISION=$(git rev-parse --short HEAD)" >> $GITHUB_ENV
- name: Set up QEMU
uses: docker/setup-qemu-action@v1
- name: Set up Docker Buildx
Expand All @@ -30,6 +34,9 @@ jobs:
context: .
push: true
platforms: linux/amd64,linux/arm64
build-args: |
KNOQ_VERSION=${{ env.KNOQ_VERSION }}
KNOQ_REVISION=${{ env.KNOQ_REVISION }}
tags: |
ghcr.io/traptitech/${{ env.IMAGE_NAME }}:latest
ghcr.io/traptitech/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }}
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ knoQ の全ての機能を動作させるためには、追加の情報が必要
| WEBHOOK_SECRET | 環境変数 | | Bot 情報 |
| CHANNEL_ID | 環境変数 | | Bot の送信先チャンネル |
| TOKEN_KEY | 環境変数 | `random32wordsXXXXXXXXXXXXXXXXXXX` | Token を暗号化する。長さ 32 文字のランダム文字列。存在しない場合はエラー。 |
| KNOQ_VERSION | 環境変数 | UNKNOWN | knoQ のバージョン (github actions でイメージ作成時に指定) |
| KNOQ_REVISION | 環境変数 | UNKNOWN | git の sha1 (github actions でイメージ作成時に指定) |
| DEVELOPMENT | 環境変数 | | 開発時かどうか |
| service.json | ファイル | 空のファイル | google calendar api に必要(権限は必要なし) |

Expand Down
2 changes: 2 additions & 0 deletions development/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ WEBHOOK_ID=
WEBHOOK_SECRET=
CHANNEL_ID=
TOKEN_KEY=
KNOQ_VERSION=
KNOQ_REVISION=
DEVELOPMENT=
1 change: 1 addition & 0 deletions development/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ services:
WEBHOOK_SECRET: ${WEBHOOK_SECRET}
CHANNEL_ID: ${CHANNEL_ID}
TOKEN_KEY: ${TOKEN_KEY:-random32wordsXXXXXXXXXXXXXXXXXXX}
KNOQ_VERSION: ${KNOQ_VERSION:-dev}
DEVELOPMENT: true
volumes:
- ../main.go:/srv/knoq/main.go
Expand Down
25 changes: 24 additions & 1 deletion docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ info:
title: traP knoQ
description: |
This is a sample knoQ server.
version: 2.1.0
version: 2.1.3

servers:
- url: http://knoq.trap.jp/api
Expand All @@ -29,6 +29,8 @@ tags:
description: 認証
- name: iCal
description: ics出力
- name: public
description: 外部公開API

paths:
/rooms:
Expand Down Expand Up @@ -525,6 +527,7 @@ paths:
post:
tags:
- authentication
- public
operationId: getAuthParams
description: リクエストに必要な情報を返す
responses:
Expand Down Expand Up @@ -572,6 +575,26 @@ paths:
type: string
example: "BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:github.com/lestrrat-go/ical\nBEGIN:VTIMEZONE\nTZID:Asia/Tokyo\nBEGIN:STANDARD\nDTSTART:19700101T000000\nTZNAME:JST\nTZOFFSETFROM:+9000\nTZOFFSETTO:+9000\nEND:STANDARD\nEND:VTIMEZONE\nBEGIN:VEVENT\nCREATED:20'200'827T09'400'8Z\nDESCRIPTION:asd¥n-----------------------------------¥nイベント詳細ページ¥nhttp://loc\nalhost:'400'0/events/855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nDTEND:20'200'828T1'400'00Z\nDTSTAMP:20'200'904T145144Z\nDTSTART:20'200'828T050000Z\nLAST-MODIFIED:20'200'827T09'400'8Z\nLOCATION:dfaaa\nORGANIZER:18984a38-4dc7-41ab-9c8d-f5469a8e30a9\nSUMMARY:sadfwe\nUID:855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nEND:VEVENT\nBEGIN:VEVENT\nCREATED:20'200'827T09'400'8Z\nDESCRIPTION:asd¥n-----------------------------------¥nイベント詳細ページ¥nhttp://loc\nalhost:'400'0/events/855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nDTEND:20'200'828T1'400'00Z\nDTSTAMP:20'200'904T145144Z\nDTSTART:20'200'828T050000Z\nLAST-MODIFIED:20'200'827T09'400'8Z\nLOCATION:dfaaa\nORGANIZER:18984a38-4dc7-41ab-9c8d-f5469a8e30a9\nSUMMARY:sadfwe\nUID:855cff6d-aef6-4de2-8ba5-bc0c3e76a617\nEND:VEVENT\nEND:VCALENDAR"

/version:
get:
tags:
- public
operationId: getVersion
description: version情報を取得
responses:
'200':
description: versionを出力
content:
application/json:
schema:
type: object
properties:
version:
type: string
example: v2.1.3
revision:
type: string
example: 587c185
components:
schemas:
# will delete
Expand Down
2 changes: 2 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

var (
VERSION string = "UNKNOWN"
REVISION string = "UNKNOWN"
DEVELOPMENT bool
)

Expand Down
2 changes: 1 addition & 1 deletion infra/db/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func createEventFilter(expr filter.Expr) (string, []interface{}, error) {
filter.AttrName: "events.name",
filter.AttrGroup: "events.group_id",
filter.AttrRoom: "events.room_id",
filter.AttrTag: "events.event_tags.tag_id",
filter.AttrTag: "event_tags.tag_id",
filter.AttrEvent: "events.id",
filter.AttrTimeStart: "events.time_start",
filter.AttrTimeEnd: "events.time_end",
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

func main() {
logger, _ := zap.NewDevelopment()
domain.VERSION = os.Getenv("KNOQ_VERSION")
domain.REVISION = os.Getenv("KNOQ_REVISION")
domain.DEVELOPMENT, _ = strconv.ParseBool(os.Getenv("DEVELOPMENT"))

gormRepo := db.GormRepository{}
Expand Down
5 changes: 5 additions & 0 deletions presentation/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ type Model struct {
UpdatedAt time.Time `json:"updatedAt"`
DeletedAt *time.Time `json:"deletedAt,omitempty"`
}

type Version struct {
Version string `json:"version"`
Revision string `json:"revision"`
}
10 changes: 10 additions & 0 deletions router/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func AccessLoggingMiddleware(logger *zap.Logger) echo.MiddlewareFunc {
}
}

// ServerVersionMiddleware X-KNOQ-VERSIONをレスポンスヘッダーを追加するミドルウェア
func ServerVersionMiddleware(version string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
c.Response().Header().Set("X-KNOQ-VERSION", version)
return next(c)
}
}
}

// TraQUserMiddleware traQユーザーか判定するミドルウェア
// TODO funcname fix
func (h *Handlers) TraQUserMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
Expand Down
3 changes: 3 additions & 0 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func (h *Handlers) SetupRoute() *echo.Echo {
}
e.Use(session.Middleware(sessions.NewCookieStore(h.SessionKey)))

e.Use(ServerVersionMiddleware(domain.VERSION))

// TODO fix "portal origin"
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{"https://portal.trap.jp", "http://localhost:8080"},
Expand Down Expand Up @@ -142,6 +144,7 @@ func (h *Handlers) SetupRoute() *echo.Echo {
e.POST("/api/authParams", h.HandlePostAuthParams)
e.GET("/api/callback", h.HandleCallback)
e.GET("/api/ical/v1/:userIDsecret", h.HandleGetiCalByPrivateID)
e.GET("/api/version", h.HandleGetVersion)

e.Use(middleware.StaticWithConfig(middleware.StaticConfig{
Skipper: func(c echo.Context) bool {
Expand Down
16 changes: 16 additions & 0 deletions router/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package router

import (
"net/http"

"github.com/labstack/echo/v4"
"github.com/traPtitech/knoQ/domain"
"github.com/traPtitech/knoQ/presentation"
)

func (h *Handlers) HandleGetVersion(c echo.Context) error {
return c.JSON(http.StatusOK, presentation.Version{
Version: domain.VERSION,
Revision: domain.REVISION,
})
}

0 comments on commit 652d6cd

Please sign in to comment.