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

Echo server #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
26 changes: 0 additions & 26 deletions .env

This file was deleted.

27 changes: 20 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
BASE_URL=127.0.0.1
API_PORT=443
API_PORT=8000

PROJECT_NAME=gotham
PROJECT_HOST=localhost
PROJECT_URL=https://www.example.com
PROJECT_API_URL=https://api.example.com

#DB
DB_CONNECTION=mysql
DB_DATABASE=example
DB_CONNECTION=postgres
DB_USERNAME=gotham
DB_DATABASE=gotham
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=admin
DB_PASSWORD=admin
DB_PORT=35432
DB_PASSWORD=password

PGUSER=admin
PGPASSWORD=password
PGDATABASE=gotham
PGHOST=localhost
PGPORT=35432

#EMAIL
[email protected]
HOST=smtp.gmail.com
PORT=587
PASSWORD=password

#VERSION
VERSION=0.1

#JWT_SECRET_KEY
JWT_SECRET_KEY=7l6dds5z2egrfcw01s6e78arte48067
JWT_SECRET_KEY=jwt_secret_key
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
# vendor/


*.idea
*.idea

.env
27 changes: 27 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# vim: set noet ci pi sts=0 sw=4 ts=4 :
# http://www.gnu.org/software/make/manual/make.html
# http://linuxlib.ru/prog/make_379_manual.html
SHELL := $(shell which bash)
#SHELL := $(shell which sh)
DEBUG ?= 0

########################################################################
# Default variables
########################################################################
-include .env
export
########################################################################
GOBIN := $(or $(GOBIN), $(GOPATH)/bin)
SUDO := $(or $(SUDO),)
GO111MODULE := $(or $(GO111MODULE), on)
GOROOT := $(GOPATH/src)
TAGS :=
LDFLAGS := -w -s
GOFLAGS :=

.PHONY: db-init
db-init:
set -o allexport;
source .env;
set +o allexport;
./infra/db/$(DB_CONNECTION)_init.sh
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,21 @@ DB_PORT=3306
DB_PASSWORD=strong_password
```

## Infra

```
cp .env.example .env

docker-compose up -d
# wait for pg container is up
# login from pgadmin and check database status

make db-init

go install github.com/swaggo/swag/cmd/[email protected]
swag init -g main.go
```

## Flags

- prevents re-creating container methods from definitions
Expand All @@ -55,6 +70,11 @@ go run gotham -migrate
go run gotham -seed
```

- run echo server
```
go run gotham -server
```

## FOLDER STRUCTURE

```
Expand Down Expand Up @@ -115,6 +135,20 @@ go run gotham -seed

### ViewModels
ViewModels folder hosts all the structs under viewmodels namespace, viewmodels are model to be use as a response return of REST API call

## Test

```
set -o allexport;
source .env;
set +o allexport;
export ADMIN_EMAIL=$(echo "SELECT json_agg(users) FROM users WHERE id=1" |psql -AXqt | jq -r '.[].email')
export ADMIN_TOKEN=$(curl -s -X POST "http://${BASE_URL}:${API_PORT}/v1/login" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"email\": \"${ADMIN_EMAIL}\", \"password\": \"password\", \"platform\": \"web\"}" | jq -r .data.access_token)

curl -X GET "http://${BASE_URL}:${API_PORT}/v1/r/users" -H "accept: application/json" -H "Authorization: Bearer ${ADMIN_TOKEN}"
curl -X GET "http://${BASE_URL}:${API_PORT}/v1/r/users/2" -H "accept: application/json" -H "Authorization: Bearer ${ADMIN_TOKEN}"
```


## Author

Expand All @@ -139,4 +173,4 @@ Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
65 changes: 62 additions & 3 deletions app/container/dic/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@ import (
middlewares "gotham/middlewares"
policies "gotham/policies"
repositories "gotham/repositories"
server "gotham/server"
services "gotham/services"
)

// C retrieves a Container from an interface.
// The function panics if the Container can not be retrieved.
//
// The interface can be :
// - a *Container
// - an *http.Request containing a *Container in its context.Context
// for the dingo.ContainerKey("dingo") key.
// - a *Container
// - an *http.Request containing a *Container in its context.Context
// for the dingo.ContainerKey("dingo") key.
//
// The function can be changed to match the needs of your application.
var C = func(i interface{}) *Container {
Expand Down Expand Up @@ -511,6 +512,64 @@ func DbPool(i interface{}) infrastructures.IGormDatabasePool {
return C(i).GetDbPool()
}

// SafeGetEchoServer works like SafeGet but only for EchoServer.
// It does not return an interface but a *server.EchoServer.
func (c *Container) SafeGetEchoServer() (*server.EchoServer, error) {
i, err := c.ctn.SafeGet("echo-server")
if err != nil {
var eo *server.EchoServer
return eo, err
}
o, ok := i.(*server.EchoServer)
if !ok {
return o, errors.New("could get 'echo-server' because the object could not be cast to *server.EchoServer")
}
return o, nil
}

// GetEchoServer is similar to SafeGetEchoServer but it does not return the error.
// Instead it panics.
func (c *Container) GetEchoServer() *server.EchoServer {
o, err := c.SafeGetEchoServer()
if err != nil {
panic(err)
}
return o
}

// UnscopedSafeGetEchoServer works like UnscopedSafeGet but only for EchoServer.
// It does not return an interface but a *server.EchoServer.
func (c *Container) UnscopedSafeGetEchoServer() (*server.EchoServer, error) {
i, err := c.ctn.UnscopedSafeGet("echo-server")
if err != nil {
var eo *server.EchoServer
return eo, err
}
o, ok := i.(*server.EchoServer)
if !ok {
return o, errors.New("could get 'echo-server' because the object could not be cast to *server.EchoServer")
}
return o, nil
}

// UnscopedGetEchoServer is similar to UnscopedSafeGetEchoServer but it does not return the error.
// Instead it panics.
func (c *Container) UnscopedGetEchoServer() *server.EchoServer {
o, err := c.UnscopedSafeGetEchoServer()
if err != nil {
panic(err)
}
return o
}

// EchoServer is similar to GetEchoServer.
// It tries to find the container with the C method and the given interface.
// If the container can be retrieved, it applies the GetEchoServer method.
// If the container can not be retrieved, it panics.
func EchoServer(i interface{}) *server.EchoServer {
return C(i).GetEchoServer()
}

// SafeGetEmail works like SafeGet but only for Email.
// It does not return an interface but a infrastructures.IEmailService.
func (c *Container) SafeGetEmail() (infrastructures.IEmailService, error) {
Expand Down
21 changes: 21 additions & 0 deletions app/container/dic/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
middlewares "gotham/middlewares"
policies "gotham/policies"
repositories "gotham/repositories"
server "gotham/server"
services "gotham/services"
)

Expand Down Expand Up @@ -169,6 +170,26 @@ func getDiDefs(provider dingo.Provider) []di.Def {
return nil
},
},
{
Name: "echo-server",
Scope: "app",
Build: func(ctn di.Container) (interface{}, error) {
d, err := provider.Get("echo-server")
if err != nil {
var eo *server.EchoServer
return eo, err
}
b, ok := d.Build.(func() (*server.EchoServer, error))
if !ok {
var eo *server.EchoServer
return eo, errors.New("could not cast build function to func() (*server.EchoServer, error)")
}
return b()
},
Close: func(obj interface{}) error {
return nil
},
},
{
Name: "email",
Scope: "app",
Expand Down
18 changes: 18 additions & 0 deletions app/defs/servers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package defs

import (
"gotham/server"

"github.com/sarulabs/di/v2"
"github.com/sarulabs/dingo/v4"
)

var ServersDefs = []dingo.Def{
{
Name: "echo-server",
Scope: di.App,
Build: func() (ES *server.EchoServer, err error) {
return &server.EchoServer{}, nil
},
},
}
4 changes: 4 additions & 0 deletions app/flags/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ import (

var (
Production *bool
Dingo *bool
Migrate *bool
Seed *bool
Server *bool
)

func init() {
Production = flag.Bool("production", false, "a bool")
Dingo = flag.Bool("dingo", false, "a bool")
Migrate = flag.Bool("migrate", false, "a bool")
Seed = flag.Bool("seed", false, "a bool")
Server = flag.Bool("server", false, "a bool")
flag.Parse()
}
7 changes: 6 additions & 1 deletion app/provider/appServiceProvider.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package provider

import (
"github.com/sarulabs/dingo/v4"
"gotham/app/defs"

"github.com/sarulabs/dingo/v4"
)

type Provider struct {
Expand Down Expand Up @@ -42,5 +43,9 @@ func (p *Provider) Load() error {
return err
}

if err := p.AddDefSlice(defs.ServersDefs); err != nil {
return err
}

return nil
}
9 changes: 8 additions & 1 deletion config/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type Config struct {
Brand struct {
ProjectName string
ProjectUrl string
ProjectHost string
ProjectApiUrl string
}
}
Expand All @@ -47,7 +48,13 @@ func Configurations() {
Brand: struct {
ProjectName string
ProjectUrl string
ProjectHost string
ProjectApiUrl string
}{ProjectName: os.Getenv("PROJECT_NAME"), ProjectUrl: os.Getenv("PROJECT_URL"), ProjectApiUrl: os.Getenv("PROJECT_API_URL")},
}{
ProjectName: os.Getenv("PROJECT_NAME"),
ProjectUrl: os.Getenv("PROJECT_URL"),
ProjectHost: os.Getenv("PROJECT_HOST"),
ProjectApiUrl: os.Getenv("PROJECT_API_URL"),
},
}
}
7 changes: 4 additions & 3 deletions controllers/userController.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type UserController struct {
// @Accept multipart/form-data
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param token header string true "Bearer Token"
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} viewModels.Paginator{data=[]models.User}
// @Failure 400 {object} viewModels.Message{}
// @Failure 401 {object} viewModels.Message{}
Expand Down Expand Up @@ -70,14 +70,15 @@ func (u UserController) Index(c echo.Context) (err error) {
// @Accept multipart/form-data
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param token header string true "Bearer Token"
// @Param user path string true "1"
// @Param Authorization header string true "Bearer Token"
// @Success 200 {object} viewModels.HTTPSuccessResponse{data=models.User}
// @Failure 404 {object} viewModels.Message{}
// @Failure 401 {object} viewModels.Message{}
// @Failure 400 {object} viewModels.Message{}
// @Failure 403 {object} viewModels.Message{}
// @Failure 500 {object} viewModels.Message{}
// @Router /v1/r/users/:user [get]
// @Router /v1/r/users/{user} [get]
func (u UserController) Show(c echo.Context) (err error) {
auth := models.ConvertUser(c.Get("auth"))

Expand Down
Loading