witty/main.go

67 lines
1.3 KiB
Go
Raw Normal View History

2022-01-04 21:41:41 +01:00
package main
import (
"log"
2022-01-04 21:41:41 +01:00
"net/http"
2022-01-05 18:38:52 +01:00
"os"
2022-01-05 21:08:27 +01:00
"github.com/gin-gonic/gin"
2022-01-04 21:41:41 +01:00
)
2022-01-09 14:56:43 +01:00
// command line options
2022-01-07 02:53:36 +01:00
var cmdToExec = []string{"bash"}
2022-01-07 02:21:11 +01:00
2022-01-05 03:21:21 +01:00
func main() {
2022-01-06 21:55:29 +01:00
fp, err := os.OpenFile("web_term.log", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
if err == nil {
defer fp.Close()
log.SetOutput(fp)
gin.DefaultWriter = fp
}
2022-01-07 02:21:11 +01:00
// parse the arguments. User can pass the command to execute
// by default, we use bash, but macos users might want to use zsh
// you can also run single program, such as pstree, htop...
// but program might misbehave (htop seems to be fine)
args := os.Args
if len(args) > 1 {
2022-01-07 02:53:36 +01:00
cmdToExec = args[1:]
2022-01-07 02:21:11 +01:00
log.Println(cmdToExec)
}
2022-01-04 21:41:41 +01:00
rt := gin.Default()
rt.SetTrustedProxies(nil)
2022-01-07 15:00:44 +01:00
rt.LoadHTMLGlob("./assets/*.html")
2022-01-04 21:41:41 +01:00
2022-01-09 14:56:43 +01:00
rt.GET("/watch/*sname", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Watcher terminal",
"path": "/ws_watch",
})
})
rt.GET("/ws_run", func(c *gin.Context) {
wsHandler(c.Writer, c.Request, false)
})
2022-01-04 21:41:41 +01:00
2022-01-09 14:56:43 +01:00
rt.GET("/ws_watch", func(c *gin.Context) {
wsHandler(c.Writer, c.Request, true)
})
// handle static files
rt.Static("/assets", "./assets")
rt.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{
"title": "Master terminal",
"path": "/ws_run ",
})
host = &c.Request.Host
2022-01-04 21:41:41 +01:00
})
2022-01-07 15:00:44 +01:00
rt.RunTLS(":8080", "./tls/cert.pem", "./tls/private-key.pem")
2022-01-05 03:21:21 +01:00
}