mirror of
				https://github.com/SourceFellows/gobuch.git
				synced 2025-11-04 07:36:19 +01:00 
			
		
		
		
	initial import
This commit is contained in:
		@@ -0,0 +1,17 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
 | 
			
		||||
	"golang.source-fellows.com/samples/applicationx/http/rest"
 | 
			
		||||
	"golang.source-fellows.com/samples/applicationx/postgres"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
	us := &postgres.UserService{}
 | 
			
		||||
 | 
			
		||||
	http.HandleFunc("/", rest.Handler(us))
 | 
			
		||||
	http.ListenAndServe(":8080", nil)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,8 @@
 | 
			
		||||
module golang.source-fellows.com/samples/applicationx
 | 
			
		||||
 | 
			
		||||
go 1.14
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/golang/mock v1.4.4
 | 
			
		||||
	rsc.io/quote/v3 v3.1.0 // indirect
 | 
			
		||||
)
 | 
			
		||||
							
								
								
									
										14
									
								
								best-practices/project-structure/dependency-sample/go.sum
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								best-practices/project-structure/dependency-sample/go.sum
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
github.com/golang/mock v1.4.3 h1:GV+pQPG/EUUbkh47niozDcADz6go/dUwhVzdUQHIVRw=
 | 
			
		||||
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
 | 
			
		||||
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
 | 
			
		||||
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
 | 
			
		||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
 | 
			
		||||
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
 | 
			
		||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
 | 
			
		||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
 | 
			
		||||
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
			
		||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 | 
			
		||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262 h1:qsl9y/CJx34tuA7QCPNp86JNJe4spst6Ff8MjvPUdPg=
 | 
			
		||||
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
 | 
			
		||||
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
 | 
			
		||||
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
 | 
			
		||||
@@ -0,0 +1,16 @@
 | 
			
		||||
package rest
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"golang.source-fellows.com/samples/applicationx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Handler(us applicationx.UserService) http.HandlerFunc {
 | 
			
		||||
	return func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
		us.CreateUser(&applicationx.User{})
 | 
			
		||||
		fmt.Fprintf(w, "Hello World! %s", time.Now())
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,29 @@
 | 
			
		||||
package rest
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"net/http/httptest"
 | 
			
		||||
	"testing"
 | 
			
		||||
 | 
			
		||||
	"github.com/golang/mock/gomock"
 | 
			
		||||
	"golang.source-fellows.com/samples/applicationx/mocks"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func TestHandler(t *testing.T) {
 | 
			
		||||
 | 
			
		||||
	ctrl := gomock.NewController(t)
 | 
			
		||||
	defer ctrl.Finish()
 | 
			
		||||
 | 
			
		||||
	userService := mocks.NewMockUserService(ctrl)
 | 
			
		||||
 | 
			
		||||
	handlerFunc := Handler(userService)
 | 
			
		||||
 | 
			
		||||
	w := httptest.NewRecorder()
 | 
			
		||||
	r, _ := http.NewRequest("GET", "/egal", nil)
 | 
			
		||||
 | 
			
		||||
	//wird der Service überhaupt aufgerufen?
 | 
			
		||||
	userService.EXPECT().CreateUser(gomock.Any()).MinTimes(1)
 | 
			
		||||
 | 
			
		||||
	handlerFunc(w, r)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,77 @@
 | 
			
		||||
// Code generated by MockGen. DO NOT EDIT.
 | 
			
		||||
// Source: user.go
 | 
			
		||||
 | 
			
		||||
// Package mocks is a generated GoMock package.
 | 
			
		||||
package mocks
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	gomock "github.com/golang/mock/gomock"
 | 
			
		||||
	applicationx "golang.source-fellows.com/samples/applicationx"
 | 
			
		||||
	reflect "reflect"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// MockUserService is a mock of UserService interface
 | 
			
		||||
type MockUserService struct {
 | 
			
		||||
	ctrl     *gomock.Controller
 | 
			
		||||
	recorder *MockUserServiceMockRecorder
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MockUserServiceMockRecorder is the mock recorder for MockUserService
 | 
			
		||||
type MockUserServiceMockRecorder struct {
 | 
			
		||||
	mock *MockUserService
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// NewMockUserService creates a new mock instance
 | 
			
		||||
func NewMockUserService(ctrl *gomock.Controller) *MockUserService {
 | 
			
		||||
	mock := &MockUserService{ctrl: ctrl}
 | 
			
		||||
	mock.recorder = &MockUserServiceMockRecorder{mock}
 | 
			
		||||
	return mock
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// EXPECT returns an object that allows the caller to indicate expected use
 | 
			
		||||
func (m *MockUserService) EXPECT() *MockUserServiceMockRecorder {
 | 
			
		||||
	return m.recorder
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateUser mocks base method
 | 
			
		||||
func (m *MockUserService) CreateUser(u *applicationx.User) error {
 | 
			
		||||
	m.ctrl.T.Helper()
 | 
			
		||||
	ret := m.ctrl.Call(m, "CreateUser", u)
 | 
			
		||||
	ret0, _ := ret[0].(error)
 | 
			
		||||
	return ret0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// CreateUser indicates an expected call of CreateUser
 | 
			
		||||
func (mr *MockUserServiceMockRecorder) CreateUser(u interface{}) *gomock.Call {
 | 
			
		||||
	mr.mock.ctrl.T.Helper()
 | 
			
		||||
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CreateUser", reflect.TypeOf((*MockUserService)(nil).CreateUser), u)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadUser mocks base method
 | 
			
		||||
func (m *MockUserService) ReadUser(id int) (*applicationx.User, error) {
 | 
			
		||||
	m.ctrl.T.Helper()
 | 
			
		||||
	ret := m.ctrl.Call(m, "ReadUser", id)
 | 
			
		||||
	ret0, _ := ret[0].(*applicationx.User)
 | 
			
		||||
	ret1, _ := ret[1].(error)
 | 
			
		||||
	return ret0, ret1
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// ReadUser indicates an expected call of ReadUser
 | 
			
		||||
func (mr *MockUserServiceMockRecorder) ReadUser(id interface{}) *gomock.Call {
 | 
			
		||||
	mr.mock.ctrl.T.Helper()
 | 
			
		||||
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ReadUser", reflect.TypeOf((*MockUserService)(nil).ReadUser), id)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteUser mocks base method
 | 
			
		||||
func (m *MockUserService) DeleteUser(id int) error {
 | 
			
		||||
	m.ctrl.T.Helper()
 | 
			
		||||
	ret := m.ctrl.Call(m, "DeleteUser", id)
 | 
			
		||||
	ret0, _ := ret[0].(error)
 | 
			
		||||
	return ret0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// DeleteUser indicates an expected call of DeleteUser
 | 
			
		||||
func (mr *MockUserServiceMockRecorder) DeleteUser(id interface{}) *gomock.Call {
 | 
			
		||||
	mr.mock.ctrl.T.Helper()
 | 
			
		||||
	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "DeleteUser", reflect.TypeOf((*MockUserService)(nil).DeleteUser), id)
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,24 @@
 | 
			
		||||
package postgres
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
 | 
			
		||||
	"golang.source-fellows.com/samples/applicationx"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var _ applicationx.UserService = &UserService{}
 | 
			
		||||
 | 
			
		||||
type UserService struct{}
 | 
			
		||||
 | 
			
		||||
func (us *UserService) CreateUser(user *applicationx.User) error {
 | 
			
		||||
	fmt.Println("Create User in Postgres Service")
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (us *UserService) DeleteUser(id int) error {
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (us *UserService) ReadUser(id int) (*applicationx.User, error) {
 | 
			
		||||
	return nil, nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										14
									
								
								best-practices/project-structure/dependency-sample/user.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								best-practices/project-structure/dependency-sample/user.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,14 @@
 | 
			
		||||
//go:generate mockgen -source=user.go -package mocks -destination mocks/user_service.go
 | 
			
		||||
 | 
			
		||||
package applicationx
 | 
			
		||||
 | 
			
		||||
type User struct {
 | 
			
		||||
	ID   int
 | 
			
		||||
	name string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type UserService interface {
 | 
			
		||||
	CreateUser(u *User) error
 | 
			
		||||
	ReadUser(id int) (*User, error)
 | 
			
		||||
	DeleteUser(id int) error
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user