Move paste handler into its own package

master
Nick Krichevsky 2019-03-10 00:37:28 -05:00
parent ef4699ed8b
commit 5d61fad70d
10 changed files with 54 additions and 54 deletions

View File

@ -1,4 +1,4 @@
package handler
package paste
import (
"io"

View File

@ -1,4 +1,4 @@
package handler
package paste
import (
"testing"

View File

@ -1,22 +1,22 @@
package handler
package paste
import (
"github.com/ollien/updown/repository"
)
// PasteService is a service to produce pastes
type PasteService struct {
// Service is a service to produce pastes
type Service struct {
db repository.PasteRepository
pasteDir string
}
// NewPasteService makes a new PasteService
func NewPasteService() *PasteService {
return initPasteService()
// NewService makes a new Service
func NewService() *Service {
return initService()
}
// CreatePaste makes a new paste to be stored
func (service PasteService) CreatePaste(title string) (Paster, error) {
func (service Service) CreatePaste(title string) (Paster, error) {
paste, err := service.db.PutPaste(title)
if err != nil {
return Paster{}, err

View File

@ -1,4 +1,4 @@
package handler
package paste
import (
"testing"
@ -31,16 +31,16 @@ func (repo *MockPasteRepository) UpdatePaste(paste repository.Paste) error {
return args.Error(0)
}
func setupPasteService() *PasteService {
func setupService() *Service {
mockRepo := &MockPasteRepository{}
return &PasteService{
return &Service{
db: mockRepo,
pasteDir: "/tmp/pastes/",
}
}
func TestCreatePaste(t *testing.T) {
service := setupPasteService()
service := setupService()
mockRepo := service.db.(*MockPasteRepository)
testPaste := repository.Paste{
ID: 23,

View File

@ -3,7 +3,7 @@
//go:generate wire
//+build !wireinject
package handler
package paste
import (
"github.com/ollien/updown/config"
@ -12,18 +12,18 @@ import (
// Injectors from wire_stubs.go:
func initPasteService() *PasteService {
func initService() *Service {
configConfig := config.Get()
databaseConnector := repository.GetConnector()
pasteRepository := providePasteRepository(databaseConnector)
pasteService := providePasteService(configConfig, pasteRepository)
pasteService := provideService(configConfig, pasteRepository)
return pasteService
}
// wire_stubs.go:
func providePasteService(appConfig config.Config, repo repository.PasteRepository) *PasteService {
return &PasteService{
func provideService(appConfig config.Config, repo repository.PasteRepository) *Service {
return &Service{
db: repo,
pasteDir: appConfig.StoragePath,
}

View File

@ -0,0 +1,27 @@
//+build wireinject
package paste
import (
"github.com/google/wire"
"github.com/ollien/updown/config"
"github.com/ollien/updown/repository"
)
// initService is generated by wire. Please see wire_gen.go for the generated implementation
func initService() *Service {
wire.Build(provideService, providePasteRepository, repository.GetConnector, config.Get)
return nil
}
func provideService(appConfig config.Config, repo repository.PasteRepository) *Service {
return &Service{
db: repo,
pasteDir: appConfig.StoragePath,
}
}
func providePasteRepository(db *repository.DatabaseConnector) repository.PasteRepository {
return repository.PasteRepository(db)
}

View File

@ -1,27 +0,0 @@
//+build wireinject
package handler
import (
"github.com/google/wire"
"github.com/ollien/updown/config"
"github.com/ollien/updown/repository"
)
// initPasteService is generated by wire. Please see wire_gen.go for the generated implementation
func initPasteService() *PasteService {
wire.Build(providePasteService, providePasteRepository, repository.GetConnector, config.Get)
return nil
}
func providePasteService(appConfig config.Config, repo repository.PasteRepository) *PasteService {
return &PasteService{
db: repo,
pasteDir: appConfig.StoragePath,
}
}
func providePasteRepository(db *repository.DatabaseConnector) repository.PasteRepository {
return repository.PasteRepository(db)
}

View File

@ -4,14 +4,14 @@ import (
"net/http"
"github.com/go-chi/chi"
"github.com/ollien/updown/handler"
"github.com/ollien/updown/handler/paste"
)
// Webserver is the main websever
type Webserver struct {
httpServer *http.Server
router chi.Router
pasteService *handler.PasteService
pasteService *paste.Service
}
// NewWebserver creates a new webserver and sets up all routes

View File

@ -8,7 +8,7 @@ package web
import (
"github.com/go-chi/chi"
"github.com/ollien/updown/config"
"github.com/ollien/updown/handler"
"github.com/ollien/updown/handler/paste"
"net/http"
"strconv"
)
@ -19,8 +19,8 @@ func initWebserver() *Webserver {
configConfig := config.Get()
router := provideRouter()
server := provideHTTPServer(configConfig, router)
pasteService := handler.NewPasteService()
webserver := provideWebserver(server, router, pasteService)
service := paste.NewService()
webserver := provideWebserver(server, router, service)
return webserver
}
@ -33,7 +33,7 @@ func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server
}
}
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *handler.PasteService) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,

View File

@ -9,12 +9,12 @@ import (
"github.com/go-chi/chi"
"github.com/google/wire"
"github.com/ollien/updown/config"
"github.com/ollien/updown/handler"
"github.com/ollien/updown/handler/paste"
)
// initWebserver is generated by wire. Please see wire_gen.go for the generated implementation
func initWebserver() *Webserver {
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, handler.NewPasteService)
wire.Build(provideWebserver, provideHTTPServer, provideRouter, config.Get, paste.NewService)
return &Webserver{}
}
@ -25,7 +25,7 @@ func provideHTTPServer(appConfig config.Config, router chi.Router) *http.Server
}
}
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *handler.PasteService) *Webserver {
func provideWebserver(httpServer *http.Server, router chi.Router, pasteService *paste.Service) *Webserver {
webserver := Webserver{
httpServer: httpServer,
router: router,