Big refactoring of PARCOURSMOB - Initial commit

This commit is contained in:
2022-08-11 17:26:55 +02:00
commit 7225d027c9
65 changed files with 10276 additions and 0 deletions

30
handlers/api/api.go Normal file
View File

@@ -0,0 +1,30 @@
package api
import (
"net/http"
"git.coopgo.io/coopgo-apps/parcoursmob/services"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/cache"
"git.coopgo.io/coopgo-apps/parcoursmob/utils/identification"
"github.com/spf13/viper"
)
type APIHandler struct {
idp *identification.IdentificationProvider
config *viper.Viper
services *services.ServicesHandler
cache *cache.CacheHandler
}
func NewAPIHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache *cache.CacheHandler) (*APIHandler, error) {
return &APIHandler{
idp: idp,
config: cfg,
services: svc,
cache: cache,
}, nil
}
func (h *APIHandler) NotFound(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
}

51
handlers/api/cache.go Normal file
View File

@@ -0,0 +1,51 @@
package api
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
func (h APIHandler) GetCache(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
cacheid := vars["cacheid"]
d, err := h.cache.Get(cacheid)
if err != nil {
fmt.Println(err)
w.WriteHeader(http.StatusNotFound)
return
}
result := d
if data, ok := d.([]any); ok {
if limitsmin, ok := r.URL.Query()["limits.min"]; ok {
min, _ := strconv.Atoi(limitsmin[0])
if limitsmax, ok := r.URL.Query()["limits.max"]; ok {
max, _ := strconv.Atoi(limitsmax[0])
if max > len(data) {
result = data[min:]
} else {
result = data[min:max]
}
} else {
result = data[min:]
}
}
}
j, err := json.Marshal(result)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}

54
handlers/api/geo.go Normal file
View File

@@ -0,0 +1,54 @@
package api
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func (h *APIHandler) GeoAutocomplete(w http.ResponseWriter, r *http.Request) {
pelias := h.config.GetString("geo.pelias.url")
t, ok := r.URL.Query()["text"]
if !ok || len(t[0]) < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
text := t[0]
resp, err := http.Get(fmt.Sprintf("%s/autocomplete?text=%s", pelias, text))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var response map[string]any
jsonErr := json.Unmarshal(body, &response)
if jsonErr != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
j, err := json.Marshal(response["features"])
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
w.Write(j)
}

46
handlers/api/oidc.go Normal file
View File

@@ -0,0 +1,46 @@
package api
import (
"context"
"fmt"
"net/http"
)
func (h APIHandler) OAuth2Callback(w http.ResponseWriter, r *http.Request) {
oauth2Token, err := h.idp.OAuth2Config.Exchange(context.Background(), r.URL.Query().Get("code"))
if err != nil {
fmt.Println("Exchange error")
fmt.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
// Extract the ID Token from OAuth2 token.
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
_, err = h.idp.TokenVerifier.Verify(context.Background(), rawIDToken)
if err != nil {
fmt.Println("not able to verify token")
fmt.Println(err)
w.WriteHeader(http.StatusUnauthorized)
return
}
session, _ := h.idp.SessionsStore.Get(r, "parcoursmob_session")
session.Values["idtoken"] = rawIDToken
redirect := "/app/"
if session.Values["redirect"] != nil && session.Values["redirect"] != "" {
redirect = session.Values["redirect"].(string)
delete(session.Values, "redirect")
}
session.Save(r, w)
http.Redirect(w, r, redirect, http.StatusFound)
}