aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Containerfile4
-rwxr-xr-xbuild.sh9
-rw-r--r--main.go13
4 files changed, 25 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index 98e6ef6..2aa56f0 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
1*.db 1*.db
2belverde-fire
diff --git a/Containerfile b/Containerfile
new file mode 100644
index 0000000..2554194
--- /dev/null
+++ b/Containerfile
@@ -0,0 +1,4 @@
1FROM alpine:latest
2WORKDIR /app
3COPY belverde-fire .
4ENTRYPOINT ["/app/belverde-fire"]
diff --git a/build.sh b/build.sh
new file mode 100755
index 0000000..ffc3cde
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,9 @@
1#!/usr/bin/env sh
2
3CGO_ENABLED=0 go build . || exit 1
4docker build -t git.d464.sh/diogo464/belverde-fire -f Containerfile . || exit 1
5
6if [ "$PUSH" = "1" ]; then
7 docker push git.d464.sh/diogo464/belverde-fire
8fi
9
diff --git a/main.go b/main.go
index f30496f..2adaa21 100644
--- a/main.go
+++ b/main.go
@@ -19,6 +19,11 @@ import (
19 "github.com/pkg/errors" 19 "github.com/pkg/errors"
20) 20)
21 21
22const (
23 ENV_VAR_HTTP_USERNAME = "HTTP_USERNAME"
24 ENV_VAR_HTTP_PASSWORD = "HTTP_PASSWORD"
25)
26
22type LocationLog struct { 27type 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) {
220func requireBasicAuth(h http.HandlerFunc) http.HandlerFunc { 225func 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)