rui_orig/appServer.go

420 lines
8.9 KiB
Go
Raw Normal View History

2022-10-27 16:14:30 +03:00
//go:build !wasm
package rui
import (
"context"
_ "embed"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os/exec"
"runtime"
"strconv"
"strings"
"time"
)
//go:embed app_socket.js
var socketScripts string
2024-03-12 19:32:22 +03:00
//go:embed app_post.js
var httpPostScripts string
2022-11-09 13:40:08 +03:00
func debugLog(text string) {
log.Println("\033[34m" + text)
}
func errorLog(text string) {
log.Println("\033[31m" + text)
}
2024-03-12 19:32:22 +03:00
type sessionInfo struct {
session Session
response chan string
}
2022-10-27 16:14:30 +03:00
type application struct {
server *http.Server
params AppParams
createContentFunc func(Session) SessionContent
2024-03-12 19:32:22 +03:00
sessions map[int]sessionInfo
2022-10-27 16:14:30 +03:00
}
func (app *application) getStartPage() string {
buffer := allocStringBuilder()
defer freeStringBuilder(buffer)
buffer.WriteString("<!DOCTYPE html>\n<html>\n")
2024-03-12 19:32:22 +03:00
getStartPage(buffer, app.params)
2022-10-27 16:14:30 +03:00
buffer.WriteString("\n</html>")
return buffer.String()
}
func (app *application) Finish() {
for _, session := range app.sessions {
2024-03-12 19:32:22 +03:00
session.session.close()
if session.response != nil {
close(session.response)
session.response = nil
}
2022-10-27 16:14:30 +03:00
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := app.server.Shutdown(ctx); err != nil {
log.Println(err.Error())
}
}
func (app *application) nextSessionID() int {
n := rand.Intn(0x7FFFFFFE) + 1
_, ok := app.sessions[n]
for ok {
n = rand.Intn(0x7FFFFFFE) + 1
_, ok = app.sessions[n]
}
return n
}
func (app *application) removeSession(id int) {
2024-03-12 19:32:22 +03:00
if info, ok := app.sessions[id]; ok {
if info.response != nil {
close(info.response)
}
delete(app.sessions, id)
}
2022-10-27 16:14:30 +03:00
}
func (app *application) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if ProtocolInDebugLog {
DebugLogF("%s %s", req.Method, req.URL.Path)
}
switch req.Method {
2024-03-12 19:32:22 +03:00
case "POST":
if req.URL.Path == "/" {
app.postHandler(w, req)
}
2022-10-27 16:14:30 +03:00
case "GET":
switch req.URL.Path {
case "/":
w.WriteHeader(http.StatusOK)
io.WriteString(w, app.getStartPage())
case "/ws":
2024-03-12 19:32:22 +03:00
if bridge := createSocketBridge(w, req); bridge != nil {
2022-11-02 21:05:55 +03:00
go app.socketReader(bridge)
2022-10-27 16:14:30 +03:00
}
2024-03-12 19:32:22 +03:00
case "/script.js":
w.WriteHeader(http.StatusOK)
if app.params.NoSocket {
io.WriteString(w, httpPostScripts)
} else {
io.WriteString(w, socketScripts)
}
io.WriteString(w, "\n")
io.WriteString(w, defaultScripts)
2022-10-27 16:14:30 +03:00
default:
filename := req.URL.Path[1:]
if size := len(filename); size > 0 && filename[size-1] == '/' {
filename = filename[:size-1]
}
if !serveResourceFile(filename, w, req) &&
!serveDownloadFile(filename, w, req) {
w.WriteHeader(http.StatusNotFound)
}
}
}
}
2024-03-12 19:32:22 +03:00
func setSessionIDCookie(w http.ResponseWriter, sessionID int) {
cookie := http.Cookie{
Name: "session",
Value: strconv.Itoa(sessionID),
HttpOnly: true,
}
http.SetCookie(w, &cookie)
}
func (app *application) postHandler(w http.ResponseWriter, req *http.Request) {
if reqBody, err := io.ReadAll(req.Body); err == nil {
message := string(reqBody)
if ProtocolInDebugLog {
DebugLog(message)
}
if obj := ParseDataText(message); obj != nil {
var session Session = nil
var response chan string = nil
if cookie, err := req.Cookie("session"); err == nil {
sessionID, err := strconv.Atoi(cookie.Value)
if err != nil {
ErrorLog(err.Error())
} else if info, ok := app.sessions[sessionID]; ok && info.response != nil {
response = info.response
session = info.session
}
}
command := obj.Tag()
2024-03-13 15:01:02 +03:00
startSession := false
2024-03-12 19:32:22 +03:00
2024-03-13 15:01:02 +03:00
if session == nil || command == "startSession" {
events := make(chan DataObject, 1024)
bridge := createHttpBridge(req)
response = bridge.response
answer := ""
session, answer = app.startSession(obj, events, bridge, response)
2024-03-12 19:32:22 +03:00
2024-03-13 15:01:02 +03:00
bridge.writeMessage(answer)
session.onStart()
if command == "session-resume" {
session.onResume()
}
bridge.sendResponse()
2024-03-12 19:32:22 +03:00
2024-03-13 15:01:02 +03:00
setSessionIDCookie(w, session.ID())
startSession = true
2024-03-12 19:32:22 +03:00
2024-03-13 15:01:02 +03:00
go sessionEventHandler(session, events, bridge)
2024-03-12 19:32:22 +03:00
}
2024-03-13 15:01:02 +03:00
if !startSession {
switch command {
case "nop":
session.sendResponse()
2024-03-12 19:32:22 +03:00
2024-03-13 15:01:02 +03:00
case "session-close":
session.onFinish()
session.App().removeSession(session.ID())
return
default:
if !session.handleAnswer(command, obj) {
session.addToEventsQueue(obj)
}
2024-03-12 19:32:22 +03:00
}
}
io.WriteString(w, <-response)
for len(response) > 0 {
io.WriteString(w, <-response)
}
}
}
}
func (app *application) socketReader(bridge *wsBridge) {
2022-10-27 16:14:30 +03:00
var session Session
events := make(chan DataObject, 1024)
for {
2022-11-02 21:05:55 +03:00
message, ok := bridge.readMessage()
2022-10-27 16:14:30 +03:00
if !ok {
events <- NewDataObject("disconnect")
return
}
if ProtocolInDebugLog {
2024-03-12 19:32:22 +03:00
DebugLog("🖥️ -> " + message)
2022-10-27 16:14:30 +03:00
}
if obj := ParseDataText(message); obj != nil {
command := obj.Tag()
switch command {
case "startSession":
answer := ""
2024-03-12 19:32:22 +03:00
if session, answer = app.startSession(obj, events, bridge, nil); session != nil {
2022-11-02 21:05:55 +03:00
if !bridge.writeMessage(answer) {
2022-10-27 16:14:30 +03:00
return
}
session.onStart()
2022-11-02 21:05:55 +03:00
go sessionEventHandler(session, events, bridge)
2022-10-27 16:14:30 +03:00
}
case "reconnect":
if sessionText, ok := obj.PropertyValue("session"); ok {
if sessionID, err := strconv.Atoi(sessionText); err == nil {
2024-03-12 19:32:22 +03:00
if info, ok := app.sessions[sessionID]; ok {
session := info.session
2022-11-02 21:05:55 +03:00
session.setBridge(events, bridge)
2022-10-27 16:14:30 +03:00
answer := allocStringBuilder()
defer freeStringBuilder(answer)
session.writeInitScript(answer)
2022-11-02 21:05:55 +03:00
if !bridge.writeMessage(answer.String()) {
2022-10-27 16:14:30 +03:00
return
}
session.onReconnect()
2022-11-02 21:05:55 +03:00
go sessionEventHandler(session, events, bridge)
2022-10-27 16:14:30 +03:00
return
}
DebugLogF("Session #%d not exists", sessionID)
} else {
ErrorLog(`strconv.Atoi(sessionText) error: ` + err.Error())
}
} else {
ErrorLog(`"session" key not found`)
}
2024-04-22 19:17:04 +03:00
bridge.writeMessage("restartSession();")
2022-10-27 16:14:30 +03:00
default:
2024-03-12 19:32:22 +03:00
if !session.handleAnswer(command, obj) {
events <- obj
}
2022-10-27 16:14:30 +03:00
}
}
}
}
2024-03-12 19:32:22 +03:00
func sessionEventHandler(session Session, events chan DataObject, bridge bridge) {
2022-10-27 16:14:30 +03:00
for {
data := <-events
switch command := data.Tag(); command {
case "disconnect":
session.onDisconnect()
return
case "session-close":
session.onFinish()
session.App().removeSession(session.ID())
2022-11-02 21:05:55 +03:00
bridge.close()
2022-10-27 16:14:30 +03:00
default:
session.handleEvent(command, data)
}
}
}
2024-03-12 19:32:22 +03:00
func (app *application) startSession(params DataObject, events chan DataObject,
bridge bridge, response chan string) (Session, string) {
2022-10-27 16:14:30 +03:00
if app.createContentFunc == nil {
return nil, ""
}
session := newSession(app, app.nextSessionID(), "", params)
2022-11-02 21:05:55 +03:00
session.setBridge(events, bridge)
2022-11-01 20:13:09 +03:00
if !session.setContent(app.createContentFunc(session)) {
2022-10-27 16:14:30 +03:00
return nil, ""
}
2024-03-12 19:32:22 +03:00
app.sessions[session.ID()] = sessionInfo{
session: session,
response: response,
}
2022-10-27 16:14:30 +03:00
answer := allocStringBuilder()
defer freeStringBuilder(answer)
answer.WriteString("sessionID = '")
answer.WriteString(strconv.Itoa(session.ID()))
answer.WriteString("';\n")
session.writeInitScript(answer)
answerText := answer.String()
if ProtocolInDebugLog {
DebugLog("Start session:")
DebugLog(answerText)
}
return session, answerText
}
var apps = []*application{}
// StartApp - create the new application and start it
func StartApp(addr string, createContentFunc func(Session) SessionContent, params AppParams) {
app := new(application)
app.params = params
2024-03-12 19:32:22 +03:00
app.sessions = map[int]sessionInfo{}
2022-10-27 16:14:30 +03:00
app.createContentFunc = createContentFunc
apps = append(apps, app)
redirectAddr := ""
if index := strings.IndexRune(addr, ':'); index >= 0 {
redirectAddr = addr[:index] + ":80"
} else {
redirectAddr = addr + ":80"
if params.CertFile != "" && params.KeyFile != "" {
addr += ":443"
} else {
addr += ":80"
}
}
app.server = &http.Server{Addr: addr}
http.Handle("/", app)
serverRun := func(err error) {
if err != nil {
if err == http.ErrServerClosed {
log.Println(err)
} else {
log.Fatal(err)
}
}
}
if params.CertFile != "" && params.KeyFile != "" {
if params.Redirect80 {
redirectTLS := func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+addr+r.RequestURI, http.StatusMovedPermanently)
}
go func() {
serverRun(http.ListenAndServe(redirectAddr, http.HandlerFunc(redirectTLS)))
}()
}
serverRun(app.server.ListenAndServeTLS(params.CertFile, params.KeyFile))
} else {
serverRun(app.server.ListenAndServe())
}
}
func FinishApp() {
for _, app := range apps {
app.Finish()
}
apps = []*application{}
}
func OpenBrowser(url string) bool {
var err error
switch runtime.GOOS {
case "linux":
for _, provider := range []string{"xdg-open", "x-www-browser", "www-browser"} {
if _, err = exec.LookPath(provider); err == nil {
2024-02-27 17:08:05 +03:00
if err = exec.Command(provider, url).Start(); err == nil {
2022-10-27 16:14:30 +03:00
return true
}
}
}
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err != nil
}