Skip to content
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
3 changes: 0 additions & 3 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,4 @@ GORM_SLOW_QUERY_LOGGING_LIMIT=300
PGX_DSN="host=localhost port=5432 user=postgres dbname=test password=foobar prefer_simple_protocol=1 sslmode=disable pool_max_conns=10 pool_min_conns=2 pool_max_conn_lifetime=1h pool_max_conn_idle_time=10m"
PGX_SLOW_QUERY_LOGGING_LIMIT=300

GQL_COMPLEXITY_LIMIT=200
GQL_API_URL=/graphql
GQL_PLAYGROUND_URL=/playground
CACHE_ENABLED=false
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@
# Dependency directories (remove the comment below to include it)
# vendor/
.idea
.env
.env

bin
tmp

node_modules/
15 changes: 15 additions & 0 deletions .graphqlconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "Untitled GraphQL Schema",
"schemaPath": "schema.graphql",
"extensions": {
"endpoints": {
"Default GraphQL Endpoint": {
"url": "http://localhost:8888/graphql",
"headers": {
"user-agent": "JS GraphQL"
},
"introspect": true
}
}
}
}
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.19-alpine as tools

RUN apk add --no-cache build-base

RUN go install github.com/cespare/reflex@v0.3
RUN go install -tags 'nowasm' github.com/kyleconroy/sqlc/cmd/sqlc@v1.16
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@v4.15

FROM golang:1.19-alpine

WORKDIR /go/src/app

RUN apk add --no-cache make build-base
COPY --from=tools /go/bin/reflex /usr/bin/reflex
COPY --from=tools /go/bin/sqlc /usr/bin/sqlc
COPY --from=tools /go/bin/migrate /usr/bin/migrate

CMD ["make", "start"]
12 changes: 11 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@
RUN_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS))
$(eval $(RUN_ARGS):;@:)

start=reflex -r '(\.go$|go\.mod)' -R .idea/ -s -d none $(2) -- sh -c 'make build && $(or $(value 1), /usr/bin/demo serve)'

####################################################################################################
## MAIN COMMANDS
####################################################################################################
help: ## Commands list
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-16s\033[0m %s\n", $$1, $$2}'

generate: ## Generate public graphql schema
build:
go build -buildvcs=false -o /usr/bin/demo cmd/main.go

start:
$(call start)

graphql-generate: ## Generate public graphql schema
go run github.com/99designs/gqlgen generate --config gqlgen.yml

m:
migrate -database postgres://modulus:secret@postgres:5432/demo?sslmode=disable -path internal/messenger/persistence/migration $(RUN_ARGS)

install: ## Make a binary to ./bin folder
go build -o ./bin/server -i /cmd/server/main.go
Expand Down
144 changes: 144 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
package main

import (
"demo/graph"
"demo/internal/auth"
"demo/internal/chi"
"demo/internal/cli"
"demo/internal/errors"
"demo/internal/framework"
"demo/internal/graphql"
"demo/internal/http"
"demo/internal/http/middleware"
"demo/internal/logger"
"demo/internal/messenger"
"demo/internal/pgx"
"demo/internal/temporal"
"github.com/ggicci/httpin"
oChi "github.com/go-chi/chi/v5"
chiMiddleware "github.com/go-chi/chi/v5/middleware"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/go-ozzo/ozzo-validation/v4/is"
"github.com/gofrs/uuid"
"github.com/spf13/cobra"
"go.uber.org/fx"
"go.uber.org/fx/fxevent"
"go.uber.org/zap"
oHttp "net/http"
"reflect"
"strings"
"time"
)

func main() {
chiCfg := chi.ModuleParams{
Configure: func(
router oChi.Router,
errorHandler *http.ErrorHandler,
requestIdMiddleware *middleware.RequestIdMiddleware,
correlationIdMiddleware *middleware.CorrelationIdMiddleware,
authMiddleware *auth.Middleware,
) {
m := http.Chain(requestIdMiddleware, correlationIdMiddleware)
router.Use(func(handler oHttp.Handler) oHttp.Handler {
return errorHandler.Wrap(m.Next(http.FromHttpHandler(handler)))
})
router.Use(chiMiddleware.SetHeader("Content-Type", "application/json"))
router.Use(chiMiddleware.CleanPath)
router.Use(chiMiddleware.RealIP)
router.Use(chiMiddleware.Timeout(5 * time.Second))
router.Use(chiMiddleware.NewCompressor(4, "application/json").Handler)
router.Use(func(handler oHttp.Handler) oHttp.Handler {
return errorHandler.Wrap(authMiddleware.Next(http.FromHttpHandler(handler)))
})
router.NotFound(func(w oHttp.ResponseWriter, req *oHttp.Request) {
errorHandler.Handle(w, req, errors.NewNotFoundError("http.notFound", "Not Found"))
})
},
}

app := fx.New(
framework.ConfigModule(),
errors.Module(),
logger.NewModule(),
cli.Module(),
chi.Module(chiCfg),
pgx.Module(pgx.ModuleConfig{}),
graph.Module(),
graphql.Module(),
temporal.Module(),
auth.Module(),
messenger.Module(),
fx.Provide(
middleware.NewRequestIdMiddleware,
middleware.NewCorrelationIdMiddleware,
cli.ProvideRoot(
func(shutdowner fx.Shutdowner) *cobra.Command {
root := &cobra.Command{
Use: "modulus",
Short: "Modulus is modulus",
Long: `Modulus is GoLang framework for building modular applications.`,
Args: cobra.NoArgs,
}

root.InitDefaultHelpCmd()
root.InitDefaultHelpFlag()
root.InitDefaultVersionFlag()

return root
},
),
cli.ProvideCommand(
func() *cobra.Command {
return &cobra.Command{
Use: "echo [string to echo]",
Short: "Echo anything to the screen",
Long: `echo is for echoing anything back.
Echo works a lot like print, except it has a child command.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
cmd.Println("Print: " + strings.Join(args, " "))
},
}
},
),
),
fx.Invoke(cli.Start),
fx.WithLogger(
func(logger *zap.Logger) fxevent.Logger {
return &fxevent.ZapLogger{Logger: logger}
},
),
)
app.Run()
}

func init() {
httpin.UseGochiURLParam("path", oChi.URLParam)
httpin.RegisterTypeDecoder(
reflect.TypeOf(uuid.UUID{}),
httpin.ValueTypeDecoderFunc(
func(s string) (interface{}, error) {
if s == "" {
return "", nil
}

err := validation.Validate(s, is.UUID)
if err != nil {
return nil, err
}

return uuid.FromString(s)
},
),
)
httpin.RegisterDirectiveExecutor(
"ctx",
httpin.DirectiveExecutorFunc(func(ctx *httpin.DirectiveContext) error {
ctx.Value.Elem().Set(reflect.ValueOf(ctx.Context))

return nil
}),
nil,
)
}
34 changes: 0 additions & 34 deletions cmd/server/main.go

This file was deleted.

16 changes: 16 additions & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type {CodegenConfig} from '@graphql-codegen/cli';

const config: CodegenConfig = {
overwrite: true,
schema: "http://localhost:8888/graphql",
generates: {
"integration/graphql.ts": {
plugins: ["typescript"]
},
"./graphql.schema.json": {
plugins: ["introspection"]
}
}
};

export default config;
12 changes: 12 additions & 0 deletions compose-dev.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
app:
entrypoint:
- sleep
- infinity
image: docker/dev-environments-go:stable-1
init: true
volumes:
- type: bind
source: /var/run/docker.sock
target: /var/run/docker.sock

80 changes: 80 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
version: '3.3'

volumes:
postgres:

services:
demo:
build: .
ports:
- 8888:8888
volumes:
- .:/go/src/app
restart: unless-stopped

postgres:
image: postgres:${POSTGRESQL_VERSION}-alpine
volumes:
- postgres:/var/lib/postgresql/data
environment:
POSTGRES_USER: modulus
POSTGRES_PASSWORD: secret
POSTGRES_DB: demo
ports:
- 5432:5432
restart: unless-stopped

# elasticsearch:
# container_name: temporal-elasticsearch
# environment:
# - cluster.routing.allocation.disk.threshold_enabled=true
# - cluster.routing.allocation.disk.watermark.low=512mb
# - cluster.routing.allocation.disk.watermark.high=256mb
# - cluster.routing.allocation.disk.watermark.flood_stage=128mb
# - discovery.type=single-node
# - ES_JAVA_OPTS=-Xms256m -Xmx256m
# - xpack.security.enabled=false
# image: elasticsearch:${ELASTICSEARCH_VERSION}
# expose:
# - 9200
#
# temporal:
# depends_on:
# - postgres
# - elasticsearch
# environment:
# - DB=postgresql
# - DB_PORT=5432
# - POSTGRES_USER=modulus
# - POSTGRES_PWD=secret
# - POSTGRES_SEEDS=postgres
# - DYNAMIC_CONFIG_FILE_PATH=config/dynamicconfig/development-sql.yaml
# - ENABLE_ES=true
# - ES_SEEDS=elasticsearch
# - ES_VERSION=v7
# image: temporalio/auto-setup:${TEMPORAL_VERSION}
# ports:
# - 7233:7233
# labels:
# kompose.volume.type: configMap
# volumes:
# - ./temporal/dynamicconfig:/etc/temporal/config/dynamicconfig
#
# temporal-admin-tools:
# depends_on:
# - temporal
# environment:
# - TEMPORAL_CLI_ADDRESS=temporal:7233
# image: temporalio/admin-tools:${TEMPORAL_VERSION}
# stdin_open: true
# tty: true
#
# temporal-ui:
# depends_on:
# - temporal
# environment:
# - TEMPORAL_ADDRESS=temporal:7233
# - TEMPORAL_CORS_ORIGINS=http://localhost:3000
# image: temporalio/ui:${TEMPORAL_UI_VERSION}
# ports:
# - 8080:8080
Loading