generated from yandex-praktikum/go-musthave-shortener-tpl
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Iter8 #8
Merged
Merged
Iter8 #8
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dfc8f77
Инкремент №4
alexunder123 323e585
инкремент №4
alexunder123 5906c48
Инкремент №5.1
alexunder123 6da5c9d
Инкремент №5.2
alexunder123 866fadf
Инкремент №6
alexunder123 14fbedc
Инкремент №6.1
alexunder123 ff3fe9e
Инкремент №7
alexunder123 31f1c12
Инкремент №7.1
alexunder123 ebb163c
Инкремент №8
alexunder123 30a0da6
Инкремент №8.1
alexunder123 5d51e81
Inc №8.1
alexunder123 8e20d2a
Инкремент №8.3
alexunder123 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -21,3 +21,8 @@ vendor/ | |
# IDEs directories | ||
.idea | ||
.vscode | ||
/temp | ||
|
||
#Temporary files | ||
$TEMP_FILE | ||
*.json |
Binary file not shown.
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
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,30 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/caarlos0/env/v6" | ||
) | ||
|
||
type Param struct { | ||
Server string `env:"SERVER_ADDRESS"` | ||
URL string `env:"BASE_URL"` | ||
Storage string `env:"FILE_STORAGE_PATH"` | ||
SaveFile int | ||
} | ||
|
||
func NewEnv() *Param { | ||
var Params Param | ||
|
||
err := env.Parse(&Params) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
ReadFlags(&Params) | ||
|
||
if Params.Storage != "" { | ||
Params.SaveFile = 1 | ||
} | ||
|
||
return &Params | ||
} |
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,34 @@ | ||
package config | ||
|
||
import ( | ||
"os" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestGetEnv(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
want *Param | ||
}{ | ||
{ | ||
name: "test1", | ||
want: &Param{ | ||
Server: "127.0.0.1:8080", | ||
URL: "http://127.0.0.1:8080", | ||
Storage: "jsonDB.json", | ||
SaveFile: 1, | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
os.Setenv("SERVER_ADDRESS", "127.0.0.1:8080") | ||
os.Setenv("BASE_URL", "http://127.0.0.1:8080") | ||
os.Setenv("FILE_STORAGE_PATH", "jsonDB.json") | ||
if got := NewEnv(); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GetEnv() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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,18 @@ | ||
package config | ||
|
||
import ( | ||
"flag" | ||
) | ||
|
||
func ReadFlags(P *Param) { | ||
if P.Server == "" { | ||
flag.StringVar(&P.Server, "a", "127.0.0.1:8080", "Адрес запускаемого сервера") | ||
} | ||
if P.URL == "" { | ||
flag.StringVar(&P.URL, "b", "http://127.0.0.1:8080", "Базовый адрес результирующего URL") | ||
} | ||
if P.Storage == "" { | ||
flag.StringVar(&P.Storage, "f", "", "Хранилище URL") | ||
} | ||
flag.Parse() | ||
} |
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
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,44 @@ | ||
package handlers | ||
|
||
import ( | ||
"compress/gzip" | ||
|
||
"io" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type gzipWriter struct { | ||
http.ResponseWriter | ||
Writer io.Writer | ||
} | ||
|
||
func (w gzipWriter) Write(b []byte) (int, error) { | ||
return w.Writer.Write(b) | ||
} | ||
|
||
func Decompress(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Header.Get("Content-Encoding") == "gzip" { | ||
gz, err := gzip.NewReader(r.Body) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
decompressed := gz | ||
defer gz.Close() | ||
r.Body = io.NopCloser(decompressed) | ||
next.ServeHTTP(w, r) | ||
return | ||
} else if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { | ||
gz := gzip.NewWriter(w) | ||
defer gz.Close() | ||
w.Header().Set("Content-Encoding", "gzip") | ||
next.ServeHTTP(gzipWriter{ResponseWriter: w, Writer: gz}, r) | ||
return | ||
} else { | ||
next.ServeHTTP(w, r) | ||
return | ||
} | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Config обычно всегда config. Приблизительно так будет читабельней: