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

refactor: Refactor to pkg and iternal structure #28

Merged
merged 7 commits into from
Sep 28, 2024
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
11 changes: 0 additions & 11 deletions api/v1/error.go

This file was deleted.

40 changes: 0 additions & 40 deletions api/v1/item.go

This file was deleted.

121 changes: 0 additions & 121 deletions api/v1/item_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions cmd/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import (
"os"
"path"

"github.com/glass-cms/glasscms/item"
"github.com/glass-cms/glasscms/parser"
"github.com/glass-cms/glasscms/sourcer"
"github.com/glass-cms/glasscms/internal/item"
"github.com/glass-cms/glasscms/internal/parser"
"github.com/glass-cms/glasscms/internal/sourcer"
"github.com/lmittmann/tint"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion cmd/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log/slog"
"os"

"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/internal/database"
"github.com/lmittmann/tint"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down
2 changes: 1 addition & 1 deletion cmd/server/server.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package server

import (
"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/internal/database"
"github.com/spf13/cobra"
)

Expand Down
13 changes: 5 additions & 8 deletions cmd/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import (
"os"
"os/user"

"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/item"
ctx "github.com/glass-cms/glasscms/lib/context"
"github.com/glass-cms/glasscms/server"
"github.com/glass-cms/glasscms/server/handler"
v1 "github.com/glass-cms/glasscms/server/handler/v1"
"github.com/glass-cms/glasscms/internal/database"
"github.com/glass-cms/glasscms/internal/item"
"github.com/glass-cms/glasscms/internal/server"
ctx "github.com/glass-cms/glasscms/pkg/context"
"github.com/lmittmann/tint"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -95,9 +93,8 @@ func (c *StartCommand) Execute(cmd *cobra.Command, _ []string) error {

repo := item.NewRepository(db, errHandler)
service := item.NewService(repo)
v1Handler := v1.NewAPIHandler(c.logger, service)

server, err := server.New(c.logger, []handler.VersionedHandler{v1Handler})
server, err := server.New(c.logger, service)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package main

//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=openapi/codegen.cfg.v1.yaml openapi/openapi.v1.yaml
//go:generate go run github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen --config=oapi-codegen-config.yaml openapi.yaml
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion database/error_test.go → internal/database/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"testing"

"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/internal/database"
"github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
)
Expand Down
File renamed without changes.
13 changes: 6 additions & 7 deletions lib/test/database.go → internal/database/test.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
package test
package database

import (
"database/sql"
"fmt"

"github.com/glass-cms/glasscms/database"
"github.com/google/uuid"
)

// NewDB creates a new in-memory SQLite database with the necessary schema
// for testing purposes.
func NewDB() (*sql.DB, error) {
config := database.Config{
Driver: database.DriverName[int32(database.DriverSqlite)],
func NewTestDB() (*sql.DB, error) {
config := Config{
Driver: DriverName[int32(DriverSqlite)],
DSN: fmt.Sprintf("file:%s?mode=memory&cache=shared", uuid.New().String()),
}

db, err := database.NewConnection(config)
db, err := NewConnection(config)
if err != nil {
return nil, err
}

if err = database.MigrateDatabase(db, config); err != nil {
if err = MigrateDatabase(db, config); err != nil {
return nil, err
}

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion item/repository.go → internal/item/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"fmt"

"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/internal/database"
)

type Repository interface {
Expand Down
4 changes: 2 additions & 2 deletions item/repository_test.go → internal/item/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"
"time"

"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/item"
"github.com/glass-cms/glasscms/internal/database"
"github.com/glass-cms/glasscms/internal/item"
_ "github.com/mattn/go-sqlite3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down
4 changes: 2 additions & 2 deletions item/service.go → internal/item/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"context"
"errors"

"github.com/glass-cms/glasscms/database"
"github.com/glass-cms/glasscms/lib/resource"
"github.com/glass-cms/glasscms/internal/database"
"github.com/glass-cms/glasscms/pkg/resource"
)

// Service is a service for managing items.
Expand Down
4 changes: 2 additions & 2 deletions parser/parser.go → internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"path/filepath"
"strings"

"github.com/glass-cms/glasscms/item"
"github.com/glass-cms/glasscms/sourcer"
"github.com/glass-cms/glasscms/internal/item"
"github.com/glass-cms/glasscms/internal/sourcer"
"github.com/mozillazg/go-slugify"
"gopkg.in/yaml.v3"
)
Expand Down
2 changes: 1 addition & 1 deletion parser/parser_test.go → internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"time"

"github.com/glass-cms/glasscms/parser"
"github.com/glass-cms/glasscms/internal/parser"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package v1
package server

import (
"net/http"
"reflect"

v1 "github.com/glass-cms/glasscms/api/v1"
"github.com/glass-cms/glasscms/server/handler"
"github.com/glass-cms/glasscms/pkg/api"
)

// ErrorMapper is a function that maps an error to an API error response.
type ErrorMapper func(error) *v1.Error
type ErrorMapper func(error) *api.Error

type ErrorHandler struct {
Mappers map[reflect.Type]ErrorMapper
Expand Down Expand Up @@ -37,20 +36,20 @@ func (h *ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err e
if mapper, exists := h.Mappers[errType]; exists {
errResp := mapper(err)

statusCode, ok := v1.ErrorCodeMapping[errResp.Code]
statusCode, ok := ErrorCodeMapping[errResp.Code]
if !ok {
statusCode = http.StatusInternalServerError
}

handler.SerializeResponse(w, r, statusCode, errResp)
SerializeResponse(w, r, statusCode, errResp)
return
}

// Fallback on generic error response if we don't have a specific error mapper.
errResp := &v1.Error{
Code: v1.ProcessingError,
errResp := &api.Error{
Code: api.ProcessingError,
Message: "An error occurred while processing the request.",
Type: v1.ApiError,
Type: api.ApiError,
}
handler.SerializeResponse(w, r, http.StatusInternalServerError, errResp)
SerializeResponse(w, r, http.StatusInternalServerError, errResp)
}
Loading
Loading