mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-10-29 14:42:24 -05:00
Pg to bun (#148)
* start moving to bun * changing more stuff * more * and yet more * tests passing * seems stable now * more big changes * small fix * little fixes
This commit is contained in:
parent
071eca20ce
commit
2dc9fc1626
713 changed files with 98694 additions and 22704 deletions
|
|
@ -19,7 +19,7 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
|
@ -29,8 +29,6 @@ import (
|
|||
"github.com/gin-gonic/gin"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/config"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/db"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
||||
"github.com/superseriousbusiness/gotosocial/internal/id"
|
||||
)
|
||||
|
||||
// SessionOptions returns the standard set of options to use for each session.
|
||||
|
|
@ -45,34 +43,23 @@ func SessionOptions(cfg *config.Config) sessions.Options {
|
|||
}
|
||||
}
|
||||
|
||||
func useSession(cfg *config.Config, dbService db.DB, engine *gin.Engine) error {
|
||||
func useSession(ctx context.Context, cfg *config.Config, sessionDB db.Session, engine *gin.Engine) error {
|
||||
// check if we have a saved router session already
|
||||
routerSessions := []*gtsmodel.RouterSession{}
|
||||
if err := dbService.GetAll(&routerSessions); err != nil {
|
||||
rs, err := sessionDB.GetSession(ctx)
|
||||
if err != nil {
|
||||
if err != db.ErrNoEntries {
|
||||
// proper error occurred
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
var rs *gtsmodel.RouterSession
|
||||
if len(routerSessions) == 1 {
|
||||
// we have a router session stored
|
||||
rs = routerSessions[0]
|
||||
} else if len(routerSessions) == 0 {
|
||||
// we have no router sessions so we need to create a new one
|
||||
var err error
|
||||
rs, err = routerSession(dbService)
|
||||
// no session saved so create a new one
|
||||
rs, err = sessionDB.CreateSession(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new router session: %s", err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// we should only have one router session stored ever
|
||||
return errors.New("we had more than one router session in the db")
|
||||
}
|
||||
|
||||
if rs == nil {
|
||||
return errors.New("error getting or creating router session: router session was nil")
|
||||
return errors.New("router session was nil")
|
||||
}
|
||||
|
||||
store := memstore.NewStore(rs.Auth, rs.Crypt)
|
||||
|
|
@ -81,34 +68,3 @@ func useSession(cfg *config.Config, dbService db.DB, engine *gin.Engine) error {
|
|||
engine.Use(sessions.Sessions(sessionName, store))
|
||||
return nil
|
||||
}
|
||||
|
||||
// routerSession generates a new router session with random auth and crypt bytes,
|
||||
// puts it in the database for persistence, and returns it for use.
|
||||
func routerSession(dbService db.DB) (*gtsmodel.RouterSession, error) {
|
||||
auth := make([]byte, 32)
|
||||
crypt := make([]byte, 32)
|
||||
|
||||
if _, err := rand.Read(auth); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := rand.Read(crypt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rid, err := id.NewULID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
rs := >smodel.RouterSession{
|
||||
ID: rid,
|
||||
Auth: auth,
|
||||
Crypt: crypt,
|
||||
}
|
||||
|
||||
if err := dbService.Put(rs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rs, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue