Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2 use viper to read config file and env vars
- Loading branch information
1 parent
79e6a52
commit 976999b
Showing
4 changed files
with
31 additions
and
13 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 |
---|---|---|
@@ -1 +1,2 @@ | ||
awss3urlsigner | ||
config.yaml |
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 |
---|---|---|
@@ -1,24 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/derektamsen/awss3urlsigner/aws" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/derektamsen/awss3urlsigner/aws" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
func handler(w http.ResponseWriter, r *http.Request) { | ||
presigned_url := awsurl.PreSign(r.URL.Path[1:]) | ||
http.Redirect(w, r, presigned_url, http.StatusFound) | ||
func getConfig() { | ||
viper.SetConfigName("config") | ||
viper.AddConfigPath(".") | ||
err := viper.ReadInConfig() | ||
if err != nil { | ||
panic(fmt.Errorf("Fatal error config file: %s", err)) | ||
} | ||
} | ||
|
||
func httphandler(w http.ResponseWriter, r *http.Request) { | ||
presignedURL := awsurl.S3PreSign(r.URL.Path[1:]) | ||
http.Redirect(w, r, presignedURL, http.StatusFound) | ||
} | ||
|
||
func main() { | ||
httpserver := &http.Server{ | ||
Addr: ":8080", | ||
getConfig() | ||
httpServer := &http.Server{ | ||
Addr: ":" + viper.GetString("http_port"), | ||
ReadTimeout: 10 * time.Second, | ||
WriteTimeout: 10 * time.Second, | ||
MaxHeaderBytes: 1 << 20, | ||
} | ||
http.HandleFunc("/", handler) | ||
log.Fatal(httpserver.ListenAndServe()) | ||
http.HandleFunc("/", httphandler) | ||
log.Fatal(httpServer.ListenAndServe()) | ||
} |