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

init base project folder structure #37

Closed
wants to merge 2 commits into from
Closed
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
20 changes: 19 additions & 1 deletion cmd/gophermart/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
package main

func main() {}
import (
"github.com/sashaaro/go-musthave-diploma-tpl/internal"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/adapters"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/handlers"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/infra"
"log"
"net/http"
)

func main() {
internal.InitConfig()

logger := adapters.CreateLogger()
pool := infra.CreatePgxPool()
//nolint:errcheck
defer pool.Close()

log.Fatal(http.ListenAndServe(internal.Config.ServerAddress, handlers.CreateServeMux(logger, pool)))
}
26 changes: 26 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module github.com/sashaaro/go-musthave-diploma-tpl

go 1.21.7

require (
github.com/caarlos0/env/v6 v6.10.1
github.com/go-chi/chi/v5 v5.0.12
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v5 v5.5.5
github.com/pressly/goose/v3 v3.19.2
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.18.0
)

require (
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/mfridman/interpolate v0.0.2 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
github.com/stretchr/testify v1.9.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/text v0.14.0 // indirect
)
194 changes: 194 additions & 0 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions internal/adapters/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package adapters

import (
"context"
"errors"
"github.com/google/uuid"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/utils"
"net/http"
)

type userContext struct{}

func UserIDFromReq(req *http.Request) (uuid.UUID, error) {
return UserIDFromCtx(req.Context())
}
func MustUserIDFromReq(req *http.Request) uuid.UUID {
return utils.Must(UserIDFromReq(req))
}

func UserIDFromCtx(ctx context.Context) (uuid.UUID, error) {
userID, ok := ctx.Value(&userContext{}).(uuid.UUID)
if !ok {
return uuid.Nil, errors.New("user id not found")
}
return userID, nil
}

func UserIDToCxt(ctx context.Context, userID uuid.UUID) context.Context {
return context.WithValue(ctx, &userContext{}, userID)
}
15 changes: 15 additions & 0 deletions internal/adapters/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package adapters

import "go.uber.org/zap"

func CreateLogger() zap.SugaredLogger {
l, err := zap.NewDevelopment()
if err != nil {
// вызываем панику, если ошибка
panic(err)
}
//nolint:errcheck
defer l.Sync()

return *l.Sugar()
}
40 changes: 40 additions & 0 deletions internal/adapters/postgres/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package postgres

import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/domain"
)

type PgUserRepository struct {
db *pgxpool.Pool
}

var _ domain.UserRepository = (*PgUserRepository)(nil)

func NewPgUserRepository(db *pgxpool.Pool) *PgUserRepository {
return &PgUserRepository{
db: db,
}
}

func (r *PgUserRepository) Insert(ctx context.Context, login, passwordHash string) (int, error) {
var id int
err := r.db.QueryRow(ctx, `INSERT INTO users (login, password_hash) VALUES ($1, $2) RETURN id`, login, passwordHash).Scan(&id)
if err != nil {
return 0, err
}

return id, err
}

func (r *PgUserRepository) GetByID(ctx context.Context, id int) (domain.User, error) {
var u domain.User
err := r.db.QueryRow(ctx, `SELECT * FROM users WHERE id = $1`, id).Scan(&u.ID, &u.Login, &u.PasswordHash)
if errors.Is(err, pgx.ErrNoRows) {
return u, domain.ErrNotFound
}
return u, err
}
3 changes: 3 additions & 0 deletions internal/app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# internal/app

В данной директории будет содержаться имплементация вашего сервиса
10 changes: 10 additions & 0 deletions internal/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package internal

var Config = config{}

type config struct {
ServerAddress string `env:"SERVER_ADDRESS"`
BaseURL string `env:"BASE_URL"`
DatabaseDSN string `env:"DATABASE_DSN"`
JwtSecret string `env:"JWT_SECRET"`
}
5 changes: 5 additions & 0 deletions internal/domain/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package domain

import "errors"

var ErrNotFound = errors.New("not found")
14 changes: 14 additions & 0 deletions internal/domain/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package domain

import "context"

type UserRepository interface {
Insert(ctx context.Context, login, password string) (int, error)
GetByID(ctx context.Context, id int) (User, error)
}

type User struct {
ID int `json:"id"`
Login string `json:"login"`
PasswordHash string `json:"-"`
}
50 changes: 50 additions & 0 deletions internal/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package internal

import (
"flag"
"fmt"
"github.com/caarlos0/env/v6"
"log"
"net/url"
"strings"
)

func InitConfig() {
serverAddress := flag.String("a", "", "listen address")
baseURL := flag.String("b", "", "base url")
databaseDSN := flag.String("d", "", "database dsn")

flag.Parse()

if err := env.Parse(&Config); err != nil {
fmt.Printf("%+v\n", err)
}
if Config.ServerAddress == "" {
Config.ServerAddress = *serverAddress
}
if Config.BaseURL == "" {
Config.BaseURL = *baseURL
}

if Config.ServerAddress == "" {
Config.ServerAddress = ":8080"
}
if Config.BaseURL == "" {
Config.BaseURL = "http://localhost:8080"
}

_, err := url.Parse(Config.BaseURL)
if err != nil {
log.Fatal("invalid base url: ", err)
}

if Config.DatabaseDSN == "" {
Config.DatabaseDSN = *databaseDSN
}

Config.DatabaseDSN = strings.TrimSpace(Config.DatabaseDSN)

if Config.JwtSecret == "" {
Config.JwtSecret = "secret"
}
}
35 changes: 35 additions & 0 deletions internal/handlers/handlers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package handlers

import (
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/adapters/postgres"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/service"
"go.uber.org/zap"
)

type HTTPHandlers struct {
logger zap.SugaredLogger
userService *service.UserService
}

func NewHTTPHandlers(logger zap.SugaredLogger, userService *service.UserService) *HTTPHandlers {
return &HTTPHandlers{
logger: logger,
userService: userService,
}
}

func CreateServeMux(logger zap.SugaredLogger, pool *pgxpool.Pool) *chi.Mux {
r := chi.NewRouter()
r.Use(middleware.Logger)

userRepo := postgres.NewPgUserRepository(pool)
userService := service.NewUserService(userRepo)
handlers := NewHTTPHandlers(logger, userService)

r.Post("/api/user/register", WithAuth(false, WithLogging(logger, handlers.registerUser)))

return r
}
25 changes: 25 additions & 0 deletions internal/handlers/handlers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package handlers

import (
"github.com/sashaaro/go-musthave-diploma-tpl/internal"
"github.com/sashaaro/go-musthave-diploma-tpl/internal/adapters"
"net/http"
"testing"
)

func TestRegisterUser(t *testing.T) {
httpClient := http.Client{CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse
}}

internal.InitConfig()

logger := adapters.CreateLogger()
_ = logger
_ = httpClient

t.Run("create short url, pass through short url", func(t *testing.T) {

})

}
Loading