basic http server
This commit is contained in:
101
cmd/ui.go
101
cmd/ui.go
@ -1,29 +1,106 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"code.icod.de/postfix/manager/ui"
|
||||
"git.icod.de/dalu/ginpongo2/v5"
|
||||
"github.com/flosch/pongo2/v5"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
const (
|
||||
prefixTCP = "tcp:"
|
||||
prefixUNIX = "unix:"
|
||||
)
|
||||
|
||||
var (
|
||||
uiDebug bool
|
||||
uiAddr string
|
||||
)
|
||||
|
||||
// uiCmd represents the ui command
|
||||
var uiCmd = &cobra.Command{
|
||||
Use: "ui",
|
||||
Short: "serves the management ui over http",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
fmt.Println("ui called")
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !uiDebug {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
r := gin.Default()
|
||||
r.SecureJsonPrefix(")]}',\n")
|
||||
r.MaxMultipartMemory = 8 << 20
|
||||
if uiDebug {
|
||||
fmt.Println("debug mode")
|
||||
fl, e := pongo2.NewLocalFileSystemLoader("ui/templates/")
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
hr := ginpongo2.New(true, fl)
|
||||
r.HTMLRender = hr
|
||||
} else {
|
||||
fmt.Println("release mode")
|
||||
subFS, e := fs.Sub(ui.Templates, "templates")
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
fl := pongo2.NewFSLoader(subFS)
|
||||
hr := ginpongo2.New(false, fl)
|
||||
r.HTMLRender = hr
|
||||
}
|
||||
|
||||
// Static
|
||||
r.Static("/assets/", "./assets/")
|
||||
|
||||
r.GET("/", func(cx *gin.Context) {
|
||||
ctx := make(pongo2.Context)
|
||||
type Data struct {
|
||||
Target string
|
||||
Message string
|
||||
}
|
||||
ctx["data"] = &Data{
|
||||
Target: "World",
|
||||
Message: "It's a great day to be alive",
|
||||
}
|
||||
cx.HTML(200, "index", ctx)
|
||||
})
|
||||
|
||||
// serve
|
||||
if strings.HasPrefix(uiAddr, prefixTCP) {
|
||||
addr := strings.TrimPrefix(uiAddr, prefixTCP)
|
||||
fmt.Println("listening on", addr)
|
||||
return r.Run(addr)
|
||||
} else if strings.HasPrefix(uiAddr, prefixUNIX) {
|
||||
addr := strings.TrimPrefix(uiAddr, prefixUNIX)
|
||||
if _, e := os.Stat(addr); errors.Is(e, os.ErrNotExist) {
|
||||
fmt.Println("listening on", addr)
|
||||
return r.RunUnix(addr)
|
||||
} else {
|
||||
if e := os.Remove(addr); e != nil {
|
||||
return e
|
||||
} else {
|
||||
fmt.Println("listening on", addr)
|
||||
return r.RunUnix(addr)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
rootCmd.AddCommand(uiCmd)
|
||||
|
||||
// Here you will define your flags and configuration settings.
|
||||
|
||||
// Cobra supports Persistent Flags which will work for this command
|
||||
// and all subcommands, e.g.:
|
||||
// uiCmd.PersistentFlags().String("foo", "", "A help for foo")
|
||||
|
||||
// Cobra supports local flags which will only run when this command
|
||||
// is called directly, e.g.:
|
||||
// uiCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
||||
uiCmd.Flags().BoolVar(&uiDebug, "debug", false, "enable dev mode")
|
||||
uiCmd.Flags().StringVar(
|
||||
&uiAddr,
|
||||
"addr",
|
||||
"tcp:localhost:8080",
|
||||
"either tcp: or unix: e.g. tcp:localhost:3030 unix:/tmp/listen.sock",
|
||||
)
|
||||
}
|
||||
|
Reference in New Issue
Block a user