Sessions in etcd KV store instead of cookies
This commit is contained in:
@@ -4,8 +4,8 @@ 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"
|
||||
cache "git.coopgo.io/coopgo-apps/parcoursmob/utils/storage"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -13,10 +13,10 @@ type APIHandler struct {
|
||||
idp *identification.IdentificationProvider
|
||||
config *viper.Viper
|
||||
services *services.ServicesHandler
|
||||
cache *cache.CacheHandler
|
||||
cache cache.CacheHandler
|
||||
}
|
||||
|
||||
func NewAPIHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache *cache.CacheHandler) (*APIHandler, error) {
|
||||
func NewAPIHandler(cfg *viper.Viper, idp *identification.IdentificationProvider, svc *services.ServicesHandler, cache cache.CacheHandler) (*APIHandler, error) {
|
||||
return &APIHandler{
|
||||
idp: idp,
|
||||
config: cfg,
|
||||
|
||||
101
handlers/api/export.go
Normal file
101
handlers/api/export.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type FlatMaps []map[string]any
|
||||
|
||||
func (maps FlatMaps) GetHeaders() (res []string) {
|
||||
keys := map[string]bool{}
|
||||
for _, m := range maps {
|
||||
for k, _ := range m {
|
||||
if _, ok := keys[k]; !ok {
|
||||
keys[k] = true
|
||||
res = append(res, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
sort.Strings(res)
|
||||
return
|
||||
}
|
||||
|
||||
func (maps FlatMaps) GetValues() (res [][]string) {
|
||||
headers := maps.GetHeaders()
|
||||
for _, m := range maps {
|
||||
line := []string{}
|
||||
for _, k := range headers {
|
||||
if v, ok := m[k]; ok && v != nil {
|
||||
line = append(line, fmt.Sprint(v))
|
||||
} else {
|
||||
line = append(line, "")
|
||||
}
|
||||
}
|
||||
res = append(res, line)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (h APIHandler) CacheExport(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
|
||||
}
|
||||
|
||||
if data, ok := d.([]any); ok {
|
||||
|
||||
flatmaps := FlatMaps{}
|
||||
//fmt.Println(data)
|
||||
|
||||
for _, v := range data {
|
||||
fmt.Println(v)
|
||||
fm := map[string]any{}
|
||||
flatten("", v.(map[string]any), fm)
|
||||
fmt.Println(fm)
|
||||
flatmaps = append(flatmaps, fm)
|
||||
}
|
||||
|
||||
fmt.Println(flatmaps)
|
||||
|
||||
w.Header().Set("Content-Type", "text/csv")
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=export-%s.csv", cacheid))
|
||||
c := csv.NewWriter(w)
|
||||
c.Write(flatmaps.GetHeaders())
|
||||
c.WriteAll(flatmaps.GetValues())
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
|
||||
}
|
||||
|
||||
func flatten(prefix string, src map[string]any, dest map[string]any) {
|
||||
if len(prefix) > 0 {
|
||||
prefix += "."
|
||||
}
|
||||
for k, v := range src {
|
||||
switch child := v.(type) {
|
||||
case map[string]any:
|
||||
flatten(prefix+k, child, dest)
|
||||
case []any:
|
||||
for i := 0; i < len(child); i++ {
|
||||
dest[prefix+k+"."+strconv.Itoa(i)] = child[i]
|
||||
}
|
||||
default:
|
||||
fmt.Println(prefix+k, " : ", v)
|
||||
dest[prefix+k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,7 @@ func (h APIHandler) OAuth2Callback(w http.ResponseWriter, r *http.Request) {
|
||||
// Extract the ID Token from OAuth2 token.
|
||||
rawIDToken, ok := oauth2Token.Extra("id_token").(string)
|
||||
if !ok {
|
||||
fmt.Println("issue retrieving token")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
@@ -36,11 +37,20 @@ func (h APIHandler) OAuth2Callback(w http.ResponseWriter, r *http.Request) {
|
||||
redirect := "/app/"
|
||||
|
||||
if session.Values["redirect"] != nil && session.Values["redirect"] != "" {
|
||||
fmt.Println("no redirect stuff")
|
||||
redirect = session.Values["redirect"].(string)
|
||||
delete(session.Values, "redirect")
|
||||
}
|
||||
|
||||
session.Save(r, w)
|
||||
if err = session.Save(r, w); err != nil {
|
||||
fmt.Println(err)
|
||||
panic(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
fmt.Println(session.Values)
|
||||
fmt.Println("redirect")
|
||||
|
||||
http.Redirect(w, r, redirect, http.StatusFound)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user