Optimize session retrieval and fix Docker timezone issue

This commit is contained in:
2024-05-02 21:02:50 +07:00
parent 4715814551
commit bb2540a5c4
4 changed files with 41 additions and 26 deletions

View File

@ -19,7 +19,7 @@ COPY --from=node_builder /src/public /src/public
COPY --from=node_builder /src/public/upload_obfuscated.js /src/public/upload.js COPY --from=node_builder /src/public/upload_obfuscated.js /src/public/upload.js
COPY --from=node_builder /src/public/validatePassword_obfuscated.js /src/public/validatePassword.js COPY --from=node_builder /src/public/validatePassword_obfuscated.js /src/public/validatePassword.js
RUN apk update && apk upgrade && apk add --no-cache ca-certificates RUN apk update && apk upgrade && apk add --no-cache ca-certificates tzdata
RUN update-ca-certificates RUN update-ca-certificates
RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ) RUN go install github.com/a-h/templ/cmd/templ@$(go list -m -f '{{ .Version }}' github.com/a-h/templ)
RUN templ generate RUN templ generate
@ -30,9 +30,12 @@ FROM scratch
WORKDIR /src WORKDIR /src
COPY --from=go_builder /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=go_builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ COPY --from=go_builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=go_builder /src/schema.sql /src COPY --from=go_builder /src/schema.sql /src
COPY --from=go_builder /src/public /src/public COPY --from=go_builder /src/public /src/public
COPY --from=go_builder /src/tmp/main /src COPY --from=go_builder /src/tmp/main /src
ENV TZ Asia/Jakarta
ENTRYPOINT ["./main"] ENTRYPOINT ["./main"]

View File

@ -17,7 +17,7 @@ func init() {
func GET(w http.ResponseWriter, r *http.Request) { func GET(w http.ResponseWriter, r *http.Request) {
userSession := r.Context().Value("user").(types.User) userSession := r.Context().Value("user").(types.User)
component := userView.Main("User Page", userSession.Email, userSession.Username, session.UserSessionInfoList[userSession.Email]) component := userView.Main("User Page", userSession.Email, userSession.Username, session.GetSessions(userSession.Email))
err := component.Render(r.Context(), w) err := component.Render(r.Context(), w)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError) http.Error(w, err.Error(), http.StatusInternalServerError)

View File

@ -59,10 +59,13 @@ func Handler(next http.Handler) http.Handler {
} }
func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) { func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
status, user := session.GetSession(r) status, user, sessionID := session.GetSession(r)
switch status { switch status {
case session.Authorized: case session.Authorized:
userSession := session.GetSessionInfo(user.Email, sessionID)
userSession.UpdateAccessTime()
ctx := context.WithValue(r.Context(), "user", user) ctx := context.WithValue(r.Context(), "user", user)
req := r.WithContext(ctx) req := r.WithContext(ctx)
r.Context().Value("user") r.Context().Value("user")
@ -94,7 +97,7 @@ func Auth(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
} }
func Guest(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) { func Guest(next http.HandlerFunc, w http.ResponseWriter, r *http.Request) {
status, _ := session.GetSession(r) status, _, _ := session.GetSession(r)
switch status { switch status {
case session.Authorized: case session.Authorized:

View File

@ -40,10 +40,8 @@ const (
InvalidSession UserStatus = "invalid_session" InvalidSession UserStatus = "invalid_session"
) )
type SessionInfoList map[string][]*SessionInfo
var GlobalSessionStore = SessionStore{Sessions: make(map[string]*Session)} var GlobalSessionStore = SessionStore{Sessions: make(map[string]*Session)}
var UserSessionInfoList = make(SessionInfoList) var UserSessionInfoList = make(map[string]map[string]*SessionInfo)
type SessionNotFoundError struct{} type SessionNotFoundError struct{}
@ -95,22 +93,22 @@ func (s *Session) Destroy(w http.ResponseWriter) {
} }
func AddSessionInfo(email string, sessionInfo *SessionInfo) { func AddSessionInfo(email string, sessionInfo *SessionInfo) {
UserSessionInfoList[email] = append(UserSessionInfoList[email], sessionInfo) if _, ok := UserSessionInfoList[email]; !ok {
UserSessionInfoList[email] = make(map[string]*SessionInfo)
}
UserSessionInfoList[email][sessionInfo.SessionID] = sessionInfo
} }
func RemoveSessionInfo(email string, id string) { func RemoveSessionInfo(email string, id string) {
sessionInfos := UserSessionInfoList[email] if userSessions, ok := UserSessionInfoList[email]; ok {
var updatedSessionInfos []*SessionInfo if _, ok := userSessions[id]; ok {
for _, sessionInfo := range sessionInfos { delete(userSessions, id)
if sessionInfo.SessionID != id { if len(userSessions) == 0 {
updatedSessionInfos = append(updatedSessionInfos, sessionInfo)
}
}
if len(updatedSessionInfos) > 0 {
UserSessionInfoList[email] = updatedSessionInfos
return
}
delete(UserSessionInfoList, email) delete(UserSessionInfoList, email)
}
}
}
} }
func RemoveAllSessions(email string) { func RemoveAllSessions(email string) {
@ -122,8 +120,8 @@ func RemoveAllSessions(email string) {
} }
func GetSessionInfo(email string, id string) *SessionInfo { func GetSessionInfo(email string, id string) *SessionInfo {
for _, sessionInfo := range UserSessionInfoList[email] { if userSession, ok := UserSessionInfoList[email]; ok {
if sessionInfo.SessionID == id { if sessionInfo, ok := userSession[id]; ok {
return sessionInfo return sessionInfo
} }
} }
@ -136,26 +134,37 @@ func (sessionInfo *SessionInfo) UpdateAccessTime() {
sessionInfo.AccessAt = formattedTime sessionInfo.AccessAt = formattedTime
} }
func GetSession(r *http.Request) (UserStatus, types.User) { func GetSession(r *http.Request) (UserStatus, types.User, string) {
cookie, err := r.Cookie("Session") cookie, err := r.Cookie("Session")
if err != nil { if err != nil {
return Unauthorized, types.User{} return Unauthorized, types.User{}, ""
} }
storeSession, err := GlobalSessionStore.Get(cookie.Value) storeSession, err := GlobalSessionStore.Get(cookie.Value)
if err != nil { if err != nil {
if errors.Is(err, &SessionNotFoundError{}) { if errors.Is(err, &SessionNotFoundError{}) {
return InvalidSession, types.User{} return InvalidSession, types.User{}, ""
} }
return Unauthorized, types.User{} return Unauthorized, types.User{}, ""
} }
val := storeSession.Values["user"] val := storeSession.Values["user"]
var userSession = types.User{} var userSession = types.User{}
userSession, ok := val.(types.User) userSession, ok := val.(types.User)
if !ok { if !ok {
return Unauthorized, types.User{} return Unauthorized, types.User{}, ""
} }
return Authorized, userSession return Authorized, userSession, cookie.Value
}
func GetSessions(email string) []*SessionInfo {
if sessions, ok := UserSessionInfoList[email]; ok {
result := make([]*SessionInfo, 0, len(sessions))
for _, sessionInfo := range sessions {
result = append(result, sessionInfo)
}
return result
}
return nil
} }