Skip to content

Commit

Permalink
Additional fixes for golang http app
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Nov 12, 2024
1 parent 08404ea commit c275934
Show file tree
Hide file tree
Showing 13 changed files with 126 additions and 680 deletions.
3 changes: 2 additions & 1 deletion examples/golang/cource/06h_http/api/middleware/logger.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package middleware

import (
"log"
"net/http"

log "github.com/sirupsen/logrus"
)

func RequestLog(next http.Handler) http.Handler {
Expand Down
2 changes: 1 addition & 1 deletion examples/golang/cource/06h_http/api/routes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api

import (
"golang/cource/06h_http/internals/app/handlers"
"06h_http/internals/app/handlers"

"github.com/gorilla/mux"
)
Expand Down
4 changes: 2 additions & 2 deletions examples/golang/cource/06h_http/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (

log "github.com/sirupsen/logrus"

"golang/cource/06h_http/internals/app"
"golang/cource/06h_http/internals/cfg"
"06h_http/internals/app"
"06h_http/internals/cfg"
)

func main() {
Expand Down
29 changes: 13 additions & 16 deletions examples/golang/cource/06h_http/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,39 @@ go 1.23

require (
github.com/georgysavva/scany v1.2.2
github.com/gorilla/mux v1.8.0
github.com/gorilla/mux v1.8.1
github.com/jackc/pgx/v4 v4.18.3
github.com/sirupsen/logrus v1.9.3
github.com/spf13/viper v1.19.0
)

require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/georgysavva/scany/v2 v2.1.3 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/pgx/v5 v5.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgtype v1.14.4 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/jackc/puddle/v2 v2.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.3 // indirect
github.com/sagikazarmark/locafero v0.6.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.11.0 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/cast v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
714 changes: 76 additions & 638 deletions examples/golang/cource/06h_http/go.sum

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (storage *CarsStorage) GetCarsList(userIdFilter int64, brandFilter string,
return result
}

func (storage *CarsStorage) GetCarsById(id int64) models.Car {
func (storage *CarsStorage) GetCarById(id int64) models.Car {
query := "SELECT users.id AS userid, users.name, users.rank, c.id, c.brand, c.colour, c.license_plate " +
"FROM users JOIN cars c on users.id = c.user_id WHERE c.id = $1"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import (
"context"
"fmt"

"golang/cource/06h_http/internals/app/models"
"06h_http/internals/app/models"

"github.com/georgysavva/scany/pgxscan"
"github.com/jackc/pgx/v4/pgxpool"
log "github.com/sirupsen/logrus"
)

type UserStorage struct {
type UsersStorage struct {
databasePool *pgxpool.Pool
}

func NewUsersStorage(pool *pgxpool.Pool) *UserStorage {
storage := new(UserStorage)
func NewUsersStorage(pool *pgxpool.Pool) *UsersStorage {
storage := new(UsersStorage)
storage.databasePool = pool
return storage
}

func (storage *UserStorage) GetUsersList(nameFilter string) []models.User {
func (storage *UsersStorage) GetUsersList(nameFilter string) []models.User {
query := "SELECT id, name, rank FROM users"
args := make([]interface{}, 0)
if nameFilter != "" {
Expand All @@ -39,7 +39,7 @@ func (storage *UserStorage) GetUsersList(nameFilter string) []models.User {
return result
}

func (storage *UserStorage) GetUserById(id int64) models.User {
func (storage *UsersStorage) GetUserById(id int64) models.User {
query := "SELECT id, name, rank FROM users WHERE id = $1"

var result models.User
Expand All @@ -52,7 +52,7 @@ func (storage *UserStorage) GetUserById(id int64) models.User {
return result
}

func (storage *UserStorage) CreateUser(user models.User) error {
func (storage *UsersStorage) CreateUser(user models.User) error {
query := "INSERT INTO users(name, rank) VALUES ($1, $2)"

_, err := storage.databasePool.Exec(context.Background(), query, user.Name, user.Rank)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package handlers
import (
"encoding/json"
"errors"
"golang/cource/06h_http/internals/app/processors"
"06h_http/internals/app/processors"
"06h_http/internals/app/models"
"net/http"
"strconv"
"strings"
Expand All @@ -22,7 +23,7 @@ func NewCarsHandler(processor *processors.CarsProcessor) *CarsHandler {
}

func (handler *CarsHandler) Create(response http.ResponseWriter, request *http.Request) {
var newCar models.Cars
var newCar models.Car
err := json.NewDecoder(request.Body).Decode(&newCar)
if err != nil {
WrapError(response, err)
Expand Down Expand Up @@ -54,8 +55,7 @@ func (handler *CarsHandler) List(response http.ResponseWriter, request *http.Req
return
}
}
list, err := handler.processor.ListCars(userIdFilter, strings.Trim(vars.Get("brand"), "\""),
strings.Trim(vars.Get("colour"), "\""), strings.Trim(vars.Get("license_plate"), "\""))
list, err := handler.processor.ListCars(userIdFilter, strings.Trim(vars.Get("brand"), "\""), strings.Trim(vars.Get("colour"), "\""), strings.Trim(vars.Get("license_plate"), "\""))
if err != nil {
WrapError(response, err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package handlers
import (
"encoding/json"
"errors"
"golang/cource/06h_http/internals/app/processors"
"06h_http/internals/app/processors"
"06h_http/internals/app/models"
"net/http"
"strconv"
"strings"
Expand Down
12 changes: 9 additions & 3 deletions examples/golang/cource/06h_http/internals/app/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package app

import (
"context"
"golang/cource/06h_http/internals/cfg"
"log"
"06h_http/internals/cfg"
"06h_http/internals/app/db"
"06h_http/internals/app/processors"
"06h_http/internals/app/handlers"
"06h_http/api"
"06h_http/api/middleware"

log "github.com/sirupsen/logrus"
"net/http"

"github.com/jackc/pgx/v4/pgxpool"
Expand All @@ -26,7 +32,7 @@ func NewServer(config cfg.Cfg, ctx context.Context) *AppServer {
func (server *AppServer) Serve() {
log.Println("Startung server")
log.Println(server.config.GetDBString())
server.db, err := pgxpool.Connect(server.ctx, server.config.GetDBString())
server.db, err = pgxpool.Connect(server.ctx, server.config.GetDBString())
if err != nil {
log.Fatalln(err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package processors

import (
"06h_http/internals/app/models"
"06h_http/internals/app/db"
"errors"
)

Expand Down Expand Up @@ -33,7 +34,7 @@ func (processor *CarsProcessor) CreateCar(car models.Car) error {
}

func (processor *CarsProcessor) FindCar(id int64) (models.Car, error) {
user := processor.storage.GetCarById(id)
car := processor.storage.GetCarById(id)
if car.Id != id {
return car, errors.New("Car not found.")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package processors

import (
"errors"
"golang/cource/06h_http/internals/app/models"
"06h_http/internals/app/models"
"06h_http/internals/app/db"
)

type UsersProcessor struct {
Expand Down Expand Up @@ -32,5 +33,5 @@ func (processor *UsersProcessor) FindUser(id int64) (models.User, error) {
}

func (processor *UsersProcessor) ListUsers(nameFilter string) ([]models.User, error) {
return processor.storage.GetUserList(nameFilter), nil
return processor.storage.GetUsersList(nameFilter), nil
}
7 changes: 4 additions & 3 deletions examples/golang/cource/06h_http/internals/cfg/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cfg

import(
log "github.com/sirupsen/logrus"
"fmt"
"github.com/spf13/viper"
log "github.com/sirupsen/logrus"
)

type Cfg struct {
Expand All @@ -26,13 +27,13 @@ func LoadAndStoreConfig() Cfg {

var cfg Cfg

err := attr.Unmarshal(%cfg)
err := attr.Unmarshal(&cfg)
if err != nil {
log.Panic(err)
}
return cfg
}

func (cfg *Cfg) GetDBString() string {
retrun fmt.Sprintf("postgres://%v:%v@%v:%v/%v", cfg.DbUser, cfg.DbPass, cfg.DbHost, cfg.DbPort, cfg.DbName)
return fmt.Sprintf("postgres://%v:%v@%v:%v/%v", cfg.DbUser, cfg.DbPass, cfg.DbHost, cfg.DbPort, cfg.DbName)
}

0 comments on commit c275934

Please sign in to comment.