diff options
Diffstat (limited to 'main.go')
| -rw-r--r-- | main.go | 13 |
1 files changed, 11 insertions, 2 deletions
| @@ -19,6 +19,11 @@ import ( | |||
| 19 | "github.com/pkg/errors" | 19 | "github.com/pkg/errors" |
| 20 | ) | 20 | ) |
| 21 | 21 | ||
| 22 | const ( | ||
| 23 | ENV_VAR_HTTP_USERNAME = "HTTP_USERNAME" | ||
| 24 | ENV_VAR_HTTP_PASSWORD = "HTTP_PASSWORD" | ||
| 25 | ) | ||
| 26 | |||
| 22 | type LocationLog struct { | 27 | type LocationLog struct { |
| 23 | Timestamp float64 `json:"timestamp"` | 28 | Timestamp float64 `json:"timestamp"` |
| 24 | Latitude float64 `json:"latitude"` | 29 | Latitude float64 `json:"latitude"` |
| @@ -220,8 +225,8 @@ func handlePostApiShapes(w http.ResponseWriter, r *http.Request) { | |||
| 220 | func requireBasicAuth(h http.HandlerFunc) http.HandlerFunc { | 225 | func requireBasicAuth(h http.HandlerFunc) http.HandlerFunc { |
| 221 | return func(w http.ResponseWriter, r *http.Request) { | 226 | return func(w http.ResponseWriter, r *http.Request) { |
| 222 | username, password, ok := r.BasicAuth() | 227 | username, password, ok := r.BasicAuth() |
| 223 | required_username := os.Getenv("HTTP_USERNAME") | 228 | required_username := os.Getenv(ENV_VAR_HTTP_USERNAME) |
| 224 | required_password := os.Getenv("HTTP_PASSWORD") | 229 | required_password := os.Getenv(ENV_VAR_HTTP_PASSWORD) |
| 225 | if !ok || username != required_username || password != required_password { | 230 | if !ok || username != required_username || password != required_password { |
| 226 | w.Header().Add("WWW-Authenticate", "Basic realm=\"User Visible Realm\"") | 231 | w.Header().Add("WWW-Authenticate", "Basic realm=\"User Visible Realm\"") |
| 227 | http.Error(w, "invalid authentication", http.StatusUnauthorized) | 232 | http.Error(w, "invalid authentication", http.StatusUnauthorized) |
| @@ -255,6 +260,10 @@ func main() { | |||
| 255 | log.Fatal(err) | 260 | log.Fatal(err) |
| 256 | } | 261 | } |
| 257 | 262 | ||
| 263 | if os.Getenv(ENV_VAR_HTTP_USERNAME) == "" || os.Getenv(ENV_VAR_HTTP_PASSWORD) == "" { | ||
| 264 | log.Fatalf("http username and password cannot be empty") | ||
| 265 | } | ||
| 266 | |||
| 258 | http.HandleFunc("GET /api/location", handleGetApiLog) | 267 | http.HandleFunc("GET /api/location", handleGetApiLog) |
| 259 | http.HandleFunc("POST /api/location", requireBasicAuth(handlePostApiLog)) | 268 | http.HandleFunc("POST /api/location", requireBasicAuth(handlePostApiLog)) |
| 260 | http.HandleFunc("GET /api/shapes", handleGetApiShapes) | 269 | http.HandleFunc("GET /api/shapes", handleGetApiShapes) |
