checkpoint

This commit is contained in:
sawka 2022-07-04 22:18:01 -07:00
parent fc18df0601
commit 5b2e88ec32
4 changed files with 72 additions and 41 deletions

View File

@ -278,6 +278,17 @@ func HandleGetSession(w http.ResponseWriter, r *http.Request) {
return
}
// params: [none]
func HandleGetRemotes(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
w.Header().Set("Access-Control-Allow-Credentials", "true")
w.Header().Set("Vary", "Origin")
w.Header().Set("Cache-Control", "no-cache")
remotes := remote.GetAllRemoteState()
WriteJsonSuccess(w, remotes)
return
}
// params: sessionid, windowid
func HandleGetWindowLines(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", r.Header.Get("Origin"))
@ -570,6 +581,7 @@ func main() {
gr.HandleFunc("/api/ptyout", HandleGetPtyOut)
gr.HandleFunc("/api/get-session", HandleGetSession)
gr.HandleFunc("/api/get-window-lines", HandleGetWindowLines)
gr.HandleFunc("/api/get-remotes", HandleGetRemotes)
gr.HandleFunc("/api/run-command", HandleRunCommand).Methods("GET", "POST", "OPTIONS")
server := &http.Server{
Addr: MainServerAddr,

View File

@ -32,6 +32,14 @@ type Store struct {
Map map[string]*MShellProc
}
type RemoteState struct {
RemoteType string `json:"remotetype"`
RemoteId string `json:"remoteid"`
RemoteName string `json:"remotename"`
Status string `json:"status"`
Cwd string `json:"cwd"`
}
type MShellProc struct {
Lock *sync.Mutex
Remote *sstore.RemoteType
@ -78,6 +86,26 @@ func GetRemote(name string) *MShellProc {
return GlobalStore.Map[name]
}
func GetAllRemoteState() []RemoteState {
GlobalStore.Lock.Lock()
defer GlobalStore.Lock.Unlock()
var rtn []RemoteState
for _, proc := range GlobalStore.Map {
state := RemoteState{
RemoteType: proc.Remote.RemoteType,
RemoteId: proc.Remote.RemoteId,
RemoteName: proc.Remote.RemoteName,
Status: proc.Status,
}
if proc.InitPk != nil {
state.Cwd = proc.InitPk.HomeDir
}
rtn = append(rtn, state)
}
return rtn
}
func MakeMShell(r *sstore.RemoteType) *MShellProc {
rtn := &MShellProc{Lock: &sync.Mutex{}, Remote: r, Status: StatusInit}
return rtn

View File

@ -97,31 +97,19 @@ func InsertRemote(ctx context.Context, remote *RemoteType) error {
}
func GetSessionById(ctx context.Context, id string) (*SessionType, error) {
db, err := GetDB()
query := `SELECT * FROM session WHERE sessionid = ?`
var session SessionType
err = db.GetContext(ctx, &session, query, id)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &session, nil
}
func GetSessionByName(ctx context.Context, name string) (*SessionType, error) {
var rtnSession *SessionType
err := WithTx(ctx, func(tx *TxWrap) error {
var session SessionType
query := `SELECT * FROM session WHERE name = ?`
found := tx.GetWrap(&session, query, name)
query := `SELECT * FROM session WHERE sessionid = ?`
found := tx.GetWrap(&session, query, id)
if !found {
return nil
}
rtnSession = &session
query = `SELECT sessionid, windowid, name, curremote, version FROM window WHERE sessionid = ?`
tx.SelectWrap(&session.Windows, query, session.SessionId)
query = `SELECT * FROM session_remote WHERE sessionid = ?`
tx.SelectWrap(&session.Remotes, query, session.SessionId)
return nil
})
if err != nil {
@ -130,6 +118,23 @@ func GetSessionByName(ctx context.Context, name string) (*SessionType, error) {
return rtnSession, nil
}
func GetSessionByName(ctx context.Context, name string) (*SessionType, error) {
db, err := GetDB()
if err != nil {
return nil, err
}
var sessionId string
query := `SELECT sessionid FROM session WHERE name = ?`
err = db.GetContext(ctx, &sessionId, query, name)
if err != nil {
if err == sql.ErrNoRows {
return nil, nil
}
return nil, err
}
return GetSessionById(ctx, sessionId)
}
func GetWindowLines(ctx context.Context, sessionId string, windowId string) ([]*LineType, error) {
var lines []*LineType
db, err := GetDB()
@ -153,10 +158,6 @@ func InsertSessionWithName(ctx context.Context, sessionName string) error {
SessionId: uuid.New().String(),
Name: sessionName,
}
localRemote, err := GetRemoteByName(ctx, LocalRemoteName)
if err != nil {
return err
}
return WithTx(ctx, func(tx *TxWrap) error {
query := `INSERT INTO session (sessionid, name) VALUES (:sessionid, :name)`
tx.NamedExecWrap(query, session)
@ -169,16 +170,6 @@ func InsertSessionWithName(ctx context.Context, sessionName string) error {
}
query = `INSERT INTO window (sessionid, windowid, name, curremote, version) VALUES (:sessionid, :windowid, :name, :curremote, :version)`
tx.NamedExecWrap(query, window)
sr := &SessionRemote{
SessionId: session.SessionId,
WindowId: window.WindowId,
RemoteName: localRemote.RemoteName,
RemoteId: localRemote.RemoteId,
Cwd: DefaultCwd,
}
query = `INSERT INTO session_remote (sessionid, windowid, remotename, remoteid, cwd) VALUES (:sessionid, :windowid, :remotename, :remoteid, :cwd)`
tx.NamedExecWrap(query, sr)
return nil
})
}

View File

@ -52,6 +52,7 @@ type SessionType struct {
Name string `json:"name"`
Windows []*WindowType `json:"windows"`
Cmds []*CmdType `json:"cmds"`
Remotes []*SessionRemote `json:"remotes"`
}
type WindowType struct {
@ -59,7 +60,6 @@ type WindowType struct {
WindowId string `json:"windowid"`
Name string `json:"name"`
CurRemote string `json:"curremote"`
Remotes []*SessionRemote `json:"remotes"`
Lines []*LineType `json:"lines"`
Version int `json:"version"`
}