Skip to content

Commit

Permalink
basics
Browse files Browse the repository at this point in the history
  • Loading branch information
bnkamalesh committed Feb 17, 2024
1 parent d6de3b7 commit f4cb398
Show file tree
Hide file tree
Showing 14 changed files with 103 additions and 65 deletions.
6 changes: 4 additions & 2 deletions cmd/server/http/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"runtime/debug"

"github.com/bnkamalesh/errors"
"github.com/bnkamalesh/webgo/v6"
"github.com/bnkamalesh/webgo/v7"

"github.com/bnkamalesh/goapp/internal/api"
"github.com/bnkamalesh/goapp/internal/pkg/logger"
Expand Down Expand Up @@ -106,7 +106,9 @@ func errWrapper(h func(w http.ResponseWriter, r *http.Request) error) http.Handl

status, msg, _ := errors.HTTPStatusCodeMessage(err)
webgo.SendError(w, msg, status)
_ = logger.Error(r.Context(), errors.Stacktrace(err))
if status > 499 {
_ = logger.Error(r.Context(), errors.Stacktrace(err))
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/server/http/handlers_users.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"

"github.com/bnkamalesh/errors"
"github.com/bnkamalesh/webgo/v6"
"github.com/bnkamalesh/webgo/v7"

"github.com/bnkamalesh/goapp/internal/users"
)
Expand Down
29 changes: 12 additions & 17 deletions cmd/server/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/bnkamalesh/goapp/internal/api"
"github.com/bnkamalesh/goapp/internal/pkg/apm"
"github.com/bnkamalesh/webgo/v6"
"github.com/bnkamalesh/webgo/v6/middleware/accesslog"
"github.com/bnkamalesh/webgo/v7"
"github.com/bnkamalesh/webgo/v7/middleware/accesslog"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
)

Expand All @@ -24,18 +24,18 @@ type Config struct {
DialTimeout time.Duration

TemplatesBasePath string
EnableAccessLog bool
}

type HTTP struct {
apis api.Server
listener string
server *http.Server
server *webgo.Router
}

// Start starts the HTTP server
func (h *HTTP) Start() error {
webgo.LOGHANDLER.Info("HTTP server, listening on", h.listener)
return h.server.ListenAndServe()
h.server.Start()
return nil
}

// NewService returns an instance of HTTP with all its dependencies set
Expand All @@ -61,7 +61,10 @@ func NewService(cfg *Config, apis api.Server) (*HTTP, error) {
handlers.routes()...,
)

router.Use(accesslog.AccessLog)
if cfg.EnableAccessLog {
router.Use(accesslog.AccessLog)
router.UseOnSpecialHandlers(accesslog.AccessLog)
}
router.Use(panicRecoverer)

// in this app, /-/ prefixed routes are used for healthchecks, readiness checks etc.
Expand All @@ -79,17 +82,9 @@ func NewService(cfg *Config, apis api.Server) (*HTTP, error) {
return wctx.Route.Pattern
})

httpServer := &http.Server{
Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
Handler: apm.NewHTTPMiddleware()(router),
ReadTimeout: cfg.ReadTimeout,
ReadHeaderTimeout: cfg.ReadTimeout,
WriteTimeout: cfg.WriteTimeout,
IdleTimeout: cfg.ReadTimeout * 2,
}

_ = apm.NewHTTPMiddleware()
return &HTTP{
server: httpServer,
server: router,
listener: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port),
}, nil
}
17 changes: 13 additions & 4 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
version: "3.9"
services:
redis:
image: "redis:alpine"
image: "redis:7"
networks:
- goapp_network

postgres:
image: "postgres"
image: "postgres:16"
environment:
POSTGRES_PASSWORD: gauserpassword
POSTGRES_USER: gauser
POSTGRES_DB: goapp
ports:
- "5432:5432"
networks:
- goapp_network

goapp:
image: golang:1.21
image: golang:1.22
volumes:
- ${PWD}:/app
working_dir: /app
tty: true
# command: go run main.go
environment:
TEMPLATES_BASEPATH: /app/cmd/server/http/web/templates
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_STORENAME: "goapp"
POSTGRES_USERNAME: "gauser"
POSTGRES_PASSWORD: "gauserpassword"
command: go run main.go
ports:
- "8080:8080"
depends_on:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ go 1.21
require (
github.com/Masterminds/squirrel v1.5.4
github.com/bnkamalesh/errors v0.11.1
github.com/bnkamalesh/webgo/v6 v6.7.0
github.com/bnkamalesh/webgo/v7 v7.0.0
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.5.3
github.com/prometheus/client_golang v1.18.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.48.0
Expand Down Expand Up @@ -33,7 +34,6 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/bnkamalesh/errors v0.11.1 h1:lxPIza88BwIf/k8jD1UlQyLS+QzEMCwqM2I6Uo81GVs=
github.com/bnkamalesh/errors v0.11.1/go.mod h1:X8+uM23IDiSq8q5kYC3dEP+sMTRlZz0IWFXo7sfrn18=
github.com/bnkamalesh/webgo/v6 v6.7.0 h1:OjS87ncnwzHeWCXyr4TPIOcW4qlNifHiRy7/Kk8VXRA=
github.com/bnkamalesh/webgo/v6 v6.7.0/go.mod h1:QHOzBydSNRq5oYWgg9v7A0RIUlyaZeT9nYCx41Sgzd8=
github.com/bnkamalesh/webgo/v7 v7.0.0 h1:Kdr32fx45IlSKmJfUeFipLKXxV+ETgynKpFV1/fCtmU=
github.com/bnkamalesh/webgo/v7 v7.0.0/go.mod h1:zS1pR4L5TSKja095la5nbv2yZ59/c4PoSungO93E8UQ=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down
28 changes: 28 additions & 0 deletions internal/configs/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/bnkamalesh/goapp/cmd/server/http"
"github.com/bnkamalesh/goapp/internal/pkg/postgres"
)

type env string
Expand All @@ -31,6 +32,7 @@ type Configs struct {
// HTTP returns the configuration required for HTTP package
func (cfg *Configs) HTTP() (*http.Config, error) {
return &http.Config{
EnableAccessLog: (cfg.Environment == EnvLocal) || (cfg.Environment == EnvTest),
TemplatesBasePath: strings.TrimSpace(os.Getenv("TEMPLATES_BASEPATH")),
Port: 8080,
ReadTimeout: time.Second * 5,
Expand All @@ -39,6 +41,32 @@ func (cfg *Configs) HTTP() (*http.Config, error) {
}, nil
}

func (cfg *Configs) Postgres() *postgres.Config {
return &postgres.Config{
Host: os.Getenv("POSTGRES_HOST"),
Port: os.Getenv("POSTGRES_PORT"),
Driver: "postgres",

StoreName: os.Getenv("POSTGRES_STORENAME"),
Username: os.Getenv("POSTGRES_USERNAME"),
Password: os.Getenv("POSTGRES_PASSWORD"),

ConnPoolSize: 24,
ReadTimeout: time.Second * 3,
WriteTimeout: time.Second * 6,
IdleTimeout: time.Minute,
DialTimeout: time.Second * 3,
}
}

func (cfg *Configs) UserPostgresTable() string {
return "users"
}

func (cfg *Configs) UserNotesPostgresTable() string {
return "user_notes"
}

func loadEnv() env {
switch env(os.Getenv("ENV")) {
case EnvLocal:
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func (cfg *Config) ConnURL() string {
)
}

// NewService returns a new instance of PGX pool
func NewService(cfg *Config) (*pgxpool.Pool, error) {
// NewPool returns a new instance of PGX pool
func NewPool(cfg *Config) (*pgxpool.Pool, error) {
poolcfg, err := pgxpool.ParseConfig(cfg.ConnURL())
if err != nil {
return nil, errors.Wrap(err, "failed to parse config")
Expand Down
3 changes: 3 additions & 0 deletions internal/users/store_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func (ps *pgstore) GetUserByEmail(ctx context.Context, email string) (*User, err
row := ps.pqdriver.QueryRow(ctx, query, args...)
err = row.Scan(user.ID, user.Name, phone, address)
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return nil, errors.NotFoundErr(ErrUserEmailNotFound, email)
}
return nil, errors.Wrap(err, "failed getting user info")
}
user.Address = address.String
Expand Down
4 changes: 4 additions & 0 deletions internal/users/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"github.com/bnkamalesh/goapp/internal/pkg/logger"
)

var (
ErrUserEmailNotFound = errors.New("user with the email not found")
)

type User struct {
ID string
Name string
Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/bnkamalesh/goapp/internal/configs"
"github.com/bnkamalesh/goapp/internal/pkg/apm"
"github.com/bnkamalesh/goapp/internal/pkg/logger"
"github.com/bnkamalesh/goapp/internal/pkg/postgres"
"github.com/bnkamalesh/goapp/internal/pkg/sysignals"
"github.com/bnkamalesh/goapp/internal/users"
)
Expand Down Expand Up @@ -131,8 +132,12 @@ func main() {
)

apmhandler := startAPM(ctx, cfgs)
pqdriver, err := postgres.NewPool(cfgs.Postgres())
if err != nil {
panic(errors.Wrap(err))
}

userPGstore := users.NewPostgresStore(nil, "")
userPGstore := users.NewPostgresStore(pqdriver, cfgs.UserPostgresTable())
userSvc := users.NewService(userPGstore)
svrAPIs := api.NewServer(userSvc)
hserver, gserver := startServers(svrAPIs, cfgs)
Expand Down
11 changes: 11 additions & 0 deletions schemas/functions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN
NEW.updated_at = now();
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;
$$ language 'plpgsql';
11 changes: 11 additions & 0 deletions schemas/user_notes.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS user_notes (
id UUID PRIMARY KEY,
title TEXT,
content TEXT,
user_id UUID references users(id),
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
);

CREATE TRIGGER tr_users_bu BEFORE UPDATE on user_notes
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
38 changes: 4 additions & 34 deletions schemas/users.sql
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
CREATE TABLE IF NOT EXISTS Users (
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY,
email TEXT UNIQUE,
uname TEXT,
phone TEXT,
uaddress TEXT,
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
)
);

CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN
NEW.updated_at = now();
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;
$$ language 'plpgsql';
--
CREATE TABLE IF NOT EXISTS UserNotes (
id UUID PRIMARY KEY,
title TEXT,
content TEXT,
user_id references Users(id),
created_at timestamptz DEFAULT now(),
updated_at timestamptz DEFAULT now()
)

CREATE OR REPLACE FUNCTION update_updated_at_column()
RETURNS TRIGGER AS $$
BEGIN
IF row(NEW.*) IS DISTINCT FROM row(OLD.*) THEN
NEW.updated_at = now();
RETURN NEW;
ELSE
RETURN OLD;
END IF;
END;
$$ language 'plpgsql';
CREATE TRIGGER tr_users_bu BEFORE UPDATE on users
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();

0 comments on commit f4cb398

Please sign in to comment.