Skip to content

Commit

Permalink
wip: add dashboard layout
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Apr 19, 2024
1 parent 4e04618 commit e3c8583
Show file tree
Hide file tree
Showing 21 changed files with 24,647 additions and 178 deletions.
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
GITHUB_ID=
GITHUB_SECRET=
DB_HOST=localhost
DB_NAME=example
DB_USER=example
DB_PASS=example
45 changes: 45 additions & 0 deletions cmd/web/cmd/cfg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"os"
)

var cfg = New()

// DB ...
type DB struct {
Username string
Password string
Port int
Database string
}

// Flags contains the command line flags.
type Flags struct {
Addr string
DB *DB
}

// NewFlags ...
func NewFlags() *Flags {
return &Flags{
DB: &DB{},
}
}

// New ...
func New() *Config {
return &Config{
Flags: NewFlags(),
}
}

// Config ...
type Config struct {
Flags *Flags
}

// Cwd returns the current working directory.
func (c *Config) Cwd() (string, error) {
return os.Getwd()
}
118 changes: 118 additions & 0 deletions cmd/web/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cmd

import (
"context"
"net/http"
"os"

"github.com/zeiss/fiber-goth/providers"
"github.com/zeiss/fiber-goth/providers/github"
"github.com/zeiss/typhoon/internal/web/adapters"
"github.com/zeiss/typhoon/internal/web/services"
"github.com/zeiss/typhoon/static"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
logger "github.com/gofiber/fiber/v2/middleware/logger"
requestid "github.com/gofiber/fiber/v2/middleware/requestid"
"github.com/katallaxie/pkg/server"
"github.com/spf13/cobra"
authz "github.com/zeiss/fiber-authz"
goth "github.com/zeiss/fiber-goth"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)

func init() {
Root.PersistentFlags().StringVar(&cfg.Flags.Addr, "addr", ":8080", "addr")
Root.PersistentFlags().StringVar(&cfg.Flags.DB.Database, "db-database", cfg.Flags.DB.Database, "Database name")
Root.PersistentFlags().StringVar(&cfg.Flags.DB.Username, "db-username", cfg.Flags.DB.Username, "Database user")
Root.PersistentFlags().StringVar(&cfg.Flags.DB.Password, "db-password", cfg.Flags.DB.Password, "Database password")
Root.PersistentFlags().IntVar(&cfg.Flags.DB.Port, "db-port", cfg.Flags.DB.Port, "Database port")

Root.SilenceUsage = true
}

var Root = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
srv := NewWebSrv(cfg)

s, _ := server.WithContext(cmd.Context())
s.Listen(srv, false)

return s.Wait()
},
}

var _ server.Listener = (*WebSrv)(nil)

// WebSrv is the server that implements the Noop interface.
type WebSrv struct {
cfg *Config
}

// NewWebSrv returns a new instance of NoopSrv.
func NewWebSrv(cfg *Config) *WebSrv {
return &WebSrv{cfg}
}

// Start starts the server.
func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.RunFunc) func() error {
return func() error {
providers.RegisterProvider(github.New(os.Getenv("GITHUB_KEY"), os.Getenv("GITHUB_SECRET"), "http://localhost:8080/auth/github/callback"))

dsn := "host=host.docker.internal user=example password=example dbname=example port=5432 sslmode=disable"
conn, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return err
}

db := adapters.NewDB(conn)
err = db.RunMigrations()
if err != nil {
return err
}

tbac := authz.NewTBAC(conn)

gothConfig := goth.Config{
Adapter: tbac,
Secret: goth.GenerateKey(),
CookieHTTPOnly: true,
ResponseFilter: func(c *fiber.Ctx) error {
return c.Redirect("/")
},
}

handlers := services.NewHandlers()

app := fiber.New()
app.Use(requestid.New())
app.Use(logger.New())

app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(static.Assets),
}))

app.Use(goth.NewProtectMiddleware(gothConfig))
app.Use(authz.SetAuthzHandler(authz.NewNoopObjectResolver(), authz.NewNoopActionResolver(), authz.NewGothAuthzPrincipalResolver()))

app.Get("/login", handlers.Login())
app.Get("/login/:provider", goth.NewBeginAuthHandler(gothConfig))
app.Get("/auth/:provider/callback", goth.NewCompleteAuthHandler(gothConfig))
app.Get("/logout", goth.NewLogoutHandler(gothConfig))

// Root handler
app.Get("/", handlers.Dashboard())

// Me handler
app.Get("/me", handlers.Me())

err = app.Listen(s.cfg.Flags.Addr)
if err != nil {
return err
}

return nil
}
}
11 changes: 11 additions & 0 deletions cmd/web/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"github.com/zeiss/typhoon/cmd/web/cmd"
)

func main() {
if err := cmd.Root.Execute(); err != nil {
panic(err)
}
}
Loading

0 comments on commit e3c8583

Please sign in to comment.