Use config values instead of hardcoded values

This commit is contained in:
Bilal
2020-05-08 10:42:11 +02:00
parent 9a2425d245
commit aa982dafb8

View File

@@ -1,6 +1,7 @@
package webserver package webserver
import ( import (
c "gitlab.com/saburly/kiviscraplib/config"
"gitlab.com/saburly/kiviscraplib/structures" "gitlab.com/saburly/kiviscraplib/structures"
"gitlab.com/saburly/kiviscraplib/utils" "gitlab.com/saburly/kiviscraplib/utils"
"log" "log"
@@ -13,7 +14,7 @@ var requests chan structures.Request
func ServeHTTP(queue chan structures.Request, end chan<- string) { func ServeHTTP(queue chan structures.Request, end chan<- string) {
requests = queue requests = queue
http.HandleFunc("/", httpHandler) http.HandleFunc("/", httpHandler)
err := http.ListenAndServe("127.0.0.1:1337", nil) err := http.ListenAndServe(c.WebServerConfig.Address, nil)
if err != nil { if err != nil {
end <- "http server" end <- "http server"
log.Fatal(err) log.Fatal(err)
@@ -21,7 +22,6 @@ func ServeHTTP(queue chan structures.Request, end chan<- string) {
} }
func httpHandler(w http.ResponseWriter, r *http.Request) { func httpHandler(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
api_key, present := query["api_key"] api_key, present := query["api_key"]
if !present || len(api_key) == 0 { if !present || len(api_key) == 0 {
@@ -30,7 +30,7 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
} }
// todo: do more for the authentication / authorization // todo: do more for the authentication / authorization
if api_key[0] != "b8be5a3b639d465039e8fbe7582270a7" { if api_key[0] != c.WebServerConfig.APIKey {
respondWithError(w, 401, "api_key is wrong") respondWithError(w, 401, "api_key is wrong")
return return
} }
@@ -56,10 +56,9 @@ func httpHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("request from %s: %s %q", r.RemoteAddr, r.Method, r.URL) log.Printf("request from %s: %s %q", r.RemoteAddr, r.Method, r.URL)
log.Printf("waiting for response") log.Printf("waiting for response")
// todo: put timeout in ENV variable
timeout := make(chan bool, 1) timeout := make(chan bool, 1)
go func() { go func() {
time.Sleep(100 * time.Second) time.Sleep(time.Duration(c.WebServerConfig.Timeout) * time.Second)
timeout <- true timeout <- true
}() }()