-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
117 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
type Config struct{} | ||
|
||
func New(path string) (Config, error) { | ||
viper.SetConfigFile(path) | ||
|
||
var config Config | ||
|
||
if err := viper.ReadInConfig(); err != nil { | ||
return config, fmt.Errorf("failed to read config: %w", err) | ||
} | ||
|
||
if err := viper.Unmarshal(&config); err != nil { | ||
return config, fmt.Errorf("failed to unmarshal config: %w", err) | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package httpserver | ||
|
||
import ( | ||
"net" | ||
"time" | ||
) | ||
|
||
type Option func(*Server) | ||
|
||
func Port(port string) Option { | ||
return func(s *Server) { | ||
s.server.Addr = net.JoinHostPort("", port) | ||
} | ||
} | ||
|
||
func ReadTimeout(timeout time.Duration) Option { | ||
return func(s *Server) { | ||
s.server.ReadTimeout = timeout | ||
} | ||
} | ||
|
||
func WriteTimeout(timeout time.Duration) Option { | ||
return func(s *Server) { | ||
s.server.WriteTimeout = timeout | ||
} | ||
} | ||
|
||
func ShutdownTimeout(timeout time.Duration) Option { | ||
return func(s *Server) { | ||
s.shutdownTimeout = timeout | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package mongodb | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
const ( | ||
DefaultTimeout = 5 * time.Second | ||
) | ||
|
||
// Конструктор устанавливает связь с mongodb | ||
func Connect(ctx context.Context, options *options.ClientOptions) (*mongo.Client, error) { | ||
client, err := mongo.Connect(ctx, options) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect to mongodb: %w", err) | ||
} | ||
|
||
ctxTimeout, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
|
||
if err := client.Ping(ctxTimeout, nil); err != nil { | ||
return nil, fmt.Errorf("failed to ping db: %w", err) | ||
} | ||
|
||
return client, nil | ||
} |