slowly but surely fixing routing and layout handling
Signed-off-by: ari melody <ari@arimelody.me>
This commit is contained in:
parent
4b58a27fdc
commit
a26bdfa646
5 changed files with 80 additions and 170 deletions
|
@ -2,27 +2,22 @@ package admin
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"arimelody.me/arimelody.me/discord"
|
||||
"arimelody.me/arimelody.me/global"
|
||||
)
|
||||
|
||||
type (
|
||||
Session struct {
|
||||
UserID string
|
||||
Token string
|
||||
}
|
||||
|
||||
loginData struct {
|
||||
UserID string
|
||||
Password string
|
||||
Expires int64
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -45,16 +40,23 @@ func CreateSession(UserID string) Session {
|
|||
return Session{
|
||||
UserID: UserID,
|
||||
Token: string(generateToken()),
|
||||
Expires: time.Now().Add(24 * time.Hour).Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func Handler() http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(r.URL.Path)
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(200)
|
||||
w.Write([]byte("hello admin!"))
|
||||
})
|
||||
w.Write([]byte("hello /admin!"))
|
||||
}))
|
||||
mux.Handle("/callback", global.HTTPLog(OAuthCallbackHandler()))
|
||||
mux.Handle("/login", global.HTTPLog(LoginHandler()))
|
||||
mux.Handle("/verify", global.HTTPLog(AuthorisedHandler(VerifyHandler())))
|
||||
mux.Handle("/logout", global.HTTPLog(AuthorisedHandler(LogoutHandler())))
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
func AuthorisedHandler(next http.Handler) http.Handler {
|
||||
|
@ -73,6 +75,17 @@ func AuthorisedHandler(next http.Handler) http.Handler {
|
|||
|
||||
var session *Session
|
||||
for _, s := range sessions {
|
||||
if s.Expires < time.Now().Unix() {
|
||||
// expired session. remove it from the list!
|
||||
new_sessions := []*Session{}
|
||||
for _, ns := range sessions {
|
||||
if ns.Token == s.Token {
|
||||
continue
|
||||
}
|
||||
new_sessions = append(new_sessions, ns)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if s.Token == auth {
|
||||
session = s
|
||||
break
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue