diff --git a/cmd/handler.go b/cmd/handler.go index 9805a82..961c55f 100644 --- a/cmd/handler.go +++ b/cmd/handler.go @@ -14,9 +14,13 @@ import ( "gitlab.com/omnibill/linux" "gitlab.com/omnibill/tui/confirmation" "gitlab.com/omnibill/tui/textinput" + "go/format" + "go/parser" + "go/token" "golang.org/x/mod/modfile" "golang.org/x/text/cases" "golang.org/x/text/language" + "io" "io/fs" "log" "os" @@ -236,6 +240,10 @@ var handlerCmd = &cobra.Command{ log.Fatalln(err) } + if len(pathSplit) == 1 { + tmplData.PackagePath = "handlers" + } + for i, _ := range pathSplit { isLast := i == len(pathSplit)-1 path := filepath.Join(append([]string{handlerDir}, pathSplit[0:i+1]...)...) @@ -246,9 +254,38 @@ var handlerCmd = &cobra.Command{ if err := os.MkdirAll(path, 0740); err != nil { log.Fatalln(err) } + if err := os.Rename(path+".go", path+"/index.go"); err != nil { log.Fatalln(err) } + + file, err := os.OpenFile(path+"/index.go", os.O_RDWR|os.O_CREATE, 0600) + if err != nil { + log.Fatalln(err) + } + + fset := token.NewFileSet() + node, err := parser.ParseFile(fset, path+"/index.go", file, parser.ParseComments) + if err != nil { + log.Fatalln(err) + } + + node.Name.Name = tmplData.PackagePath + + if err := file.Truncate(0); err != nil { + log.Fatalln(err) + } + + _, err = file.Seek(0, io.SeekStart) + if err != nil { + log.Fatalf("Failed to seek to beginning: %v", err) + } + + if err := format.Node(file, fset, node); err != nil { + log.Fatalln(err) + } + + file.Close() } if _, err := os.Lstat(path); err != nil && !errors.Is(err, fs.ErrNotExist) { diff --git a/cmd/root.go b/cmd/root.go index f2ef5d9..f62a399 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -44,11 +44,12 @@ func Execute() { func init() { cobra.OnInitialize(initConfig) - rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.omnibill.yaml)") + rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.omnibill.toml)") } // initConfig reads in config file and ENV variables if set. func initConfig() { + // All my homies hate yaml, lol. viper.SetConfigType("toml") if cfgFile != "" { viper.SetConfigFile(cfgFile) diff --git a/cmd/templates/handler.go.tmpl b/cmd/templates/handler.go.tmpl index fe3747f..72b9470 100644 --- a/cmd/templates/handler.go.tmpl +++ b/cmd/templates/handler.go.tmpl @@ -13,16 +13,17 @@ import ( "github.com/gofiber/fiber/v2/middleware/session" "github.com/uptrace/bun" "go.uber.org/zap" + {{- if .GetView }} "{{.ModulePath}}/web/utils" + {{ end }} + . "{{.ModulePath}}/web/utils/handler" {{- if .GetView }} PAGE_VIEW "{{.ModulePath}}/web/views/{{.Path}}" {{ end }} ) func init() { - utils.Handlers = append(utils.Handlers, &{{.UpperName}}Handler{ - Path: "{{.Path}}", - }) + Register({{.UpperName}}Handler{Path: "{{.Path}}"}) } type {{.UpperName}}Handler struct { diff --git a/cmd/templates/imports.go.tmpl b/cmd/templates/imports.go.tmpl index 6f91345..32cf517 100644 --- a/cmd/templates/imports.go.tmpl +++ b/cmd/templates/imports.go.tmpl @@ -1,4 +1,4 @@ -// Code generated by Omnibill - DO NOT EDIT +// Code generated by omnibill - DO NOT EDIT. package handlers diff --git a/go.mod b/go.mod index c2e841f..f07c54d 100644 --- a/go.mod +++ b/go.mod @@ -23,6 +23,7 @@ require ( ) require ( + github.com/AfterShip/email-verifier v1.4.1 // indirect github.com/a-h/templ v0.2.793 // indirect github.com/andybalholm/brotli v1.1.1 // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect @@ -34,6 +35,7 @@ require ( github.com/google/go-tpm v0.9.1 // indirect github.com/google/uuid v1.6.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect + github.com/hbollon/go-edlib v1.6.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect @@ -69,6 +71,7 @@ require ( go.uber.org/atomic v1.9.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect + golang.org/x/net v0.30.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.26.0 // indirect golang.org/x/term v0.25.0 // indirect diff --git a/go.sum b/go.sum index a1a41ec..996643f 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/AfterShip/email-verifier v1.4.1 h1:vDmnqq680siSLw8rtiAYaqgmqYeW+AUoMfEY1RjWK8k= +github.com/AfterShip/email-verifier v1.4.1/go.mod h1:AcFyA5b7X6L4l5dBuemWBSh8mq74nxkBTtoWgLOFrbw= github.com/a-h/templ v0.2.793 h1:Io+/ocnfGWYO4VHdR0zBbf39PQlnzVCVVD+wEEs6/qY= github.com/a-h/templ v0.2.793/go.mod h1:lq48JXoUvuQrU0VThrK31yFwdRjTCnIE5bcPCM9IP1w= github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA= @@ -32,6 +34,8 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hbollon/go-edlib v1.6.0 h1:ga7AwwVIvP8mHm9GsPueC0d71cfRU/52hmPJ7Tprv4E= +github.com/hbollon/go-edlib v1.6.0/go.mod h1:wnt6o6EIVEzUfgbUZY7BerzQ2uvzp354qmS2xaLkrhM= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= @@ -149,6 +153,8 @@ golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0 golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY= golang.org/x/mod v0.20.0 h1:utOm6MM3R3dnawAiJgn0y+xvuYRsm1RKM/4giyfDgV0= golang.org/x/mod v0.20.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= +golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= +golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/web/utils/handler/funcs.go b/web/utils/handler/funcs.go new file mode 100644 index 0000000..130b5a4 --- /dev/null +++ b/web/utils/handler/funcs.go @@ -0,0 +1,62 @@ +package handler + +import ( + "github.com/gofiber/fiber/v2" + "github.com/gofiber/fiber/v2/middleware/session" + "go.uber.org/zap" + "omnibill.net/omnibill/web/utils" +) + +// Register registers the handler with the webserver. +// It takes a handler as it's input. +// If the handler does not implement any of the handler interfaces, it will panic. +func Register(handler any) { + var hasHandler bool + if _, ok := handler.(utils.GET); ok { + hasHandler = true + } + if _, ok := handler.(utils.POST); ok { + hasHandler = true + } + if _, ok := handler.(utils.PUT); ok { + hasHandler = true + } + if _, ok := handler.(utils.DELETE); ok { + hasHandler = true + } + if _, ok := handler.(utils.PATCH); ok { + hasHandler = true + } + if _, ok := handler.(utils.HEAD); ok { + hasHandler = true + } + if _, ok := handler.(utils.OPTIONS); ok { + hasHandler = true + } + + if !hasHandler { + panic("Invalid handler") + } + + utils.Handlers = append(utils.Handlers, &handler) +} + +// DestroySession destroys any Session provided to it. +// It takes: zap.Logger, session.Session, fiber.Ctx, and an optional redirectPath of type string. +// The function returns a redirect with an error, or just an error, if any. +func DestroySession(logger *zap.Logger, sess *session.Session, ctx *fiber.Ctx, redirectPath ...string) error { + if err := sess.Destroy(); err != nil { + logger.Error("error destroying session", zap.Error(err)) + return fiber.ErrInternalServerError + } + if err := sess.Save(); err != nil { + logger.Error("error saving session", zap.Error(err)) + return fiber.ErrInternalServerError + } + + if redirectPath != nil && len(redirectPath) > 0 { + return ctx.Redirect(redirectPath[0]) + } else { + return nil + } +} diff --git a/web/utils/session.go b/web/utils/session.go deleted file mode 100644 index d4b585b..0000000 --- a/web/utils/session.go +++ /dev/null @@ -1 +0,0 @@ -package utils