Adding legacy API paths

This commit is contained in:
Caleb Gardner
2024-08-06 22:36:58 -05:00
parent bb44e26dd4
commit a9ca12395e
4 changed files with 23 additions and 5 deletions
@@ -36,8 +36,13 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
var req countLogReq
err = json.NewDecoder(r.Body).Decode(&req)
if err != nil || req.Platform == "" {
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request")
return
if r.URL.Query().Get("platform") != "" {
//TODO: remove legacy code
req.Platform = r.URL.Query().Get("platform")
} else {
ReturnError(w, http.StatusBadRequest, "invalidBody", "Bad request")
return
}
}
ap := b.GetApp(hdr.Key)
count := ap.CountTable()
@@ -72,8 +77,8 @@ func (b *Backend) countLog(w http.ResponseWriter, r *http.Request) {
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return
}
json.NewEncoder(w).Encode(map[string]string{"id": req.ID})
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"id": req.ID})
}
func addToCountTable(w http.ResponseWriter, c CountTable, platform string, curDate int) error {
@@ -93,8 +98,8 @@ func addToCountTable(w http.ResponseWriter, c CountTable, platform string, curDa
ReturnError(w, http.StatusInternalServerError, "internal", "Server error")
return err
}
json.NewEncoder(w).Encode(map[string]string{"id": id.String()})
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(map[string]string{"id": id.String()})
return nil
}
+3
View File
@@ -56,6 +56,9 @@ func NewBackend(keyTable Table[ApiKey], apps ...App) (*Backend, error) {
if hasLog {
b.m.HandleFunc("POST /count", b.countLog)
b.m.HandleFunc("GET /count", b.getCount)
//TODO: Remove legacy paths
b.m.HandleFunc("POST /log", b.countLog)
}
if hasCrash {
b.m.HandleFunc("POST /crash", b.reportCrash)
+10
View File
@@ -2,6 +2,7 @@ package backend
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
@@ -36,6 +37,12 @@ type ParsedHeader struct {
func (b *Backend) ParseHeader(r *http.Request) (*ParsedHeader, error) {
out := &ParsedHeader{}
key := r.Header.Get("X-API-Key")
//TODO: Remove legacy code
if key == "" {
key = r.URL.Query().Get("key")
}
token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
if key != "" {
apiKey, err := b.keyTable.Get(key)
@@ -97,6 +104,7 @@ func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm s
hdr, err := b.ParseHeader(r)
if hdr == nil || hdr.Key == nil {
if err == ErrApiKeyUnauthorized {
fmt.Println("yo1")
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, nil
}
@@ -111,11 +119,13 @@ func (b *Backend) VerifyHeader(w http.ResponseWriter, r *http.Request, keyPerm s
if allowManagementKey {
return hdr, nil
} else {
fmt.Println("yo2")
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, nil
}
}
if _, ok := b.apps[hdr.Key.AppID]; !ok {
fmt.Println("yo3")
ReturnError(w, http.StatusUnauthorized, "invalidKey", "Application not authorized")
return nil, errors.New("server misconfigured, appID present in DB, but App not added to backend")
}
+1 -1
View File
@@ -80,7 +80,7 @@ func setupBackend(mux *http.ServeMux) {
swApp,
cdrApp,
)
back.AddCorsAddress("darkstorm.tech")
back.AddCorsAddress("https://darkstorm.tech")
if err != nil {
log.Fatal("error setting up backend:", err)
}