├── domain ├── model │ ├── error.go │ ├── notification.go │ └── app.go └── repository │ ├── app_repository.go │ └── notification_repository.go ├── go.mod ├── .gitignore ├── interface └── database │ └── sql_handler.go ├── handler ├── healthcheck_handler.go ├── helloworld_handler.go ├── notification_handler.go └── app_handler.go ├── utils ├── messages_config.go ├── request_check.go ├── config.go └── messages.go ├── Dockerfile ├── Makefile ├── main.go ├── usecase ├── app_interactor.go └── notifiation_interactor.go ├── infrastructure ├── router.go └── sql_handler.go ├── README_EN.md ├── README.md ├── go.sum └── LICENSE /domain/model/error.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | // ErrorMessages ... entity for error result 4 | type ErrorMessages struct { 5 | Code string `json:"code"` 6 | Message string `json:"message"` 7 | } 8 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/uma-arai/sbcntr-backend 2 | 3 | go 1.16 4 | 5 | require ( 6 | github.com/labstack/echo/v4 v4.6.1 7 | github.com/labstack/gommon v0.3.0 8 | gorm.io/driver/mysql v1.1.2 9 | gorm.io/gorm v1.21.15 10 | ) 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # ----- for golang app ------ 2 | # Binaries for programs and plugins 3 | *.exe 4 | *.exe~ 5 | *.dll 6 | *.so 7 | *.dylib 8 | 9 | # Test binary, built with `go test -c` 10 | *.test 11 | 12 | # Output of the go coverage tool, specifically when used with LiteIDE 13 | *.out 14 | 15 | # Dependency directories (remove the comment below to include it) 16 | # vendor/ 17 | 18 | # Avoid pushing exec binary 19 | main 20 | 21 | # ----- for IDE (GoLand) ----- 22 | .idea 23 | -------------------------------------------------------------------------------- /interface/database/sql_handler.go: -------------------------------------------------------------------------------- 1 | package database 2 | 3 | // SQLHandler ... 4 | type SQLHandler interface { 5 | Where(out interface{}, query interface{}, args ...interface{}) interface{} 6 | Scan(out interface{}, order string) interface{} 7 | Count(out *int, model interface{}, query interface{}, args ...interface{}) interface{} 8 | Create(input interface{}) (result interface{}, err error) 9 | Update(out interface{}, value interface{}, query interface{}, args ...interface{}) interface{} 10 | } 11 | -------------------------------------------------------------------------------- /handler/healthcheck_handler.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "github.com/labstack/echo/v4" 5 | ) 6 | 7 | // HealthCheckHandler ... 8 | type HealthCheckHandler struct { 9 | } 10 | 11 | // NewHealthCheckHandler ... 12 | func NewHealthCheckHandler() *HealthCheckHandler { 13 | return &HealthCheckHandler{} 14 | } 15 | 16 | // HealthCheck ... 17 | func (handler *HealthCheckHandler) HealthCheck() echo.HandlerFunc { 18 | 19 | return func(c echo.Context) error { 20 | return c.JSON(200, nil) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /handler/helloworld_handler.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/labstack/echo/v4" 7 | "github.com/uma-arai/sbcntr-backend/domain/model" 8 | ) 9 | 10 | // HelloWorldHandler ... 11 | type HelloWorldHandler struct { 12 | } 13 | 14 | // NewHelloWorldHandler ... 15 | func NewHelloWorldHandler() *HelloWorldHandler { 16 | return &HelloWorldHandler{} 17 | } 18 | 19 | // SayHelloWorld ... 20 | func (handler *HelloWorldHandler) SayHelloWorld() echo.HandlerFunc { 21 | body := &model.Hello{ 22 | Data: "Hello world", 23 | } 24 | return func(c echo.Context) error { 25 | return c.JSON(http.StatusOK, body) 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /utils/messages_config.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | // MessagesConfig ... 4 | var MessagesConfig = `{ 5 | "00001E": { 6 | "statusCode": 400, 7 | "messageCode": "00001E", 8 | "message": { 9 | "ja": "ヘッダーチェック処理にてエラーが発生しました。", 10 | "en": "header parameter is invalid." 11 | } 12 | }, 13 | "00002E": { 14 | "statusCode": 400, 15 | "messageCode": "00002E", 16 | "message": { 17 | "ja": "クライアントIDチェック処理にてエラーが発生しました。", 18 | "en": "ClientID parameter is invalid." 19 | } 20 | }, 21 | "10001E": { 22 | "statusCode": 500, 23 | "messageCode": "10001E", 24 | "message": { 25 | "ja": "DBへのデータ取得時にエラーが発生しました。", 26 | "en": "DB select error." 27 | } 28 | } 29 | }` 30 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | # Multi stage building strategy for reducing image size. 2 | FROM golang:1.16.8-alpine3.13 AS build-env 3 | ENV GO111MODULE=on 4 | RUN mkdir /app 5 | WORKDIR /app 6 | 7 | # Install each dependencies 8 | COPY go.mod /app 9 | COPY go.sum /app 10 | RUN go mod download 11 | RUN apk add --no-cache --virtual git gcc make build-base alpine-sdk 12 | 13 | # COPY main module 14 | COPY . /app 15 | 16 | # Check and Build 17 | RUN go get golang.org/x/lint/golint && \ 18 | make validate && \ 19 | make build-linux 20 | 21 | ### If use TLS connection in container, add ca-certificates following command. 22 | ### > RUN apk add --no-cache ca-certificates 23 | FROM gcr.io/distroless/base-debian10 24 | COPY --from=build-env /app/main / 25 | EXPOSE 80 26 | ENTRYPOINT ["/main"] 27 | 28 | -------------------------------------------------------------------------------- /utils/request_check.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "github.com/labstack/echo/v4" 5 | ) 6 | 7 | const ( 8 | headerClientID string = "x-client-id" 9 | ) 10 | 11 | // HeaderCheck ... 12 | func HeaderCheck(context interface{}, headerName string, headerValue string) (err error) { 13 | c := context.(echo.Context) 14 | currentHeader := c.Request().Header.Get(headerName) 15 | if currentHeader != headerValue { 16 | err = SetErrorMassage("00001E") 17 | } 18 | return 19 | } 20 | 21 | // ClientIDCheck ... 22 | func ClientIDCheck(context interface{}) (err error) { 23 | config := NewAPIConfig() 24 | 25 | c := context.(echo.Context) 26 | clientID := c.Request().Header.Get(headerClientID) 27 | if clientID != config.HeaderValue.ClientID { 28 | err = SetErrorMassage("00002E") 29 | } 30 | return 31 | } 32 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Golang parameter(gofmt: code formatter, go vet/golist: lint tools) 2 | GOCMD=go 3 | GOFMT=go fmt 4 | GOVET=$(GOCMD) vet 5 | GOLINT=golint 6 | GOBUILD=$(GOCMD) build 7 | GOCLEAN=$(GOCMD) clean 8 | GOTEST=$(GOCMD) test 9 | GOGET=$(GOCMD) get 10 | BINARY_NAME=main 11 | BINARY_UNIX=$(BINARY_NAME)_unix 12 | 13 | BUILD_PATH=. 14 | APP_PATH=. 15 | 16 | all: 17 | make validate 18 | make build 19 | make run 20 | 21 | # delete debug symbols by golang option -dlflags "-s -w" 22 | build: 23 | $(GOBUILD) -o $(BUILD_PATH)/$(BINARY_NAME) -ldflags "-s -w" $(APP_PATH)/main.go 24 | validate: 25 | $(GOFMT) ./... 26 | $(GOVET) ./... 27 | $(GOTEST) -v ./... 28 | clean: 29 | rm -f $(BUILD_PATH)/$(BINARY_NAME) 30 | rm -f $(BUILD_PATH)/$(BINARY_UNIX) 31 | run: 32 | $(BUILD_PATH)/$(BINARY_NAME) 33 | 34 | # cross-compile for linux 35 | build-linux: 36 | CGO_ENABLED=0 GOOS=linux GOARCH=amd64 make build 37 | 38 | -------------------------------------------------------------------------------- /utils/config.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "os" 5 | ) 6 | 7 | // APIConfig ... 8 | type APIConfig struct { 9 | HeaderValue struct { 10 | ClientID string 11 | } 12 | } 13 | 14 | // ConfigDB ... 15 | type ConfigDB struct { 16 | MySQL struct { 17 | DBMS string 18 | Protocol string 19 | Username string 20 | Password string 21 | DBName string 22 | } 23 | } 24 | 25 | // NewAPIConfig ... 26 | func NewAPIConfig() *APIConfig { 27 | config := new(APIConfig) 28 | config.HeaderValue.ClientID = os.Getenv("SBCNTR_CLIENT_ID_HEADER") 29 | 30 | return config 31 | } 32 | 33 | // NewConfigDB ... 34 | func NewConfigDB() *ConfigDB { 35 | config := new(ConfigDB) 36 | 37 | config.MySQL.DBMS = "mysql" 38 | config.MySQL.Protocol = "tcp(" + os.Getenv("DB_HOST") + ":3306)" 39 | config.MySQL.Username = os.Getenv("DB_USERNAME") 40 | config.MySQL.Password = os.Getenv("DB_PASSWORD") 41 | config.MySQL.DBName = os.Getenv("DB_NAME") 42 | 43 | return config 44 | } 45 | -------------------------------------------------------------------------------- /domain/model/notification.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | type ( 4 | // Notification ... entity for notification db result 5 | Notification struct { 6 | ID int `json:"id" gorm:"column:id"` 7 | Title string `json:"title" gorm:"column:title"` 8 | Description string `json:"description" gorm:"column:description"` 9 | Category string `json:"category" gorm:"column:category"` 10 | Unread bool `json:"unread" gorm:"column:unread"` 11 | CreatedAt string `json:"createdAt" gorm:"column:createdAt"` 12 | UpdatedAt string `json:"updatedAt" gorm:"column:updatedAt"` 13 | } 14 | 15 | // Notifications ... array entity for notification 16 | Notifications struct { 17 | Data []Notification `json:"data"` 18 | } 19 | 20 | // NotificationCount ... unread count of notifications 21 | NotificationCount struct { 22 | Data int `json:"data"` 23 | } 24 | ) 25 | 26 | // TableName ... override GORM table name accessor 27 | func (Notification) TableName() string { 28 | return "Notification" 29 | } 30 | -------------------------------------------------------------------------------- /domain/model/app.go: -------------------------------------------------------------------------------- 1 | package model 2 | 3 | // App ... entity for db result 4 | type App struct { 5 | ID string `json:"id" gorm:"column:id"` 6 | Message string `json:"message" gorm:"column:message"` 7 | } 8 | 9 | // Response ... 10 | type Response struct { 11 | Code int `json:"code" xml:"code"` 12 | Message string `json:"msg" xml:"msg"` 13 | } 14 | 15 | // Hello ... entity for hello message 16 | type Hello struct { 17 | Data string `json:"data" xml:"data"` 18 | } 19 | 20 | type ( 21 | // Item ... 22 | Item struct { 23 | ID int `json:"id" gorm:"column:id"` 24 | Title string `json:"title" gorm:"column:title"` 25 | Name string `json:"name" gorm:"column:name"` 26 | Favorite bool `json:"favorite" gorm:"column:favorite"` 27 | Img string `json:"img" gorm:"column:img"` 28 | CreatedAt string `json:"createdAt" gorm:"column:createdAt"` 29 | UpdatedAt string `json:"updatedAt" gorm:"column:updatedAt"` 30 | } 31 | 32 | // Items ... 33 | Items struct { 34 | Data []Item `json:"data"` 35 | } 36 | ) 37 | 38 | // TableName ... override GORM table name accessor 39 | func (Item) TableName() string { 40 | return "Item" 41 | } 42 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "os" 7 | "os/signal" 8 | "syscall" 9 | "time" 10 | 11 | "github.com/uma-arai/sbcntr-backend/infrastructure" 12 | ) 13 | 14 | const ( 15 | envTLSCert = "TLS_CERT" 16 | envTLSKey = "TLS_KEY" 17 | ) 18 | 19 | func main() { 20 | 21 | // Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds. 22 | // Use a buffered channel to avoid missing signals as recommended for signal.Notify 23 | quit := make(chan os.Signal, 1) 24 | signal.Notify(quit, syscall.SIGTERM) 25 | 26 | router := infrastructure.Router() 27 | // Start server 28 | go func() { 29 | if os.Getenv(envTLSCert) == "" || os.Getenv(envTLSKey) == "" { 30 | router.Logger.Fatal(router.Start(":80")) 31 | } else { 32 | router.Logger.Fatal(router.StartTLS(":443", 33 | os.Getenv(envTLSCert), os.Getenv(envTLSKey))) 34 | } 35 | }() 36 | 37 | <-quit 38 | fmt.Println("Caught SIGTERM, shutting down") 39 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) 40 | defer cancel() 41 | 42 | if err := router.Shutdown(ctx); err != nil { 43 | router.Logger.Fatal("Error:", err) 44 | } 45 | fmt.Println("Exited app") 46 | } 47 | -------------------------------------------------------------------------------- /usecase/app_interactor.go: -------------------------------------------------------------------------------- 1 | package usecase 2 | 3 | import ( 4 | "github.com/uma-arai/sbcntr-backend/domain/model" 5 | "github.com/uma-arai/sbcntr-backend/domain/repository" 6 | "github.com/uma-arai/sbcntr-backend/utils" 7 | ) 8 | 9 | // AppInteractor ... 10 | type AppInteractor struct { 11 | AppRepository repository.AppRepositoryInterface 12 | } 13 | 14 | // GetItems ... 15 | func (interactor *AppInteractor) GetItems(favorite string) (app model.Items, err error) { 16 | var query string 17 | var args interface{} 18 | if favorite == "true" { 19 | query = "favorite = ?" 20 | args = true 21 | } else if favorite == "false" { 22 | query = "favorite = ?" 23 | args = false 24 | } else { 25 | query = "" 26 | args = "" 27 | } 28 | 29 | app, err = interactor.AppRepository.Find(query, args) 30 | if err != nil { 31 | err = utils.SetErrorMassage("10001E") 32 | return 33 | } 34 | 35 | return 36 | } 37 | 38 | // CreateItem ... 39 | func (interactor *AppInteractor) CreateItem(input model.Item) (response model.Response, err error) { 40 | response, err = interactor.AppRepository.Create(input) 41 | if err != nil { 42 | err = utils.SetErrorMassage("10001E") 43 | return 44 | } 45 | 46 | return 47 | } 48 | 49 | // UpdateFavoriteAttr ... 50 | func (interactor *AppInteractor) UpdateFavoriteAttr(input model.Item) (item model.Item, err error) { 51 | item, err = interactor.AppRepository.Update(map[string]interface{}{"Favorite": input.Favorite}, "id = ?", input.ID) 52 | 53 | if err != nil { 54 | err = utils.SetErrorMassage("10001E") 55 | return 56 | } 57 | 58 | return 59 | } 60 | -------------------------------------------------------------------------------- /usecase/notifiation_interactor.go: -------------------------------------------------------------------------------- 1 | package usecase 2 | 3 | import ( 4 | "github.com/uma-arai/sbcntr-backend/domain/model" 5 | "github.com/uma-arai/sbcntr-backend/domain/repository" 6 | "github.com/uma-arai/sbcntr-backend/utils" 7 | ) 8 | 9 | // NotificationInteractor ... 10 | type NotificationInteractor struct { 11 | NotificationRepository repository.NotificationRepositoryInterface 12 | } 13 | 14 | // GetNotifications ... 15 | func (interactor *NotificationInteractor) GetNotifications(id string) (app model.Notifications, err error) { 16 | if id == "" { 17 | app, err = interactor.NotificationRepository.FindAll() 18 | if err != nil { 19 | err = utils.SetErrorMassage("10001E") 20 | return 21 | } 22 | 23 | } else { 24 | app, err = interactor.NotificationRepository.Where(id) 25 | if err != nil { 26 | err = utils.SetErrorMassage("10001E") 27 | return 28 | } 29 | } 30 | 31 | return 32 | } 33 | 34 | // GetUnreadNotificationCount ... 35 | func (interactor *NotificationInteractor) GetUnreadNotificationCount() (count model.NotificationCount, err error) { 36 | 37 | count, err = interactor.NotificationRepository.Count("unread = ?", true) 38 | if err != nil { 39 | err = utils.SetErrorMassage("10001E") 40 | return 41 | } 42 | 43 | return 44 | } 45 | 46 | // MarkNotificationsRead ... 47 | func (interactor *NotificationInteractor) MarkNotificationsRead() (notification model.Notification, err error) { 48 | notification, err = interactor.NotificationRepository.Update(map[string]interface{}{"Unread": false}, "unread = ?", true) 49 | 50 | if err != nil { 51 | err = utils.SetErrorMassage("10001E") 52 | return 53 | } 54 | 55 | return 56 | } 57 | -------------------------------------------------------------------------------- /infrastructure/router.go: -------------------------------------------------------------------------------- 1 | package infrastructure 2 | 3 | import ( 4 | "os" 5 | 6 | "github.com/labstack/echo/v4" 7 | "github.com/labstack/echo/v4/middleware" 8 | "github.com/labstack/gommon/log" 9 | handlers "github.com/uma-arai/sbcntr-backend/handler" 10 | ) 11 | 12 | // Router ... 13 | func Router() *echo.Echo { 14 | e := echo.New() 15 | 16 | // Middleware 17 | logger := middleware.LoggerWithConfig(middleware.LoggerConfig{ 18 | Format: `{"id":"${id}","time":"${time_rfc3339}","remote_ip":"${remote_ip}",` + 19 | `"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}",` + 20 | `"status":${status},"error":"${error}"}` + "\n", 21 | Output: os.Stdout, 22 | }) 23 | e.Use(logger) 24 | e.Use(middleware.Recover()) 25 | e.Logger.SetLevel(log.INFO) 26 | e.HideBanner = true 27 | e.HidePort = false 28 | 29 | AppHandler := handlers.NewAppHandler(NewSQLHandler()) 30 | healthCheckHandler := handlers.NewHealthCheckHandler() 31 | helloWorldHandler := handlers.NewHelloWorldHandler() 32 | NotificationHandler := handlers.NewNotificationHandler(NewSQLHandler()) 33 | 34 | e.GET("/", healthCheckHandler.HealthCheck()) 35 | e.GET("/healthcheck", healthCheckHandler.HealthCheck()) 36 | e.GET("/v1/helloworld", helloWorldHandler.SayHelloWorld()) 37 | 38 | e.GET("/v1/Items", AppHandler.GetItems()) 39 | e.POST("/v1/Item", AppHandler.CreateItem()) 40 | e.POST("/v1/Item/favorite", AppHandler.UpdateFavoriteAttr()) 41 | 42 | e.GET("/v1/Notifications", NotificationHandler.GetNotifications()) 43 | e.GET("/v1/Notifications/Count", NotificationHandler.GetUnreadNotificationCount()) 44 | e.POST("/v1/Notifications/Read", NotificationHandler.PostNotificationsRead()) 45 | 46 | return e 47 | } 48 | -------------------------------------------------------------------------------- /README_EN.md: -------------------------------------------------------------------------------- 1 | # sbcntr-backend 2 | 3 | The repository for sbcntr backend API application. 4 | 5 | ## Overview 6 | This Repository provides demo web application coded by golang. This app is consist of three APIs. 7 | - Healthcheck API 8 | - Hello World API (turn inside the container) 9 | - DB Access API(Notification, Item) 10 | 11 | ## Prerequisite 12 | - Before start, you should build go runtime with go 1.16. 13 | - Please clone this repository according to GOPATH env and move to this directory "sbcntr-backend". 14 | - You will find main directory in "$GOPATH/src/github.com/uma-arai/sbcntr-backend" 15 | - Download required go module for this app by following command. 16 | ```bash 17 | ❯ go get golang.org/x/lint/golint 18 | ❯ go install 19 | ❯ go mod download 20 | ``` 21 | - Set following env variables to connect DB. 22 | - DB_HOST 23 | - DB_USERNAME 24 | - DB_PASSWORD 25 | - DB_NAME 26 | 27 | ## How to Build and Deploy 28 | ### Golang 29 | ```bash 30 | ❯ make all 31 | ``` 32 | ### Docker 33 | ```bash 34 | ❯ docker build -t sbcntr-backend:latest . 35 | ❯ docker images 36 | REPOSITORY TAG IMAGE ID CREATED SIZE 37 | sbcntr-backend latest cdb20b70f267 58 minutes ago 4.45MB 38 | : 39 | ❯ docker run -d -p 80:80 sbcntr-backend:latest 40 | ``` 41 | 42 | ### REST API (after deploy) 43 | ```bash 44 | ❯ curl http://localhost:80/v1/helloworld 45 | {"data":"Hello world"} 46 | 47 | ❯ curl http://localhost:80/healthcheck 48 | null 49 | ``` 50 | 51 | ## Options 52 | - If you want to deploy this app with SSL signed by self, set env 53 | variables TLS_CERT and TLS_KEY. 54 | 55 | ## Notes 56 | - We just check this operation only Mac OS (version 10.15). 57 | -------------------------------------------------------------------------------- /utils/messages.go: -------------------------------------------------------------------------------- 1 | package utils 2 | 3 | import ( 4 | "encoding/json" 5 | "errors" 6 | "fmt" 7 | "github.com/labstack/echo/v4" 8 | "github.com/uma-arai/sbcntr-backend/domain/model" 9 | ) 10 | 11 | var messageConfig map[string]interface{} 12 | 13 | func init() { 14 | err := json.Unmarshal([]byte(MessagesConfig), &messageConfig) 15 | if err != nil { 16 | panic(err) 17 | } 18 | } 19 | 20 | // GetMessageStatusCode is ... 21 | func GetMessageStatusCode(messageCode string) int { 22 | return int((messageConfig[messageCode].(map[string]interface{}))["statusCode"].(float64)) 23 | } 24 | 25 | // GetMessageMessageCode is ... 26 | func GetMessageMessageCode(messageCode string) string { 27 | return (messageConfig[messageCode].(map[string]interface{}))["messageCode"].(string) 28 | } 29 | 30 | // GetMessageMessage is ... 31 | func GetMessageMessage(lang string, messageCode string, args ...interface{}) string { 32 | llang := lang 33 | 34 | if !(llang == "ja" || llang == "en") { 35 | llang = "ja" 36 | } 37 | return fmt.Sprintf(((messageConfig[messageCode].(map[string]interface{}))["message"].(map[string]interface{}))[llang].(string), args...) 38 | } 39 | 40 | // GetErrorMassage is ... 41 | func GetErrorMassage(context interface{}, lang string, err1 error) (err error) { 42 | c := context.(echo.Context) 43 | messageCode := err1.Error() 44 | 45 | errStatusCode := GetMessageStatusCode(messageCode) 46 | errMessageCode := GetMessageMessageCode(messageCode) 47 | errMessage := GetMessageMessage(lang, messageCode) 48 | 49 | errorMessages := &model.ErrorMessages{ 50 | Code: errMessageCode, 51 | Message: errMessage, 52 | } 53 | 54 | return c.JSON(errStatusCode, errorMessages) 55 | } 56 | 57 | // SetErrorMassage is ... 58 | func SetErrorMassage(messageCode string) (err error) { 59 | err = errors.New(messageCode) 60 | return 61 | } 62 | -------------------------------------------------------------------------------- /domain/repository/app_repository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "github.com/uma-arai/sbcntr-backend/domain/model" 5 | "github.com/uma-arai/sbcntr-backend/interface/database" 6 | ) 7 | 8 | // AppRepositoryInterface ... 9 | type AppRepositoryInterface interface { 10 | FindAll() (items model.Items, err error) 11 | Find(query interface{}, args ...interface{}) (items model.Items, err error) 12 | Create(input model.Item) (out model.Response, err error) 13 | Update(value map[string]interface{}, query interface{}, args ...interface{}) (item model.Item, err error) 14 | } 15 | 16 | // AppRepository ... 17 | type AppRepository struct { 18 | database.SQLHandler 19 | } 20 | 21 | // FindAll ... 22 | func (repo *AppRepository) FindAll() (items model.Items, err error) { 23 | repo.SQLHandler.Scan(&items.Data, "id desc") 24 | return 25 | } 26 | 27 | // Find ... 28 | func (repo *AppRepository) Find(query interface{}, args ...interface{}) (items model.Items, err error) { 29 | repo.SQLHandler.Where(&items.Data, query, args...) 30 | return 31 | } 32 | 33 | // Create ... 34 | func (repo *AppRepository) Create(input model.Item) (out model.Response, err error) { 35 | _, err = repo.SQLHandler.Create(&input) 36 | 37 | if err != nil { 38 | return model.Response{ 39 | Code: 400, 40 | Message: "Create error", 41 | }, err 42 | } 43 | 44 | return model.Response{ 45 | Code: 200, 46 | Message: "OK", 47 | }, nil 48 | } 49 | 50 | // Update ... 51 | func (repo *AppRepository) Update(value map[string]interface{}, query interface{}, args ...interface{}) (item model.Item, err error) { 52 | // NOTE: When update with struct, GORM will only update non-zero fields, 53 | // you might want to use map to update attributes or use Select to specify fields to update 54 | repo.SQLHandler.Update(&item, value, query, args...) 55 | 56 | return 57 | } 58 | -------------------------------------------------------------------------------- /domain/repository/notification_repository.go: -------------------------------------------------------------------------------- 1 | package repository 2 | 3 | import ( 4 | "github.com/uma-arai/sbcntr-backend/domain/model" 5 | "github.com/uma-arai/sbcntr-backend/interface/database" 6 | ) 7 | 8 | // NotificationRepositoryInterface ... 9 | type NotificationRepositoryInterface interface { 10 | Where(id string) (account model.Notifications, err error) 11 | FindAll() (notifications model.Notifications, err error) 12 | Count(query interface{}, args ...interface{}) (data model.NotificationCount, err error) 13 | Update(value map[string]interface{}, query interface{}, args ...interface{}) (notification model.Notification, err error) 14 | } 15 | 16 | // NotificationRepository .... 17 | type NotificationRepository struct { 18 | database.SQLHandler 19 | } 20 | 21 | // Where ... 22 | func (repo *NotificationRepository) Where(id string) (notifications model.Notifications, err error) { 23 | repo.SQLHandler.Where(¬ifications.Data, "id = ?", id) 24 | return 25 | } 26 | 27 | // FindAll ... 28 | func (repo *NotificationRepository) FindAll() (notifications model.Notifications, err error) { 29 | repo.SQLHandler.Scan(¬ifications.Data, "id desc") 30 | return 31 | } 32 | 33 | // Count ... 34 | func (repo *NotificationRepository) Count(query interface{}, args ...interface{}) (data model.NotificationCount, err error) { 35 | var count int 36 | repo.SQLHandler.Count(&count, &model.Notification{}, query, args...) 37 | 38 | return model.NotificationCount{Data: count}, nil 39 | } 40 | 41 | // Update ... 42 | func (repo *NotificationRepository) Update(value map[string]interface{}, query interface{}, args ...interface{}) (notification model.Notification, err error) { 43 | // NOTE: When update with struct, GORM will only update non-zero fields, 44 | // you might want to use map to update attributes or use Select to specify fields to update 45 | repo.SQLHandler.Update(¬ification, value, query, args...) 46 | 47 | return 48 | } 49 | -------------------------------------------------------------------------------- /handler/notification_handler.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "net/http" 5 | 6 | "github.com/labstack/echo/v4" 7 | "github.com/uma-arai/sbcntr-backend/domain/repository" 8 | "github.com/uma-arai/sbcntr-backend/interface/database" 9 | "github.com/uma-arai/sbcntr-backend/usecase" 10 | "github.com/uma-arai/sbcntr-backend/utils" 11 | ) 12 | 13 | // NotificationHandler ... 14 | type NotificationHandler struct { 15 | Interactor usecase.NotificationInteractor 16 | } 17 | 18 | // NewNotificationHandler ... 19 | func NewNotificationHandler(sqlHandler database.SQLHandler) *NotificationHandler { 20 | return &NotificationHandler{ 21 | Interactor: usecase.NotificationInteractor{ 22 | NotificationRepository: &repository.NotificationRepository{ 23 | SQLHandler: sqlHandler, 24 | }, 25 | }, 26 | } 27 | } 28 | 29 | // GetNotifications ... 30 | func (handler *NotificationHandler) GetNotifications() echo.HandlerFunc { 31 | return func(c echo.Context) (err error) { 32 | 33 | id := c.QueryParam("id") 34 | resJSON, err := handler.Interactor.GetNotifications(id) 35 | if err != nil { 36 | return utils.GetErrorMassage(c, "en", err) 37 | } 38 | 39 | return c.JSON(http.StatusOK, resJSON) 40 | } 41 | } 42 | 43 | // GetUnreadNotificationCount ... 44 | func (handler *NotificationHandler) GetUnreadNotificationCount() echo.HandlerFunc { 45 | return func(c echo.Context) (err error) { 46 | resJSON, err := handler.Interactor.GetUnreadNotificationCount() 47 | if err != nil { 48 | return utils.GetErrorMassage(c, "en", err) 49 | } 50 | 51 | return c.JSON(http.StatusOK, resJSON) 52 | } 53 | } 54 | 55 | // PostNotificationsRead ... 56 | func (handler *NotificationHandler) PostNotificationsRead() echo.HandlerFunc { 57 | return func(c echo.Context) (err error) { 58 | resJSON, err := handler.Interactor.MarkNotificationsRead() 59 | if err != nil { 60 | return utils.GetErrorMassage(c, "en", err) 61 | } 62 | 63 | return c.JSON(http.StatusOK, resJSON) 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # sbcntr-backend 2 | 書籍用のバックエンドAPI用のダウンロードリポジトリです。 3 | 4 | ## 概要 5 | echoフレームワークを利用した、Golang製のAPIサーバーです。 6 | Golangには数多くのフレームワークがあります。 7 | REST APIサーバーを実装するために十分な機能が備わっていることや、ドキュメントが充実していることから今回echoを選択しています。 8 | 9 | APIサーバーとDB(MySQL)の接続はO/RマッパライブラリであるGORM[^gorm]を利用しています。 10 | 11 | [^gorm]: https://gorm.io/ 12 | 13 | バックエンドアプリケーションは次の2つのサービスを備えています。 14 | また、各APIエンドポイントの接頭辞として、`/v1`が付与されます。 15 | 16 | - Itemサービス(アイテムサービス) 17 | - DB接続なしで画面表示をするためのHelloworldを返却します(/helloworld) 18 | - Itemテーブルに登録されているデータを返却します(/Item) 19 | - フロントエンドから入力した情報を元にItemを新規作成します(/Item) 20 | - Itemへお気に入りマークのOn/Offを可能とします(/Item/favorite) 21 | - Notifiationサービス(通知サービス) 22 | - Notificationテーブルに登録されているデータを返却します(/Notifications) 23 | - クエリパラメータでidを渡すことで特定のデータのみを返却します 24 | - 通知バッジを表示するために未読の通知件数を返却します(/Notifiactions/Count) 25 | - 未読の通知を一括で既読に変更します(/Notifications/Read) 26 | 27 | ## 利用想定 28 | 本書の内容に沿って、ご利用ください。 29 | 30 | ## ローカル利用方法 31 | 32 | ### 事前準備 33 | - Goのバージョンは16系を利用します。 34 | - GOPATHの場所に応じて適切なディレクトリに、このリポジトリのコードをクローンしてください。 35 | - 次のコマンドを利用してモジュールをダウンロードしてください。 36 | 37 | ```bash 38 | $ go get golang.org/x/lint/golint 39 | $ go install 40 | $ go mod download 41 | ``` 42 | 43 | - 本バックエンドAPIではDB接続があります。DB接続のために次の環境変数を設定してください。 44 | - DB_HOST 45 | - DB_USERNAME 46 | - DB_PASSWORD 47 | - DB_NAME 48 | 49 | ### DBの用意 50 | 51 | 事前にローカルでMySQLサーバを立ち上げてください。 52 | 53 | https://dev.mysql.com/downloads/mysql/ 54 | 55 | ### ビルド&デプロイ 56 | 57 | #### ローカルで動かす場合 58 | 59 | ```bash 60 | ❯ make all 61 | ``` 62 | 63 | #### Dockerから動かす場合 64 | 65 | ```bash 66 | ❯ docker build -t sbcntr-backend:latest . 67 | ❯ docker images 68 | REPOSITORY TAG IMAGE ID CREATED SIZE 69 | sbcntr-backend latest cdb20b70f267 58 minutes ago 4.45MB 70 | : 71 | ❯ docker run -d -p 80:80 sbcntr-backend:latest 72 | ``` 73 | 74 | ### デプロイ後の疎通確認 75 | 76 | ```bash 77 | ❯ curl http://localhost:80/v1/helloworld 78 | {"data":"Hello world"} 79 | 80 | ❯ curl http://localhost:80/healthcheck 81 | null 82 | ``` 83 | 84 | ## 注意事項 85 | - Mac OS Bigsur 11.6でのみ動作確認しています。 86 | -------------------------------------------------------------------------------- /infrastructure/sql_handler.go: -------------------------------------------------------------------------------- 1 | package infrastructure 2 | 3 | import ( 4 | "fmt" 5 | "gorm.io/driver/mysql" 6 | 7 | "github.com/uma-arai/sbcntr-backend/utils" 8 | _ "gorm.io/driver/mysql" // for access to mysql 9 | "gorm.io/gorm" 10 | ) 11 | 12 | // DB ... 13 | type DB struct { 14 | Host string 15 | Username string 16 | Password string 17 | DBName string 18 | Connect *gorm.DB 19 | } 20 | 21 | // SQLHandler ... SQL handler struct 22 | type SQLHandler struct { 23 | Conn *gorm.DB 24 | } 25 | 26 | // NewSQLHandler ... 27 | func NewSQLHandler() *SQLHandler { 28 | 29 | c := utils.NewConfigDB() 30 | 31 | USER := c.MySQL.Username 32 | PASS := c.MySQL.Password 33 | PROTOCOL := c.MySQL.Protocol 34 | DBNAME := c.MySQL.DBName 35 | 36 | CONNECT := USER + ":" + PASS + "@" + PROTOCOL + "/" + DBNAME + "?parseTime=true&loc=Asia%2FTokyo" 37 | conn, err := gorm.Open(mysql.Open(CONNECT), &gorm.Config{}) 38 | 39 | if err != nil { 40 | // Note: ハンズオンでパニックすると辛いのでエラーハンドリングはコメントアウトしています 41 | // panic(err.Error()) 42 | fmt.Print("Error: No database connection established.") 43 | } 44 | sqlHandler := new(SQLHandler) 45 | sqlHandler.Conn = conn 46 | 47 | return sqlHandler 48 | } 49 | 50 | // Where ... 51 | func (handler *SQLHandler) Where(out interface{}, query interface{}, args ...interface{}) interface{} { 52 | if query == "" { 53 | return handler.Conn.Find(out) 54 | } 55 | 56 | return handler.Conn.Where(query, args).Find(out) 57 | } 58 | 59 | // Scan ... 60 | func (handler SQLHandler) Scan(out interface{}, order string) interface{} { 61 | return handler.Conn.Order(order).Find(out) 62 | } 63 | 64 | // Count ... 65 | func (handler *SQLHandler) Count(out *int, model interface{}, query interface{}, args ...interface{}) interface{} { 66 | out64 := int64(*out) 67 | return handler.Conn.Model(model).Where(query, args).Count(&out64) 68 | } 69 | 70 | // Create ... 71 | func (handler *SQLHandler) Create(input interface{}) (result interface{}, err error) { 72 | fmt.Println(input) 73 | response := handler.Conn.Create(input) 74 | 75 | return nil, response.Error 76 | } 77 | 78 | // Update ... 79 | func (handler *SQLHandler) Update(out interface{}, value interface{}, query interface{}, args ...interface{}) interface{} { 80 | return handler.Conn.Model(out).Where(query, args).Updates(value) 81 | } 82 | -------------------------------------------------------------------------------- /handler/app_handler.go: -------------------------------------------------------------------------------- 1 | package handlers 2 | 3 | import ( 4 | "net/http" 5 | "time" 6 | 7 | "github.com/uma-arai/sbcntr-backend/domain/model" 8 | 9 | "github.com/labstack/echo/v4" 10 | "github.com/uma-arai/sbcntr-backend/domain/repository" 11 | "github.com/uma-arai/sbcntr-backend/interface/database" 12 | "github.com/uma-arai/sbcntr-backend/usecase" 13 | "github.com/uma-arai/sbcntr-backend/utils" 14 | ) 15 | 16 | // AppHandler ... 17 | type AppHandler struct { 18 | Interactor usecase.AppInteractor 19 | } 20 | 21 | // NewAppHandler ... 22 | func NewAppHandler(sqlHandler database.SQLHandler) *AppHandler { 23 | return &AppHandler{ 24 | Interactor: usecase.AppInteractor{ 25 | AppRepository: &repository.AppRepository{ 26 | SQLHandler: sqlHandler, 27 | }, 28 | }, 29 | } 30 | } 31 | 32 | // GetItems ... 33 | func (handler *AppHandler) GetItems() echo.HandlerFunc { 34 | return func(c echo.Context) (err error) { 35 | favorite := c.QueryParam("favorite") 36 | resJSON, err := handler.Interactor.GetItems(favorite) 37 | if err != nil { 38 | return utils.GetErrorMassage(c, "en", err) 39 | } 40 | 41 | return c.JSON(http.StatusOK, resJSON) 42 | } 43 | } 44 | 45 | // CreateItem ... 46 | func (handler *AppHandler) CreateItem() echo.HandlerFunc { 47 | return func(c echo.Context) (err error) { 48 | i := new(model.Item) 49 | if err = c.Bind(i); err != nil { 50 | return echo.NewHTTPError(http.StatusBadRequest, err.Error()) 51 | } 52 | 53 | input := model.Item{ 54 | Title: i.Title, 55 | Name: i.Name, 56 | Favorite: false, 57 | Img: i.Img, 58 | CreatedAt: time.Now().Format("2006-01-02 15:04:05"), 59 | UpdatedAt: time.Now().Format("2006-01-02 15:04:05"), 60 | } 61 | 62 | if input.Name == "" { 63 | return c.JSON(http.StatusBadRequest, model.Response{ 64 | Message: "No name param found", 65 | }) 66 | } 67 | 68 | if input.Title == "" { 69 | return c.JSON(http.StatusBadRequest, model.Response{ 70 | Message: "No title param found", 71 | }) 72 | 73 | } 74 | 75 | if input.Img == "" { 76 | return c.JSON(http.StatusBadRequest, model.Response{ 77 | Message: "No img param found", 78 | }) 79 | } 80 | 81 | resJSON, err := handler.Interactor.CreateItem(input) 82 | if err != nil { 83 | return utils.GetErrorMassage(c, "en", err) 84 | } 85 | 86 | return c.JSON(http.StatusOK, resJSON) 87 | } 88 | } 89 | 90 | // UpdateFavoriteAttr ... 91 | func (handler *AppHandler) UpdateFavoriteAttr() echo.HandlerFunc { 92 | return func(c echo.Context) (err error) { 93 | input := new(model.Item) 94 | if err = c.Bind(input); err != nil { 95 | return echo.NewHTTPError(http.StatusBadRequest, err.Error()) 96 | } 97 | 98 | resJSON, err := handler.Interactor.UpdateFavoriteAttr(*input) 99 | if err != nil { 100 | return utils.GetErrorMassage(c, "en", err) 101 | } 102 | 103 | return c.JSON(http.StatusOK, resJSON) 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= 2 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 3 | github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE= 4 | github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= 5 | github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY= 6 | github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I= 7 | github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= 8 | github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= 9 | github.com/jinzhu/now v1.1.2 h1:eVKgfIdy9b6zbWBMgFpfDPoAMifwSZagU9HmEU6zgiI= 10 | github.com/jinzhu/now v1.1.2/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= 11 | github.com/labstack/echo/v4 v4.6.1 h1:OMVsrnNFzYlGSdaiYGHbgWQnr+JM7NG+B9suCPie14M= 12 | github.com/labstack/echo/v4 v4.6.1/go.mod h1:RnjgMWNDB9g/HucVWhQYNQP9PvbYf6adqftqryo7s9k= 13 | github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0= 14 | github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k= 15 | github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE= 16 | github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= 17 | github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= 18 | github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s= 19 | github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ= 20 | github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= 21 | github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y= 22 | github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= 23 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 24 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 25 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= 26 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= 27 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= 28 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 29 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 30 | github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8= 31 | github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4= 32 | github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= 33 | golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 h1:HWj/xjIHfjYU5nVXpTM0s39J9CbLn7Cc5a7IC5rwsMQ= 34 | golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= 35 | golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= 36 | golang.org/x/net v0.0.0-20210913180222-943fd674d43e h1:+b/22bPvDYt4NPDcy4xAGCmON713ONAWFeY3Z7I3tR8= 37 | golang.org/x/net v0.0.0-20210913180222-943fd674d43e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= 38 | golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= 39 | golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 40 | golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 41 | golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 42 | golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 43 | golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= 44 | golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 45 | golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 46 | golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0 h1:xrCZDmdtoloIiooiA9q0OQb9r8HejIHYoHGhGCe1pGg= 47 | golang.org/x/sys v0.0.0-20210910150752-751e447fb3d0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 48 | golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= 49 | golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 50 | golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 51 | golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk= 52 | golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= 53 | golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE= 54 | golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= 55 | golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= 56 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= 57 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= 58 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= 59 | gorm.io/driver/mysql v1.1.2 h1:OofcyE2lga734MxwcCW9uB4mWNXMr50uaGRVwQL2B0M= 60 | gorm.io/driver/mysql v1.1.2/go.mod h1:4P/X9vSc3WTrhTLZ259cpFd6xKNYiSSdSZngkSBGIMM= 61 | gorm.io/gorm v1.21.12/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= 62 | gorm.io/gorm v1.21.15 h1:gAyaDoPw0lCyrSFWhBlahbUA1U4P5RViC1uIqoB+1Rk= 63 | gorm.io/gorm v1.21.15/go.mod h1:F+OptMscr0P2F2qU97WT1WimdH9GaQPoDW7AYd5i2Y0= 64 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2021 uma-arai 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------