diff options
| author | diogo464 <[email protected]> | 2024-10-01 16:04:03 +0100 |
|---|---|---|
| committer | diogo464 <[email protected]> | 2024-10-01 16:04:03 +0100 |
| commit | ece33434ee227d864ac4b2aeecce3b8f46085567 (patch) | |
| tree | ad8b77255999779b431f657f2f21ebb8745c7465 | |
| parent | 1422e5893b7e8563431507a2058967082ec4f4cb (diff) | |
added build files
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Containerfile | 4 | ||||
| -rwxr-xr-x | build.sh | 9 | ||||
| -rw-r--r-- | main.go | 13 |
4 files changed, 25 insertions, 2 deletions
| @@ -1 +1,2 @@ | |||
| 1 | *.db | 1 | *.db |
| 2 | belverde-fire | ||
diff --git a/Containerfile b/Containerfile new file mode 100644 index 0000000..2554194 --- /dev/null +++ b/Containerfile | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | FROM alpine:latest | ||
| 2 | WORKDIR /app | ||
| 3 | COPY belverde-fire . | ||
| 4 | ENTRYPOINT ["/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 | |||
| 3 | CGO_ENABLED=0 go build . || exit 1 | ||
| 4 | docker build -t git.d464.sh/diogo464/belverde-fire -f Containerfile . || exit 1 | ||
| 5 | |||
| 6 | if [ "$PUSH" = "1" ]; then | ||
| 7 | docker push git.d464.sh/diogo464/belverde-fire | ||
| 8 | fi | ||
| 9 | |||
| @@ -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) |
