Skip to content

Commit

Permalink
fix: handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sugar-cat7 committed Mar 16, 2024
1 parent 641ddcc commit 2daf30d
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 80 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ ddl:
@echo "Generating new ddl files..."
goose -dir ./service/common-api/schema/migration create $(name) sql
@echo "Generating new ddl files...done"
local-db-setup:
local:
@echo "Setting up local database..."
docker-compose -f ./docker/docker-compose.local.yml up -d
docker-compose -f ./docker/docker-compose.local.yml up
@echo "Setting up local database...done"
migrate:
@echo "Migrating database..."
Expand Down
3 changes: 3 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM golang:1.20.4-bullseye

RUN go install github.com/cosmtrek/air@latest
27 changes: 19 additions & 8 deletions docker/docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
version: '3.8'

services:
postgres:
container_name: vspo-common-db
image: postgres:latest
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: vspo
# postgres:
# container_name: vspo-common-db
# image: postgres:latest
# environment:
# POSTGRES_USER: user
# POSTGRES_PASSWORD: password
# POSTGRES_DB: vspo
# ports:
# - "5432:5432"
go:
container_name: vspo-common-go
volumes:
- ../service/common-api:/project/
working_dir: /project
tty: true
build: "./docker"
ports:
- "5432:5432"
- 8080:8080
- 8081:8081
command: sh -c 'go mod tidy && air'
44 changes: 44 additions & 0 deletions service/common-api/.air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
args_bin = []
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/index.go"
delay = 0
exclude_dir = ["assets", "tmp", "vendor", "testdata"]
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
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 service/common-api/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.vercel
tmp
7 changes: 7 additions & 0 deletions service/common-api/cmd/index.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"log"
"net/http"

http_handler "github.com/sugar-cat7/vspo-portal/service/common-api/infra/http"
Expand All @@ -9,4 +10,10 @@ import (
// debug
func main() {
http.HandleFunc("/", http_handler.Run)
port := "8080"
log.Printf("Starting server on port %s...\n", port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatalf("Error occurred while starting the server: %v", err)
}
}
8 changes: 8 additions & 0 deletions service/common-api/infra/dependency/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
type Dependency struct {
CreatorInteractor usecase.CreatorInteractor
VideosInteractor usecase.VideoInteractor
ChannelInteractor usecase.ChannelInteractor
}

func (d *Dependency) Inject(ctx context.Context, e *environment.Environment) *Dependency {
creatorRepository := repository.NewCreator()
videosRepository := repository.NewVideo()
channelRepository := repository.NewChannel()
youtubeClient := youtube.NewService(e.YoutubeEnvironment.YoutubeAPIKey)
twitchClient := twitch.NewService(twitch.NewClient(e))
twitcastingClient := twitcasting.NewService(twitcasting.NewClient(e))
Expand All @@ -32,9 +34,15 @@ func (d *Dependency) Inject(ctx context.Context, e *environment.Environment) *De
twitchClient,
twitcastingClient,
)
channelInteractor := usecase.NewChannelInteractor(
creatorRepository,
channelRepository,
youtubeClient,
)

return &Dependency{
CreatorInteractor: creatorInteractor,
VideosInteractor: videoInteractor,
ChannelInteractor: channelInteractor,
}
}
20 changes: 10 additions & 10 deletions service/common-api/infra/environment/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ type Environment struct {
}

type ServerEnvironment struct {
ENV string `env:"ENV,required"`
LogLevel string `env:"LOG_LEVEL,required"`
ENV string `env:"ENV,required" envDefault:"local"`
LogLevel string `env:"LOG_LEVEL,required" envDefault:"debug"`
}

type DatabaseEnvironment struct {
DBHost string `env:"DB_HOST,required"`
DBUser string `env:"DB_USER,required"`
DBPassword string `env:"DB_PASSWORD,required"`
DBDatabase string `env:"DB_DATABASE,required"`
DBHost string `env:"DB_HOST,required" envDefault:"localhost"`
DBUser string `env:"DB_USER,required" envDefault:"user"`
DBPassword string `env:"DB_PASSWORD,required" envDefault:"password"`
DBDatabase string `env:"DB_DATABASE,required" envDefault:"vspo"`
}

type YoutubeEnvironment struct {
YoutubeAPIKey string `env:"YOUTUBE_API_KEY,required"`
YoutubeAPIKey string `env:"YOUTUBE_API_KEY,required" envDefault:"xxx"`
}

type TwitchEnvironment struct {
TwitchClientID string `env:"TWITCH_CLIENT_ID,required"`
TwitchClientSecret string `env:"TWITCH_CLIENT_SECRET,required"`
TwitchClientID string `env:"TWITCH_CLIENT_ID,required" envDefault:"xxx"`
TwitchClientSecret string `env:"TWITCH_CLIENT_SECRET,required" envDefault:"xxx"`
}

type TwitcastingEnvironment struct {
TwitcastingAccessToken string `env:"TWITCASTING_ACCESS_TOKEN,required"`
TwitcastingAccessToken string `env:"TWITCASTING_ACCESS_TOKEN,required" envDefault:"xxx"`
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package channel

import (
"context"

api "github.com/sugar-cat7/vspo-portal/service/common-api/generated/cron"
"github.com/sugar-cat7/vspo-portal/service/common-api/usecase"
)

// Handler is an interface for handling channel operations.
type Handler interface {
ChannelsPost(ctx context.Context, req *api.ChannelsPostReq) (api.ChannelsPostRes, error)
}

// CH is Handler implementation.
type CH struct {
channelInteractor usecase.ChannelInteractor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package video

import (
"context"

api "github.com/sugar-cat7/vspo-portal/service/common-api/generated/cron"
"github.com/sugar-cat7/vspo-portal/service/common-api/usecase"
)

// Handler is an interface for handling clip operations.
type Handler interface {
VideosPost(ctx context.Context, req *api.VideosPostReq) (api.VideosPostRes, error)
}

// VH is Handler implementation.
type VH struct {
videoInteractor usecase.VideoInteractor
Expand Down
9 changes: 9 additions & 0 deletions service/common-api/infra/http/cron/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
oas "github.com/sugar-cat7/vspo-portal/service/common-api/generated/cron"
"github.com/sugar-cat7/vspo-portal/service/common-api/infra/http/cron/internal/handler/channel"
"github.com/sugar-cat7/vspo-portal/service/common-api/infra/http/cron/internal/handler/video"
"github.com/sugar-cat7/vspo-portal/service/common-api/usecase"
)

// Compile-time check for Handler.
Expand All @@ -14,3 +15,11 @@ type RootHandler struct {
channel.CH
video.VH
}

// NewHandler creates a new instance of a RootHandler.
func NewHandler(channelInteractor usecase.ChannelInteractor, videoInteractor usecase.VideoInteractor) *RootHandler {
return &RootHandler{
CH: channel.NewHandler(channelInteractor),
VH: video.NewHandler(videoInteractor),
}
}
32 changes: 11 additions & 21 deletions service/common-api/infra/http/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/sugar-cat7/vspo-portal/service/common-api/infra/dependency"
"github.com/sugar-cat7/vspo-portal/service/common-api/infra/environment"
cron_handler "github.com/sugar-cat7/vspo-portal/service/common-api/infra/http/cron"

http_handler "github.com/sugar-cat7/vspo-portal/service/common-api/infra/http/server"
"github.com/sugar-cat7/vspo-portal/service/common-api/pkg/logger"
"go.uber.org/zap"
)

// Run starts the server.
func Run(w http.ResponseWriter, r *http.Request) {
e := &environment.Environment{}
if err := env.Parse(e); err != nil {
Expand All @@ -32,37 +33,26 @@ func Run(w http.ResponseWriter, r *http.Request) {
logger.Info("[START] server.")
// Cron
cs, err := cron.NewServer(
&cron_handler.RootHandler{},
&cron_handler.SecurityHandler{},
cron_handler.NewHandler(
d.ChannelInteractor,
d.VideosInteractor,
),
cron_handler.NewSecurityHandler(),
)
if err != nil {
panic(err)
}
// API
hs, err := api.NewServer(
&http_handler.RootHandler{},
&http_handler.SecurityHandler{},
http_handler.NewHandler(
d.CreatorInteractor,
d.VideosInteractor),
http_handler.NewSecurityHandler(),
)
if err != nil {
panic(err)
}

if e.ServerEnvironment.ENV == "local" {
// In a local environment, listen on different ports
go func() {
logger.Info("Starting cron server on :8081")
if err := http.ListenAndServe(":8081", cs); err != nil {
logger.Error("Cron server failed to start", zap.Error(err))
}
}()

logger.Info("Starting API server on :8080")
if err := http.ListenAndServe(":8080", hs); err != nil {
logger.Error("API server failed to start", zap.Error(err))
}
return
}

// In a production environment, listen on the same port
pathSegments := strings.Split(r.URL.Path, "/")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package creator

import (
"context"

api "github.com/sugar-cat7/vspo-portal/service/common-api/generated/api"
"github.com/sugar-cat7/vspo-portal/service/common-api/usecase"
)

// Handler is an interface for handling creator operations.
type Handler interface {
CreatorsGet(ctx context.Context, params api.CreatorsGetParams) (api.CreatorsGetRes, error)
}

// CH is Handler implementation.
type CH struct {
creatorInteractor usecase.CreatorInteractor
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package video

import (
"context"

api "github.com/sugar-cat7/vspo-portal/service/common-api/generated/api"
)

// Handler is an interface for handling clip operations.
type Handler interface {
VideosGet(ctx context.Context, params api.VideosGetParams) (api.VideosGetRes, error)
VideosPut(ctx context.Context, req *api.VideosPutReq) (api.VideosPutRes, error)
VideosPost(ctx context.Context, req *api.VideosPostReq) (api.VideosPostRes, error)
}
import "github.com/sugar-cat7/vspo-portal/service/common-api/usecase"

// VH is Handler implementation.
type VH struct{}
type VH struct {
videoInteractor usecase.VideoInteractor
}

// NewHandler returns a new instance of a clip handler.
func NewHandler() VH {
return VH{}
func NewHandler(
videoInteractor usecase.VideoInteractor,
) VH {
return VH{
videoInteractor: videoInteractor,
}
}
9 changes: 9 additions & 0 deletions service/common-api/infra/http/server/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
oas "github.com/sugar-cat7/vspo-portal/service/common-api/generated/api"
"github.com/sugar-cat7/vspo-portal/service/common-api/infra/http/server/internal/handler/creator"
"github.com/sugar-cat7/vspo-portal/service/common-api/infra/http/server/internal/handler/video"
"github.com/sugar-cat7/vspo-portal/service/common-api/usecase"
)

// Compile-time check for Handler.
Expand All @@ -14,3 +15,11 @@ type RootHandler struct {
creator.CH
video.VH
}

// NewHandler creates a new instance of a RootHandler.
func NewHandler(creatorInteractor usecase.CreatorInteractor, videoInteractor usecase.VideoInteractor) *RootHandler {
return &RootHandler{
CH: creator.NewHandler(creatorInteractor),
VH: video.NewHandler(videoInteractor),
}
}

0 comments on commit 2daf30d

Please sign in to comment.