package server

import (
	"local/oauth2/oauth2server/config"
	"local/router"
	"local/storage"
)

var wildcard = router.Wildcard

const (
	USERS  = "users"
	ACCESS = "access"
	TOKEN  = "token"
	SALT   = "salt"
)

type Server struct {
	*router.Router
	store storage.DB
}

func New() *Server {
	store, err := storage.New(storage.TypeFromString(config.Store), config.StoreAddr, config.StoreUser, config.StorePass)
	if err != nil {
		panic(err)
	}
	purgeIssuedCredentials(store)
	return &Server{
		Router: router.New(),
		store:  store,
	}
}

func purgeIssuedCredentials(store storage.DB) {
	accesses, _ := store.List([]string{ACCESS})
	for _, access := range accesses {
		store.Set(access, nil, ACCESS)
	}
	tokens, _ := store.List([]string{TOKEN})
	for _, token := range tokens {
		store.Set(token, nil, TOKEN)
	}
}
