From 4a78d06f4ea33b8b91b09c37627871592fcbdba3 Mon Sep 17 00:00:00 2001 From: kekskurse Date: Mon, 8 Nov 2021 03:33:36 +0100 Subject: [PATCH] Simple main.go --- main.go | 114 ++------------- .../webpage/layoutmiddleware.go | 4 +- pkg/webpage/readme.md | 1 + pkg/webpage/webpage.go | 134 ++++++++++++++++++ readme.md | 3 +- 5 files changed, 149 insertions(+), 107 deletions(-) rename layoutmiddleware.go => pkg/webpage/layoutmiddleware.go (95%) create mode 100644 pkg/webpage/readme.md create mode 100644 pkg/webpage/webpage.go diff --git a/main.go b/main.go index 512dbc1..a1a1fbe 100644 --- a/main.go +++ b/main.go @@ -1,23 +1,10 @@ package main import ( - "database/sql" "embed" "gin-test/pkg/sample" "gin-test/pkg/user" - "github.com/go-chi/jwtauth/v5" - "github.com/rs/zerolog/log" - "net/http" - "os" - - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" - - "github.com/golang-migrate/migrate/v4" - "github.com/golang-migrate/migrate/v4/database/mysql" - "github.com/golang-migrate/migrate/v4/source/iofs" - "github.com/unrolled/render" - "github.com/urfave/cli/v2" + "gin-test/pkg/webpage" ) // content holds our static web server content. @@ -28,97 +15,16 @@ var webserver embed.FS var migrationFS embed.FS func main() { - app := &cli.App{ - Name: "Webpage", - Usage: "Sample Webapplication in golang", - Commands: []*cli.Command{ - { - Name: "run", - //Aliases: []string{"c"}, - Usage: "Run Webapplication", - Action: func(c *cli.Context) error { - return runWebpage() - }, - }, - { - Name: "migrate", - //Aliases: []string{"a"}, - Usage: "Execute the migrations", - Action: func(c *cli.Context) error { - return runMigration() - }, - }, + config := webpage.WebPageConfig{ + Commands: nil, + Templates: webserver, + Static: webserver, + Migrations: migrationFS, + Bootstrap: func(config webpage.BootstrapConfig) { + sample.Register(config.Router) + user.Register(config.Router, config.Token, config.UserClient) }, } - err := app.Run(os.Args) - if err != nil { - log.Fatal().Err(err).Msg("Cant run application") - } + webpage.RunWebApp(config) } - -func runMigration() error { - log.Debug().Msg("Start Migration") - db, err := sql.Open("mysql", "root:test@tcp(localhost:3306)/test?multiStatements=true") - if err != nil { - log.Fatal().Err(err).Msg("Cant conect to Database") - } - - d, err := iofs.New(migrationFS, "database/migrations") - - if err != nil { - log.Fatal().Err(err).Msg("Cant create iofs") - } - - driver, err := mysql.WithInstance(db, &mysql.Config{}) - - if err != nil { - log.Fatal().Err(err).Msg("Cant create driver") - } - - m, err := migrate.NewWithInstance( - "iofs", d, - "test", driver) - - if err != nil { - log.Fatal().Err(err).Msg("Cant create migration object") - } - - err = m.Up() - if err != nil { - log.Error().Err(err).Msg("Cant execute Migrations") - return err - } - log.Info().Msg("Execute Migrations") - return nil -} - -func runWebpage() error { - r := chi.NewRouter() - render := render.New(render.Options{ - FileSystem: &render.EmbedFileSystem{ - FS: webserver, - }, - }) - r.Use(middleware.Logger) - - r.Handle("/static/*", http.FileServer(http.FS(webserver))) - r.Group(func(r chi.Router) { - r.Use(TemplateMiddelware) - r.Get("/", func(w http.ResponseWriter, r *http.Request) { - render.HTML(w, http.StatusOK, "index", map[string]string {"title": "Soeren"}) - }) - - tokenAuth := jwtauth.New("HS256", []byte("secret"), nil) - uc := user.GetUserClient() - - - sample.Register(r) - user.Register(r, tokenAuth, uc) - }) - - - http.ListenAndServe(":3000", r) //todo; handle this error - return nil -} - diff --git a/layoutmiddleware.go b/pkg/webpage/layoutmiddleware.go similarity index 95% rename from layoutmiddleware.go rename to pkg/webpage/layoutmiddleware.go index a3ebce5..7f36a64 100644 --- a/layoutmiddleware.go +++ b/pkg/webpage/layoutmiddleware.go @@ -1,4 +1,4 @@ -package main +package webpage import ( "bytes" @@ -23,7 +23,7 @@ func TemplateMiddelware(next http.Handler) http.Handler { render := render.New(render.Options{ Layout: "layout", FileSystem: &render.EmbedFileSystem{ - FS: webserver, + FS: config.Templates, }, }) diff --git a/pkg/webpage/readme.md b/pkg/webpage/readme.md new file mode 100644 index 0000000..d9d53c1 --- /dev/null +++ b/pkg/webpage/readme.md @@ -0,0 +1 @@ +Add any non config things from main.go here diff --git a/pkg/webpage/webpage.go b/pkg/webpage/webpage.go new file mode 100644 index 0000000..e4ff43d --- /dev/null +++ b/pkg/webpage/webpage.go @@ -0,0 +1,134 @@ +package webpage + +import ( + "database/sql" + "embed" + "gin-test/pkg/user" + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/jwtauth/v5" + "github.com/golang-migrate/migrate/v4" + "github.com/golang-migrate/migrate/v4/database/mysql" + "github.com/golang-migrate/migrate/v4/source/iofs" + "github.com/rs/zerolog/log" + "github.com/unrolled/render" + "github.com/urfave/cli/v2" + "net/http" + "os" +) + +type WebPageConfig struct { + Commands []*cli.Command + Templates embed.FS + Static embed.FS + Migrations embed.FS + Bootstrap func(bootstrapConfig BootstrapConfig) +} + +type BootstrapConfig struct { + Router chi.Router + Token *jwtauth.JWTAuth + UserClient user.UserClient +} + +var config WebPageConfig + +func RunWebApp(cfg WebPageConfig) { + config = cfg + commands := config.Commands + commands = append(commands, &cli.Command{ + Name: "run", + //Aliases: []string{"c"}, + Usage: "Run Webapplication", + Action: func(c *cli.Context) error { + return runWebpage() + }, + }, &cli.Command{ + Name: "migrate", + //Aliases: []string{"c"}, + Usage: "Run Migration Scripts", + Action: func(c *cli.Context) error { + return runMigration() + }, + }) + + app := &cli.App{ + Name: "Webpage", + Usage: "Sample Webapplication in golang", + Commands: commands, + } + + + err := app.Run(os.Args) + if err != nil { + log.Fatal().Err(err).Msg("Cant run application") + } +} + + +func runWebpage() error { + r := chi.NewRouter() + render := render.New(render.Options{ + FileSystem: &render.EmbedFileSystem{ + FS: config.Templates, + }, + }) + r.Use(middleware.Logger) + + r.Handle("/static/*", http.FileServer(http.FS(config.Static))) + r.Group(func(r chi.Router) { + r.Use(TemplateMiddelware) + r.Get("/", func(w http.ResponseWriter, r *http.Request) { + render.HTML(w, http.StatusOK, "index", map[string]string {"title": "Soeren"}) + }) + + tokenAuth := jwtauth.New("HS256", []byte("secret"), nil) + uc := user.GetUserClient() + + config.Bootstrap(BootstrapConfig{ + Router: r, + Token: tokenAuth, + UserClient: uc, + }) + }) + + + http.ListenAndServe(":3000", r) //todo; handle this error + return nil +} + +func runMigration() error { + log.Debug().Msg("Start Migration") + db, err := sql.Open("mysql", "root:test@tcp(localhost:3306)/test?multiStatements=true") + if err != nil { + log.Fatal().Err(err).Msg("Cant conect to Database") + } + + d, err := iofs.New(config.Migrations, "database/migrations") + + if err != nil { + log.Fatal().Err(err).Msg("Cant create iofs") + } + + driver, err := mysql.WithInstance(db, &mysql.Config{}) + + if err != nil { + log.Fatal().Err(err).Msg("Cant create driver") + } + + m, err := migrate.NewWithInstance( + "iofs", d, + "test", driver) + + if err != nil { + log.Fatal().Err(err).Msg("Cant create migration object") + } + + err = m.Up() + if err != nil { + log.Error().Err(err).Msg("Cant execute Migrations") + return err + } + log.Info().Msg("Execute Migrations") + return nil +} \ No newline at end of file diff --git a/readme.md b/readme.md index ac49507..aeba963 100644 --- a/readme.md +++ b/readme.md @@ -43,4 +43,5 @@ Alles ist in einem binary # next steps * conifg per env * makefile -* ci/Cd bis docker image bauen \ No newline at end of file +* ci/Cd bis docker image bauen +* Hot reload while develop \ No newline at end of file