mirror of
https://github.com/m1k1o/neko.git
synced 2025-07-31 15:29:41 +02:00
WIP legacy adapter.
This commit is contained in:
parent
1fd4e53637
commit
1c4fc4653b
4 changed files with 823 additions and 205 deletions
96
server/internal/http/legacy/session.go
Normal file
96
server/internal/http/legacy/session.go
Normal file
|
@ -0,0 +1,96 @@
|
|||
package legacy
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/demodesk/neko/internal/api"
|
||||
"github.com/demodesk/neko/pkg/types"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
type session struct {
|
||||
url string
|
||||
id string
|
||||
token string
|
||||
profile types.MemberProfile
|
||||
client *http.Client
|
||||
|
||||
connClient *websocket.Conn
|
||||
connBackend *websocket.Conn
|
||||
}
|
||||
|
||||
func newSession(url string) *session {
|
||||
return &session{
|
||||
url: url,
|
||||
client: http.DefaultClient,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *session) apiReq(method, path string, request, response any) error {
|
||||
body, err := json.Marshal(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req, err := http.NewRequest(method, s.url+path, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
|
||||
if s.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+s.token)
|
||||
}
|
||||
|
||||
res, err := s.client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
|
||||
if res.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(res.Body)
|
||||
return fmt.Errorf("unexpected status code: %d, body: %s", res.StatusCode, body)
|
||||
}
|
||||
|
||||
return json.NewDecoder(res.Body).Decode(response)
|
||||
}
|
||||
|
||||
func (s *session) create(password string) (string, error) {
|
||||
data := api.SessionDataPayload{}
|
||||
|
||||
// pefrom login with arbitrary username that will be changed later
|
||||
err := s.apiReq(http.MethodPost, "/api/login", api.SessionLoginPayload{
|
||||
Username: "admin",
|
||||
Password: password,
|
||||
}, &data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
s.id = data.ID
|
||||
s.token = data.Token
|
||||
s.profile = data.Profile
|
||||
|
||||
if s.token == "" {
|
||||
return "", fmt.Errorf("token not found")
|
||||
}
|
||||
|
||||
return data.Token, nil
|
||||
}
|
||||
|
||||
func (s *session) destroy() {
|
||||
defer s.client.CloseIdleConnections()
|
||||
|
||||
// logout session
|
||||
err := s.apiReq(http.MethodPost, "/api/logout", nil, nil)
|
||||
if err != nil {
|
||||
log.Println("failed to logout session:", err)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue