Simple main.go
This commit is contained in:
parent
a33efadb29
commit
4a78d06f4e
5 changed files with 149 additions and 107 deletions
114
main.go
114
main.go
|
@ -1,23 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
|
||||||
"embed"
|
"embed"
|
||||||
"gin-test/pkg/sample"
|
"gin-test/pkg/sample"
|
||||||
"gin-test/pkg/user"
|
"gin-test/pkg/user"
|
||||||
"github.com/go-chi/jwtauth/v5"
|
"gin-test/pkg/webpage"
|
||||||
"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"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// content holds our static web server content.
|
// content holds our static web server content.
|
||||||
|
@ -28,97 +15,16 @@ var webserver embed.FS
|
||||||
var migrationFS embed.FS
|
var migrationFS embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := &cli.App{
|
config := webpage.WebPageConfig{
|
||||||
Name: "Webpage",
|
Commands: nil,
|
||||||
Usage: "Sample Webapplication in golang",
|
Templates: webserver,
|
||||||
Commands: []*cli.Command{
|
Static: webserver,
|
||||||
{
|
Migrations: migrationFS,
|
||||||
Name: "run",
|
Bootstrap: func(config webpage.BootstrapConfig) {
|
||||||
//Aliases: []string{"c"},
|
sample.Register(config.Router)
|
||||||
Usage: "Run Webapplication",
|
user.Register(config.Router, config.Token, config.UserClient)
|
||||||
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()
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
err := app.Run(os.Args)
|
webpage.RunWebApp(config)
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err).Msg("Cant run application")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package webpage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
@ -23,7 +23,7 @@ func TemplateMiddelware(next http.Handler) http.Handler {
|
||||||
render := render.New(render.Options{
|
render := render.New(render.Options{
|
||||||
Layout: "layout",
|
Layout: "layout",
|
||||||
FileSystem: &render.EmbedFileSystem{
|
FileSystem: &render.EmbedFileSystem{
|
||||||
FS: webserver,
|
FS: config.Templates,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
1
pkg/webpage/readme.md
Normal file
1
pkg/webpage/readme.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Add any non config things from main.go here
|
134
pkg/webpage/webpage.go
Normal file
134
pkg/webpage/webpage.go
Normal file
|
@ -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
|
||||||
|
}
|
|
@ -44,3 +44,4 @@ Alles ist in einem binary
|
||||||
* conifg per env
|
* conifg per env
|
||||||
* makefile
|
* makefile
|
||||||
* ci/Cd bis docker image bauen
|
* ci/Cd bis docker image bauen
|
||||||
|
* Hot reload while develop
|
Loading…
Reference in a new issue