alexedwards / scs

HTTP Session Management for Go
MIT License
2.02k stars 165 forks source link

Fails using ListenAndServeTLS #169

Closed BigBoulard closed 1 year ago

BigBoulard commented 1 year ago

Hi Alex,

I get a nil pointer exception while using ListenAndServeTLS. Any idea?

package main

import (
    "crypto/tls"
    "net/http"
    "os"
    "time"

    "log"

    "github.com/alexedwards/scs/v2"
    "github.com/go-chi/chi/v5"
    "github.com/go-chi/chi/v5/middleware"
)

const FrontRedirectUrl = "http://localhost:5173/success"

var sessionManager *scs.SessionManager

func main() {
    // HTTP router
    r := chi.NewRouter()
    r.Use(middleware.RequestID)
    r.Use(middleware.Logger)
    r.Use(middleware.Recoverer)
    r.Use(middleware.RealIP)
    r.Use(middleware.Timeout(60 * time.Second))

    r.Get("/", func(w http.ResponseWriter, req *http.Request) {
        w.Write([]byte("Hello server"))
    })

    // generate a cert struct
    cwd, err := os.Getwd()
    if err != nil {
        log.Fatal("can't load current working directory")
    }
    cert, err := tls.LoadX509KeyPair(cwd+"/../cert/cert.pem", cwd+"/../cert/key.pem")
    if err != nil {
        log.Fatalf("can't load cert files %s or %s ", cwd+"/../cert/cert.pem", cwd+"/../cert/key.pem")
    }

    // create a custom server to use the cert struct
    server := &http.Server{
        Addr:    "localhost:3000",
        Handler: sessionManager.LoadAndSave(r), // HERE >>>>>>>>>>> works if I specify "r" instead
        TLSConfig: &tls.Config{
            Certificates: []tls.Certificate{cert},
        },
    }
    log.Fatal(server.ListenAndServeTLS("", ""))
}
goroutine 33 [running]:
net/http.(*http2serverConn).runHandler.func1()
        /usr/local/go/src/net/http/h2_bundle.go:6042 +0x145
panic({0x12fd720, 0x1596970})
        /usr/local/go/src/runtime/panic.go:884 +0x213
github.com/alexedwards/scs/v2.(*SessionManager).LoadAndSave.func1({0x13d07d0, 0xc000254000}, 0xc00024e100)
        /Users/me/go/pkg/mod/github.com/alexedwards/scs/v2@v2.5.1/session.go:135 +0x59
net/http.HandlerFunc.ServeHTTP(0x0?, {0x13d07d0?, 0xc000254000?}, 0x0?)
        /usr/local/go/src/net/http/server.go:2122 +0x2f
net/http.serverHandler.ServeHTTP({0x0?}, {0x13d07d0, 0xc000254000}, 0xc00024e100)
        /usr/local/go/src/net/http/server.go:2936 +0x316
net/http.initALPNRequest.ServeHTTP({{0x13d0b48?, 0xc000066090?}, 0xc000016380?, {0xc000144000?}}, {0x13d07d0, 0xc000254000}, 0xc00024e100)
        /usr/local/go/src/net/http/server.go:3545 +0x245
net/http.(*http2serverConn).runHandler(0x0?, 0x0?, 0x0?, 0x0?)
        /usr/local/go/src/net/http/h2_bundle.go:6049 +0x83
created by net/http.(*http2serverConn).processHeaders
        /usr/local/go/src/net/http/h2_bundle.go:5762 +0x68a
2023/06/22 23:04:58 http2: panic serving 127.0.0.1:62199: runtime error: invalid memory address or nil pointer dereference
// LoadAndSave provides middleware which automatically loads and saves session
// data for the current request, and communicates the session token to and from
// the client in a cookie.
func (s *SessionManager) LoadAndSave(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        var token string
        cookie, err := r.Cookie(s.Cookie.Name) <------------------ Happening here
alexedwards commented 1 year ago

You need to set the sessionManager global to an actual initialized instance of scs.SessionManager (it's currently nil). Please see the basic use example in the README: https://github.com/alexedwards/scs#basic-use