Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨feat: add API server implementation using Echo framework #87

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
384 changes: 382 additions & 2 deletions CREDITS

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,20 @@ mockgen -source=./app/infrastructure/database/connection.go -destination=./app/i
mockgen -source=./app/infrastructure/database/connection_manager.go -destination=./app/infrastructure/database/connection_manager_mock.go -package=database
# ./app/domain
mockgen -source=./app/domain/jrp/history/history_repository.go -destination=./app/domain/jrp/history/history_repository_mock.go -package=history
# ./app/presentation/api/jrp/server
mockgen -source=./app/presentation/api/jrp/server/server.go -destination=./app/presentation/api/jrp/server/server_mock.go -package=server
# ./app/presentation/cli/jrp/command
mockgen -source=./app/presentation/cli/jrp/command/command.go -destination=./app/presentation/cli/jrp/command/command_mock.go -package=command
# ./pkg/proxy
mockgen -source=./pkg/proxy/buffer.go -destination=./pkg/proxy/buffer_mock.go -package=proxy
mockgen -source=./pkg/proxy/cobra.go -destination=./pkg/proxy/cobra_mock.go -package=proxy
mockgen -source=./pkg/proxy/debug.go -destination=./pkg/proxy/debug_mock.go -package=proxy
mockgen -source=./pkg/proxy/echo.go -destination=./pkg/proxy/echo_mock.go -package=proxy
mockgen -source=./pkg/proxy/envconfig.go -destination=./pkg/proxy/envconfig_mock.go -package=proxy
mockgen -source=./pkg/proxy/gzip.go -destination=./pkg/proxy/gzip_mock.go -package=proxy
mockgen -source=./pkg/proxy/http.go -destination=./pkg/proxy/http_mock.go -package=proxy
mockgen -source=./pkg/proxy/io.go -destination=./pkg/proxy/io_mock.go -package=proxy
mockgen -source=./pkg/proxy/json.go -destination=./pkg/proxy/json_mock.go -package=proxy
mockgen -source=./pkg/proxy/keyboard.go -destination=./pkg/proxy/keyboard_mock.go -package=proxy
mockgen -source=./pkg/proxy/os.go -destination=./pkg/proxy/os_mock.go -package=proxy
mockgen -source=./pkg/proxy/pflag.go -destination=./pkg/proxy/pflag_mock.go -package=proxy
Expand All @@ -41,6 +45,7 @@ mockgen -source=./pkg/proxy/tablewriter.go -destination=./pkg/proxy/tablewriter_
mockgen -source=./pkg/utility/capture.go -destination=./pkg/utility/capture_mock.go -package=utility
mockgen -source=./pkg/utility/download_util.go -destination=./pkg/utility/download_util_mock.go -package=utility
mockgen -source=./pkg/utility/file_util.go -destination=./pkg/utility/file_util_mock.go -package=utility
mockgen -source=./pkg/utility/json_util.go -destination=./pkg/utility/json_util_mock.go -package=utility
mockgen -source=./pkg/utility/keyboard_util.go -destination=./pkg/utility/keyboard_util_mock.go -package=utility
mockgen -source=./pkg/utility/prompt_util.go -destination=./pkg/utility/prompt_util_mock.go -package=utility
mockgen -source=./pkg/utility/rand_util.go -destination=./pkg/utility/rand_util_mock.go -package=utility
Expand Down
85 changes: 85 additions & 0 deletions app/presentation/api/jrp/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package config

import (
"path/filepath"
"strings"

baseConfig "github.com/yanosea/jrp/v2/app/config"
"github.com/yanosea/jrp/v2/app/infrastructure/database"

"github.com/yanosea/jrp/v2/pkg/proxy"
"github.com/yanosea/jrp/v2/pkg/utility"
)

// JrpServerConfigurator is an interface that gets the configuration of the Jrp server application.
type JrpServerConfigurator interface {
GetConfig() (*JrpServerConfig, error)
}

// ServerConfigurator is a struct that implements the JrpServerConfigurator interface.
type ServerConfigurator struct {
*baseConfig.BaseConfigurator
}

// NewJrpServerConfigurator creates a new JrpServerConfigurator.
func NewJrpServerConfigurator(
envconfigProxy proxy.Envconfig,
fileUtil utility.FileUtil,
) JrpServerConfigurator {
return &ServerConfigurator{
BaseConfigurator: baseConfig.NewConfigurator(
envconfigProxy,
fileUtil,
),
}
}

// JrpServerConfig is a struct that contains the configuration of the Jrp server application.
type JrpServerConfig struct {
baseConfig.JrpConfig
JrpPort string
}

// envConfig is a struct that contains the environment variables.
type envConfig struct {
JrpPort string `envconfig:"JRP_SERVER_PORT" default:"8080"`
WnJpnDBType database.DBType `envconfig:"JRP_SERVER_WNJPN_DB_TYPE" default:"sqlite"`
WnJpnDBDsn string `envconfig:"JRP_SERVER_WNJPN_DB" default:"XDG_DATA_HOME/jrp/wnjpn.db"`
}

// GetConfig gets the configuration of the Jrp server application.
func (c *ServerConfigurator) GetConfig() (*JrpServerConfig, error) {
var env envConfig
if err := c.Envconfig.Process("", &env); err != nil {
return nil, err
}

config := &JrpServerConfig{
JrpConfig: baseConfig.JrpConfig{
WNJpnDBType: env.WnJpnDBType,
WNJpnDBDsn: env.WnJpnDBDsn,
},
JrpPort: env.JrpPort,
}

if config.WNJpnDBType == database.SQLite {
xdgDataHome, err := c.FileUtil.GetXDGDataHome()
if err != nil {
return nil, err
}

config.WNJpnDBDsn = strings.Replace(
config.WNJpnDBDsn,
"XDG_DATA_HOME",
xdgDataHome,
1,
)
if err := c.FileUtil.MkdirIfNotExist(
filepath.Dir(config.WNJpnDBDsn),
); err != nil {
return nil, err
}
}

return config, nil
}
181 changes: 181 additions & 0 deletions app/presentation/api/jrp/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package config

import (
"errors"
"reflect"
"testing"

baseConfig "github.com/yanosea/jrp/v2/app/config"
"github.com/yanosea/jrp/v2/app/infrastructure/database"

"github.com/yanosea/jrp/v2/pkg/proxy"
"github.com/yanosea/jrp/v2/pkg/utility"

"go.uber.org/mock/gomock"
)

func TestNewJrpServerConfigurator(t *testing.T) {
envconfig := proxy.NewEnvconfig()
fileUtil := utility.NewFileUtil(
proxy.NewGzip(),
proxy.NewIo(),
proxy.NewOs(),
)

type args struct {
envconfigProxy proxy.Envconfig
fileUtil utility.FileUtil
}
tests := []struct {
name string
args args
want JrpServerConfigurator
}{
{
name: "positive testing",
args: args{
envconfigProxy: envconfig,
fileUtil: fileUtil,
},
want: &ServerConfigurator{
BaseConfigurator: baseConfig.NewConfigurator(
envconfig,
fileUtil,
),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewJrpServerConfigurator(tt.args.envconfigProxy, tt.args.fileUtil); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewJrpServerConfigurator() = %v, want %v", got, tt.want)
}
})
}
}

func TestServerConfigurator_GetConfig(t *testing.T) {
type fields struct {
BaseConfigurator *baseConfig.BaseConfigurator
}
tests := []struct {
name string
fields fields
want *JrpServerConfig
wantErr bool
setup func(mockCtrl *gomock.Controller, tt *fields)
}{
{
name: "positive testing",
fields: fields{
BaseConfigurator: &baseConfig.BaseConfigurator{
Envconfig: nil,
FileUtil: nil,
}},
want: &JrpServerConfig{
JrpConfig: baseConfig.JrpConfig{
WNJpnDBType: database.SQLite,
WNJpnDBDsn: "~/.local/share/jrp/wnjpn.db",
},
},
wantErr: false,
setup: func(mockCtrl *gomock.Controller, tt *fields) {
mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl)
mockEnvconfig.EXPECT().Process("", gomock.Any()).DoAndReturn(
func(_ string, cfg *envConfig) error {
cfg.WnJpnDBType = database.SQLite
cfg.WnJpnDBDsn = "XDG_DATA_HOME/jrp/wnjpn.db"
return nil
})
mockFileUtil := utility.NewMockFileUtil(mockCtrl)
mockFileUtil.EXPECT().GetXDGDataHome().Return("~/.local/share", nil)
mockFileUtil.EXPECT().MkdirIfNotExist("~/.local/share/jrp").Return(nil)
tt.BaseConfigurator.Envconfig = mockEnvconfig
tt.BaseConfigurator.FileUtil = mockFileUtil
},
},
{
name: "negative testing (c.Envconfig.Process(\"\", &config) failed)",
fields: fields{
BaseConfigurator: &baseConfig.BaseConfigurator{
Envconfig: nil,
FileUtil: nil,
}},
want: nil,
wantErr: true,
setup: func(mockCtrl *gomock.Controller, tt *fields) {
mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl)
mockEnvconfig.EXPECT().Process("", gomock.Any()).Return(errors.New("Envconfig.Process() failed"))
tt.BaseConfigurator.Envconfig = mockEnvconfig
},
},
{
name: "negative testing (c.FileUtil.GetXDGDataHome() failed)",
fields: fields{
BaseConfigurator: &baseConfig.BaseConfigurator{
Envconfig: nil,
FileUtil: nil,
}},
want: nil,
wantErr: true,
setup: func(mockCtrl *gomock.Controller, tt *fields) {
mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl)
mockEnvconfig.EXPECT().Process("", gomock.Any()).DoAndReturn(
func(_ string, cfg *envConfig) error {
cfg.WnJpnDBType = database.SQLite
cfg.WnJpnDBDsn = "XDG_DATA_HOME/jrp/wnjpn.db"
return nil
})
mockFileUtil := utility.NewMockFileUtil(mockCtrl)
mockFileUtil.EXPECT().GetXDGDataHome().Return("", errors.New("FileUtil.GetXDGDataHome() failed"))
tt.BaseConfigurator.Envconfig = mockEnvconfig
tt.BaseConfigurator.FileUtil = mockFileUtil
},
},
{
name: "negative testing (c.FileUtil.MkdirIfNotExist(filepath.Dir(config.WNJpnDBConnectionString)) failed)",
fields: fields{
BaseConfigurator: &baseConfig.BaseConfigurator{
Envconfig: nil,
FileUtil: nil,
},
},
want: nil,
wantErr: true,
setup: func(mockCtrl *gomock.Controller, tt *fields) {
mockEnvconfig := proxy.NewMockEnvconfig(mockCtrl)
mockEnvconfig.EXPECT().Process("", gomock.Any()).DoAndReturn(
func(_ string, cfg *envConfig) error {
cfg.WnJpnDBType = database.SQLite
cfg.WnJpnDBDsn = "XDG_DATA_HOME/jrp/wnjpn.db"
return nil
})
mockFileUtil := utility.NewMockFileUtil(mockCtrl)
mockFileUtil.EXPECT().GetXDGDataHome().Return("~/.local/share", nil)
mockFileUtil.EXPECT().MkdirIfNotExist("~/.local/share/jrp").Return(errors.New("FileUtil.MkdirIfNotExist() failed"))
tt.BaseConfigurator.Envconfig = mockEnvconfig
tt.BaseConfigurator.FileUtil = mockFileUtil
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
if tt.setup != nil {
tt.setup(mockCtrl, &tt.fields)
}
c := &ServerConfigurator{
BaseConfigurator: tt.fields.BaseConfigurator,
}
got, err := c.GetConfig()
if (err != nil) != tt.wantErr {
t.Errorf("ServerConfigurator.GetConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("ServerConfigurator.GetConfig() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions app/presentation/api/jrp/config/godoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package config provides the configuration of the jrp server application.
package config
24 changes: 24 additions & 0 deletions app/presentation/api/jrp/formatter/formatter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package formatter

import (
"errors"
)

// Formatter is an interface that formats the output of jrp cli.
type Formatter interface {
Format(result interface{}) ([]byte, error)
}

// NewFormatter returns a new instance of the Formatter interface.
func NewFormatter(
format string,
) (Formatter, error) {
var f Formatter
switch format {
case "json":
f = NewJsonFormatter()
default:
return nil, errors.New("invalid format")
}
return f, nil
}
48 changes: 48 additions & 0 deletions app/presentation/api/jrp/formatter/formatter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package formatter

import (
"reflect"
"testing"
)

func TestNewFormatter(t *testing.T) {
type args struct {
format string
}
tests := []struct {
name string
args args
want Formatter
wantErr bool
}{
{
name: "positive testing (format is json)",
args: args{
format: "json",
},
want: &JsonFormatter{},
wantErr: false,
},

{
name: "negative testing (format is invalid)",
args: args{
format: "invalid",
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewFormatter(tt.args.format)
if (err != nil) != tt.wantErr {
t.Errorf("NewFormatter() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewFormatter() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions app/presentation/api/jrp/formatter/godoc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package formatter provides the formatter for the jrp.
package formatter
Loading
Loading