Skip to content

Commit

Permalink
Merge pull request #31 from kynmh69/29-feature-add-to-manage-api-key
Browse files Browse the repository at this point in the history
29 feature add to manage api key
  • Loading branch information
kynmh69 authored Mar 30, 2024
2 parents 3fbdbd4 + 9208fb1 commit dd9d810
Show file tree
Hide file tree
Showing 2,942 changed files with 326,854 additions and 3 deletions.
49 changes: 49 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ on:
env:
UPDATER_DIR: "src/updater"
API_DIR: "src/api"
KEY_MAN_DIR: "src/key_management"

jobs:
coverage-updater:
Expand Down Expand Up @@ -109,3 +110,51 @@ jobs:
verbose: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

coverage-key-manager:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_USER: app
POSTGRES_PASSWORD: password
POSTGRES_DB: unittest
ports:
- 5432:5432
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
steps:
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22'
cache-dependency-path: |
src/go.sum
- name: Run migration
working-directory: ${{ env.KEY_MAN_DIR }}
shell: bash -l {0}
run: |
go install github.com/pressly/goose/v3/cmd/goose@latest
$(go env GOPATH)/bin/goose --dir database/migrations/ postgres "host=localhost user=app password=password dbname=unittest sslmode=disable" up
- name: Build
working-directory: ${{ env.KEY_MAN_DIR }}
run: go build -v ./...

- name: Test
working-directory: ${{ env.KEY_MAN_DIR }}
env:
COV_FILE_NAME: coverage.txt
MODE: atomic
run: go test -v -race -coverprofile=${{ env.COV_FILE_NAME }} -covermode=${{ env.MODE }} ./...

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v4
with:
flags: smart-tests
verbose: true
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
1 change: 1 addition & 0 deletions .github/workflows/docker-publish-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
Expand Down
98 changes: 98 additions & 0 deletions .github/workflows/docker-publish-key-manager.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
name: Create Key Manager image

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

on:
push:
branches: [ "main" ]
# Publish semver tags as releases.
tags: [ 'v*.*.*' ]
pull_request:
branches: [ "main" ]

env:
# Use docker.io for Docker Hub if empty
REGISTRY: ghcr.io
# github.repository as <account>/<repo>
IMAGE_NAME: ${{ github.repository }}


jobs:
publish-api:

runs-on: ubuntu-latest
permissions:
contents: read
packages: write
# This is used to complete the identity challenge
# with sigstore/fulcio when running outside of PRs.
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v4

# Install the cosign tool except on PR
# https://github.com/sigstore/cosign-installer
- name: Install cosign
if: github.event_name != 'pull_request'
uses: sigstore/cosign-installer@v3 #v3.1.1
with:
cosign-release: 'v2.2.3'

# Set up BuildKit Docker container builder to be able to build
# multi-platform images and export cache
# https://github.com/docker/setup-buildx-action
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3 # v3.1.0

# Login against a Docker registry except on PR
# https://github.com/docker/login-action
- name: Log into registry ${{ env.REGISTRY }}
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

# Extract metadata (tags, labels) for Docker
# https://github.com/docker/metadata-action
- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v5 # v5.0.0
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-key-manager

# Build and push Docker image with Buildx (don't push on PR)
# https://github.com/docker/build-push-action
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v5
with:
context: .
file: docker/api/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
# repository is public to avoid leaking data. If you would like to publish
# transparency data even for private images, pass --force to cosign below.
# https://github.com/sigstore/cosign
- name: Sign the published Docker image
if: ${{ github.event_name != 'pull_request' }}
env:
# https://docs.github.com/en/actions/security-guides/security-hardening-for-github-actions#using-an-intermediate-environment-variable
TAGS: ${{ steps.meta.outputs.tags }}
DIGEST: ${{ steps.build-and-push.outputs.digest }}
# This step uses the identity token to provision an ephemeral certificate
# against the sigstore community Fulcio instance.
run: echo "${TAGS}" | xargs -I {} cosign sign --yes {}@${DIGEST}
1 change: 1 addition & 0 deletions .github/workflows/docker-publish-updater.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ jobs:
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64

# Sign the resulting Docker image digest except on PRs.
# This will only write to the public Rekor transparency log when the Docker
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@
# Go workspace file
go.work

.vscode/
.vscode/
log/
17 changes: 17 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,23 @@ services:
- ./src:/go/app:rw
ports:
- 8080:80

key_manager:
build:
context: .
dockerfile: ./docker/key_management/Dockerfile
environment:
- TZ=Asia/Tokyo
- LOG_LEVEL=info
depends_on:
- database
networks:
- net-1
volumes:
- ./src:/go/app:rw
ports:
- 8081:80


volumes:
psql_data:
Expand Down
10 changes: 10 additions & 0 deletions docker/key_management/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# syntax=docker/dockerfile:1
FROM golang:1.22 AS build-updater
COPY ./src/ /go/app/
WORKDIR /go/app
RUN go install github.com/cosmtrek/air@latest

RUN go mod download
WORKDIR /go/app/key_management

CMD [ "air", "-c", ".air.toml"]
2 changes: 1 addition & 1 deletion src/api/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/kynmh69/go-ja-holidays/api/middleware"
"github.com/kynmh69/go-ja-holidays/api/router"
"github.com/kynmh69/go-ja-holidays/database"
"github.com/kynmh69/go-ja-holidays/middleware"
"github.com/kynmh69/go-ja-holidays/util"
"github.com/labstack/echo/v4"
)
Expand Down
1 change: 1 addition & 0 deletions src/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (

require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/google/uuid v1.6.0
github.com/labstack/echo-contrib v0.15.0
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
Expand Down
2 changes: 2 additions & 0 deletions src/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/labstack/echo-contrib v0.15.0 h1:9K+oRU265y4Mu9zpRDv3X+DGTqUALY6oRHCSZZKCRVU=
Expand Down
46 changes: 46 additions & 0 deletions src/key_management/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ."
delay = 1000
exclude_dir = ["assets", "tmp", "vendor", "testdata", "node_modules"]
exclude_file = []
exclude_regex = ["_test.go"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
include_file = []
kill_delay = "0s"
log = "build-errors.log"
poll = false
poll_interval = 0
post_cmd = []
pre_cmd = []
rerun = false
rerun_delay = 500
send_interrupt = false
stop_on_error = false

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
main_only = false
time = false

[misc]
clean_on_exit = false

[screen]
clear_on_rebuild = false
keep_scroll = true
1 change: 1 addition & 0 deletions src/key_management/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp/
17 changes: 17 additions & 0 deletions src/key_management/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# API Key管理

## TailwindCSSのコンパイル

以下の手順は`key_management`配下で実施すること。

1. TailwindCSSに関連するソースをインストールする

```bash
npm install -D tailwindcss postcss autoprefixer
```

2. コンパイルをする

```bash
npx tailwindcss -i ./static/css/main.css -o ./static/css/main_output.css
```
11 changes: 11 additions & 0 deletions src/key_management/controller/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package controller

import "github.com/labstack/echo/v4"

type Controller interface {
Retrieve(c echo.Context) error
Create(c echo.Context) error
Update(c echo.Context) error
Delete(c echo.Context) error
GetControllerName() string
}
47 changes: 47 additions & 0 deletions src/key_management/controller/key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package controller

import (
"errors"
"net/http"

"github.com/kynmh69/go-ja-holidays/model"
"github.com/labstack/echo/v4"
)

const (
TOP_PAGE_NAME = "top.html"
TOP_PATH = "/manage/key"
)

type KeyManagement struct {
ControllerName string
}

func (k KeyManagement) Retrieve(c echo.Context) error {
logger := c.Logger()
apiKeys, _ := model.GetApiKeys()
logger.Debug("APIKEYS", apiKeys)
return c.Render(http.StatusOK, TOP_PAGE_NAME, apiKeys)
}

func (k KeyManagement) Create(c echo.Context) error {
model.CreateApiKey(c)
return c.Redirect(http.StatusFound, TOP_PATH)
}

func (k KeyManagement) Update(c echo.Context) error {
return errors.New("not implemented")
}

func (k KeyManagement) Delete(c echo.Context) error {
model.DeleteApiKey(c)
return c.Redirect(http.StatusFound, TOP_PATH)
}

func (k KeyManagement) GetControllerName() string {
return k.ControllerName
}

func NewKeyManagement(controllerName string) *KeyManagement {
return &KeyManagement{ControllerName: controllerName}
}
Loading

0 comments on commit dd9d810

Please sign in to comment.