generated from Yandex-Practicum/go-musthave-shortener-tpl
-
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.
Merge pull request #25 from nglmq/iter25
Iter25
- Loading branch information
Showing
20 changed files
with
2,545 additions
and
29 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,30 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIFMDCCAxigAwIBAgICBnowDQYJKoZIhvcNAQELBQAwKDELMAkGA1UEBhMCUlUx | ||
GTAXBgNVBAoTEFlhbmRleC5QcmFrdGlrdW0wHhcNMjQwODE4MTI1MzIwWhcNMzQw | ||
ODE4MTI1MzIwWjAoMQswCQYDVQQGEwJSVTEZMBcGA1UEChMQWWFuZGV4LlByYWt0 | ||
aWt1bTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAMia0+SwQRJrzhax | ||
ZWFuBP13Sz8dC3BeINON4NFxXI1J6xAyNfNBqxXh7pQH0hTmoDBNJ1bdEuQg+td6 | ||
CgXFvEjU/ULEJJm+1bJmUvzWr3eiLKFrirHy7gPSupa70nv6wsnFncjwiU3/g2mI | ||
FfP2csqz8e6+UWrsUK3H4l3Mgk8cp+hkADyaAt6WPQjRHG1wYYZPtidUATfYWmXf | ||
XW3bC998XgmENyoVrBMjLNNGkxzb2r+J4RgLm9IZAPUv/wEAcW3Rw4HR/R4X/z1d | ||
IlTWhZYvMu0QO2CBafZbRU5kcRSuM9wcIZ3Xz7fgo2sNTfGeeQJ/dYA1o/RlaYra | ||
lTu3PMC725mOiwq0EqR88uutXd2jLKWLqTmaoxDe4+X19HtdG33SqI5ELrhRfZFR | ||
hTorkuBhYd+2K+/JxaGNqt/fcmSbQOyWsmHDesd4XeRQvjoZLCoGWyVCVDHlckQo | ||
PTXAXm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JAZ | ||
pdMS74B/Wyko/VzFTTqWeKOqKElrtpdCBlKnJvyp3sNkRVNP | ||
-----END CERTIFICATE----- |
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 |
---|---|---|
@@ -1,19 +1,76 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
grpcserver "github.com/nglmq/url-shortener/internal/app/grpc/server" | ||
"log" | ||
"net/http" | ||
_ "net/http/pprof" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
|
||
"github.com/nglmq/url-shortener/internal/app/cert" | ||
|
||
"github.com/nglmq/url-shortener/config" | ||
"github.com/nglmq/url-shortener/internal/app/server" | ||
) | ||
|
||
var ( | ||
buildVersion string = "N/A" | ||
buildDate string = "N/A" | ||
buildCommit string = "N/A" | ||
) | ||
|
||
func main() { | ||
fmt.Printf("Build version: %s\n", buildVersion) | ||
fmt.Printf("Build date: %s\n", buildDate) | ||
fmt.Printf("Build commit: %s\n", buildCommit) | ||
|
||
r, err := server.Start() | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
log.Fatal(http.ListenAndServe(config.FlagRunAddr, r)) | ||
if config.GRPCServer { | ||
if err := grpcserver.StartGRPCServer(); err != nil { | ||
log.Fatal(err) | ||
} | ||
} else { | ||
srv := &http.Server{ | ||
Addr: config.FlagRunAddr, | ||
Handler: r, | ||
} | ||
|
||
idleConnsClosed := make(chan struct{}) | ||
sigint := make(chan os.Signal, 3) | ||
|
||
signal.Notify(sigint, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) | ||
|
||
go func() { | ||
<-sigint | ||
|
||
if err := srv.Shutdown(context.Background()); err != nil { | ||
log.Printf("HTTP server Shutdown: %v", err) | ||
} | ||
close(idleConnsClosed) | ||
}() | ||
|
||
if config.EnableHTTPS { | ||
cert.CertGen() | ||
fmt.Printf("Starting server: %s", config.FlagRunAddr) | ||
err = srv.ListenAndServeTLS("cert.pem", "key.pem") | ||
} else { | ||
err = srv.ListenAndServe() | ||
} | ||
|
||
if !errors.Is(err, http.ErrServerClosed) { | ||
log.Fatalf("HTTP server ListenAndServe: %v", err) | ||
} | ||
|
||
<-idleConnsClosed | ||
fmt.Println("Server Shutdown gracefully") | ||
} | ||
} |
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,100 @@ | ||
// To start the staticcheck: | ||
// | ||
// 1. go build -o staticlint cmd/staticlint/main.go | ||
// 2. ./staticlint.exe <path to the package> | ||
|
||
package main | ||
|
||
import ( | ||
"go/ast" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/multichecker" | ||
"golang.org/x/tools/go/analysis/passes/asmdecl" | ||
"golang.org/x/tools/go/analysis/passes/assign" | ||
"golang.org/x/tools/go/analysis/passes/atomic" | ||
"golang.org/x/tools/go/analysis/passes/bools" | ||
"golang.org/x/tools/go/analysis/passes/buildtag" | ||
"golang.org/x/tools/go/analysis/passes/cgocall" | ||
"golang.org/x/tools/go/analysis/passes/composite" | ||
"golang.org/x/tools/go/analysis/passes/copylock" | ||
"golang.org/x/tools/go/analysis/passes/httpresponse" | ||
"golang.org/x/tools/go/analysis/passes/loopclosure" | ||
"golang.org/x/tools/go/analysis/passes/lostcancel" | ||
"golang.org/x/tools/go/analysis/passes/nilfunc" | ||
"golang.org/x/tools/go/analysis/passes/printf" | ||
"golang.org/x/tools/go/analysis/passes/shadow" | ||
"golang.org/x/tools/go/analysis/passes/shift" | ||
"golang.org/x/tools/go/analysis/passes/stdmethods" | ||
"golang.org/x/tools/go/analysis/passes/structtag" | ||
"golang.org/x/tools/go/analysis/passes/tests" | ||
"golang.org/x/tools/go/analysis/passes/unmarshal" | ||
"golang.org/x/tools/go/analysis/passes/unreachable" | ||
"golang.org/x/tools/go/analysis/passes/unsafeptr" | ||
"golang.org/x/tools/go/analysis/passes/unusedresult" | ||
"honnef.co/go/tools/staticcheck" | ||
) | ||
|
||
// ErrCheckOsExit checks os.Exit call in main function | ||
var ErrCheckOsExit = &analysis.Analyzer{ | ||
Name: "errcheckosexit", | ||
Doc: "check os.Exit call in main function", | ||
Run: run, | ||
} | ||
|
||
func run(pass *analysis.Pass) (interface{}, error) { | ||
for _, file := range pass.Files { | ||
ast.Inspect(file, func(n ast.Node) bool { | ||
if call, ok := n.(*ast.CallExpr); ok { | ||
if s, ok := call.Fun.(*ast.SelectorExpr); ok { | ||
if x, ok := s.X.(*ast.Ident); ok { | ||
if x.Name == "os" && s.Sel.Name == "Exit" { | ||
pass.Reportf(call.Pos(), "os.Exit called") | ||
} | ||
} | ||
} | ||
} | ||
|
||
return true | ||
}) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
func main() { | ||
checks := []*analysis.Analyzer{ | ||
asmdecl.Analyzer, | ||
assign.Analyzer, | ||
atomic.Analyzer, | ||
bools.Analyzer, | ||
buildtag.Analyzer, | ||
cgocall.Analyzer, | ||
composite.Analyzer, | ||
copylock.Analyzer, | ||
httpresponse.Analyzer, | ||
loopclosure.Analyzer, | ||
lostcancel.Analyzer, | ||
nilfunc.Analyzer, | ||
printf.Analyzer, | ||
shift.Analyzer, | ||
shadow.Analyzer, | ||
stdmethods.Analyzer, | ||
structtag.Analyzer, | ||
tests.Analyzer, | ||
unmarshal.Analyzer, | ||
unreachable.Analyzer, | ||
unsafeptr.Analyzer, | ||
unusedresult.Analyzer, | ||
ErrCheckOsExit, | ||
} | ||
|
||
for _, v := range staticcheck.Analyzers { | ||
if strings.HasPrefix(v.Doc.Title, "SA") { | ||
checks = append(checks, v.Analyzer) | ||
} | ||
} | ||
|
||
multichecker.Main(checks...) | ||
} |
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,9 @@ | ||
{ | ||
"server_address": "localhost:8080", | ||
"base_url": "http://localhost:8080", | ||
"file_storage_path": "", | ||
"database_dsn": "postgres://postgres:ANTON_og228@localhost:5432/postgres", | ||
"enable_https": false, | ||
"trusted_subnet": "79.139.178.194/32", | ||
"grpc_server": true | ||
} |
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,56 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
// JSONConfig Struct for JSON configuration | ||
type JSONConfig struct { | ||
ServerAddress string `json:"server_address"` // аналог переменной окружения SERVER_ADDRESS или флага -a | ||
BaseURL string `json:"base_url"` // аналог переменной окружения BASE_URL или флага -b | ||
FileStoragePath string `json:"file_storage_path"` // аналог переменной окружения FILE_STORAGE_PATH или флага -f | ||
DatabaseDSN string `json:"database_dsn"` // аналог переменной окружения DATABASE_DSN или флага -d | ||
EnableHTTPS bool `json:"enable_https"` // аналог переменной окружения ENABLE_HTTPS или флага -s | ||
TrustedSubnet string `json:"trusted_subnet"` // аналог переменной окружения TRUSTED_SUBNET или флага -t | ||
GRPCServer bool `json:"grpc_server"` // аналог переменной окружения GRPC_SERVER или флага -g | ||
} | ||
|
||
// ReadJSONConfig read config from file | ||
func ReadJSONConfig(filename string) error { | ||
file, err := os.Open(filename) | ||
if err != nil { | ||
return fmt.Errorf("failed to open file: %v", err) | ||
} | ||
|
||
var config JSONConfig | ||
|
||
if err := json.NewDecoder(file).Decode(&config); err != nil { | ||
return fmt.Errorf("error decoding json: %v", err) | ||
} | ||
|
||
if config.ServerAddress != "" { | ||
FlagRunAddr = config.ServerAddress | ||
} | ||
if config.BaseURL != "" { | ||
FlagBaseURL = config.BaseURL | ||
} | ||
if config.FileStoragePath != "" { | ||
FlagInMemoryStorage = config.FileStoragePath | ||
} | ||
if config.DatabaseDSN != "" { | ||
DBConnection = config.DatabaseDSN | ||
} | ||
if config.EnableHTTPS { | ||
EnableHTTPS = true | ||
} | ||
if config.TrustedSubnet != "" { | ||
TrustedSubnet = config.TrustedSubnet | ||
} | ||
if config.GRPCServer { | ||
GRPCServer = true | ||
} | ||
|
||
return 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
Oops, something went wrong.