46 lines
886 B
Go
46 lines
886 B
Go
package web
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/labstack/echo/v4/middleware"
|
|
)
|
|
|
|
type WebServer struct {
|
|
Listen string
|
|
Port int64
|
|
Directory string
|
|
Url string
|
|
Debug bool
|
|
}
|
|
|
|
func NewWebServer(listen string, port int64, directory, url string, debug bool) *WebServer {
|
|
return &WebServer{
|
|
Listen: listen,
|
|
Port: port,
|
|
Directory: strings.TrimSuffix(directory, "/"),
|
|
Url: strings.TrimSuffix(url, "/"),
|
|
Debug: debug,
|
|
}
|
|
}
|
|
|
|
func (w *WebServer) Run() {
|
|
e := echo.New()
|
|
|
|
if w.Debug {
|
|
e.Use(middleware.Logger())
|
|
e.Use(middleware.Recover())
|
|
}
|
|
|
|
e.GET("/api/list", func(ctx echo.Context) error {
|
|
return List(ctx, w.Directory, w.Url)
|
|
})
|
|
|
|
e.GET("/api/stream", func(ctx echo.Context) error {
|
|
return Stream(ctx, w.Directory, w.Url)
|
|
})
|
|
|
|
e.Logger.Fatal(e.Start(fmt.Sprintf("%s:%d", w.Listen, w.Port)))
|
|
}
|