go-sample-webpage/main.go

125 lines
2.6 KiB
Go
Raw Normal View History

2021-11-03 01:10:36 +00:00
package main
import (
2021-11-08 01:25:36 +00:00
"database/sql"
2021-11-03 01:10:36 +00:00
"embed"
"gin-test/pkg/sample"
2021-11-04 01:14:51 +00:00
"gin-test/pkg/user"
2021-11-08 01:25:36 +00:00
"github.com/go-chi/jwtauth/v5"
"github.com/rs/zerolog/log"
2021-11-03 01:10:36 +00:00
"net/http"
2021-11-08 01:25:36 +00:00
"os"
2021-11-03 01:10:36 +00:00
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
2021-11-04 01:14:51 +00:00
2021-11-08 01:25:36 +00:00
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
"github.com/golang-migrate/migrate/v4/source/iofs"
2021-11-03 01:10:36 +00:00
"github.com/unrolled/render"
2021-11-08 01:25:36 +00:00
"github.com/urfave/cli/v2"
2021-11-03 01:10:36 +00:00
)
// content holds our static web server content.
//go:embed static/* templates/*
var webserver embed.FS
2021-11-08 01:25:36 +00:00
//go:embed database/migrations/*.sql
var migrationFS embed.FS
2021-11-03 01:10:36 +00:00
func main() {
2021-11-08 01:25:36 +00:00
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()
},
},
},
}
err := app.Run(os.Args)
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 {
2021-11-03 01:10:36 +00:00
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"})
})
2021-11-08 01:25:36 +00:00
tokenAuth := jwtauth.New("HS256", []byte("secret"), nil)
uc := user.GetUserClient()
2021-11-03 01:10:36 +00:00
2021-11-08 01:25:36 +00:00
sample.Register(r)
user.Register(r, tokenAuth, uc)
})
2021-11-03 01:10:36 +00:00
2021-11-08 01:25:36 +00:00
http.ListenAndServe(":3000", r) //todo; handle this error
return nil
2021-11-03 01:10:36 +00:00
}