podterminal/web/router.go

144 lines
3.2 KiB
Go

package web
import (
"fmt"
"log"
"net/http/httputil"
"net/url"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-contrib/sessions/cookie"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/spf13/viper"
"git.jmbit.de/jmb/podterminal/pods"
)
var proxies = make(map[string]*httputil.ReverseProxy)
func Run() error {
router := setupRouter()
address := fmt.Sprintf("%s:%d", viper.GetString("ip_addr"), viper.GetInt("port"))
log.Println("Listening on address", address)
var err error
if viper.GetBool("ssl") == true {
err = router.RunTLS(
address,
viper.GetString("ssl_cert"),
viper.GetString("ssl_cert_key"),
)
log.Println("Using SSL")
} else {
err = router.Run(address)
}
log.Println("Router is ready")
if err != nil {
return err
}
return nil
}
func setupRouter() *gin.Engine {
log.Println("Setting up router")
gin.ForceConsoleColor()
gin.SetMode("release")
router := gin.New()
store := cookie.NewStore([]byte(uuid.NewString()))
store.Options(sessions.Options{
MaxAge: viper.GetInt("maxAge"),
})
router.Use(gin.Recovery())
router.Use(sessions.Sessions("session", store))
//router.Use(urlLog())
router.Use(containerProxy)
// router.Any("/", containerProxy)
return router
}
func urlLog() gin.HandlerFunc {
return func(c *gin.Context) {
log.Printf(
"[INFO] %s: %s: %s",
c.Request.RemoteAddr,
c.Request.Method,
c.Request.URL,
)
}
}
func createReverseProxy(backendService string) (*httputil.ReverseProxy, error) {
var err error
log.Println("Creating reverse Proxy for ", backendService)
backendURL, err := url.Parse(backendService)
if err != nil {
log.Printf("Could not parse backend URL: %v", err)
}
proxy := &httputil.ReverseProxy{
Rewrite: func(request *httputil.ProxyRequest) {
request.SetURL(backendURL)
request.Out.Host = request.In.Host
},
}
return proxy, err
}
func containerProxy(c *gin.Context) {
session := sessions.Default(c)
session.Save()
sessionID := session.ID()
if session.Get("ct") == nil {
log.Println("Creating Container for Session ", sessionID)
ct, err := pods.CreateContainer()
if err != nil {
c.HTML(500, "Error", fmt.Sprintf("[%s] Could not create Container: %v", sessionID, err))
c.Abort()
}
err = pods.StartContainer(ct)
if err != nil {
c.HTML(500, "Error", fmt.Sprintf("[%s] Could not start Container: %v", sessionID, err))
c.Abort()
}
// Hack to wait for Container to start up and get assigned an IP
time.Sleep(3 * time.Second)
ctip, err := pods.GetContainerIP(ct)
if err != nil {
c.HTML(500, "Error", fmt.Sprintf("[%s] Could not get Container ip: %v", sessionID, err))
c.Abort()
}
proxies[ct], err = createReverseProxy(fmt.Sprintf("http://%s:3000", ctip))
if err != nil {
c.HTML(
500,
"Error",
fmt.Sprintf("[%s] Could not create Container Proxy: %v", sessionID, err),
)
c.Abort()
}
session.Set("ct", ct)
session.Save()
c.Redirect(301, "/")
} else {
sessionCT := session.Get("ct")
switch sessionCT.(type) {
case string:
default:
c.HTML(500, "Error", "Session Container ID is not a string")
c.Abort()
}
id := session.Get("ct").(string)
proxy := proxies[id]
proxy.ServeHTTP(c.Writer, c.Request)
}
}