First try
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package infrastructure
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type ApplicationEnvironment struct {
|
||||
TemplateLocation string
|
||||
}
|
||||
|
||||
func ConfigSetup(configName, configPath string) {
|
||||
viper.SetConfigName(configName)
|
||||
viper.SetConfigType("toml")
|
||||
viper.AddConfigPath(configPath)
|
||||
}
|
||||
|
||||
func ValidateVariablesAreSet(variables []string) {
|
||||
for i := range variables {
|
||||
if !viper.IsSet(variables[i]) {
|
||||
log.Fatalf("%s variable was not set!\nAborting application start!", variables[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetConfig() ApplicationEnvironment {
|
||||
|
||||
if err := viper.ReadInConfig(); err != nil {
|
||||
log.Fatalf("fatal error config file in infrastructure: %s", err)
|
||||
}
|
||||
ValidateVariablesAreSet([]string{})
|
||||
|
||||
return ApplicationEnvironment{
|
||||
TemplateLocation: viper.GetString("TemplateLocation"),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/diaid83/pokemongotemplates/internal/infrastructure"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ApplicationState struct {
|
||||
HTTPServer *http.Server
|
||||
Handler *gin.Engine
|
||||
Config *infrastructure.ApplicationEnvironment
|
||||
}
|
||||
|
||||
type ApplicationServer struct {
|
||||
State ApplicationState
|
||||
}
|
||||
|
||||
func (s *ApplicationServer) registerHandlers() {
|
||||
var files []string
|
||||
|
||||
templateLocation := fmt.Sprintf("%s/web/templates", s.State.Config.TemplateLocation)
|
||||
|
||||
err := filepath.Walk(templateLocation, func(path string, info fs.FileInfo, err error) error {
|
||||
if strings.HasSuffix(path, ".gohtml") {
|
||||
files = append(files, path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
s.State.Handler.LoadHTMLFiles(files...)
|
||||
s.State.Handler.GET("/", s.pokemonPageHandler())
|
||||
s.State.Handler.GET("/custom", s.customPokemonPageHandler())
|
||||
}
|
||||
|
||||
func NewApplicationServer(userOptions *ApplicationState) *ApplicationServer {
|
||||
state := userOptions
|
||||
if state == nil {
|
||||
state = &ApplicationState{}
|
||||
}
|
||||
|
||||
if state.Handler == nil {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
state.Handler = gin.Default()
|
||||
}
|
||||
|
||||
if state.HTTPServer == nil {
|
||||
state.HTTPServer = &http.Server{
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 100 * time.Second,
|
||||
Addr: ":8080",
|
||||
Handler: state.Handler,
|
||||
}
|
||||
}
|
||||
|
||||
if state.Config == nil {
|
||||
config := infrastructure.GetConfig()
|
||||
state.Config = &config
|
||||
}
|
||||
|
||||
s := ApplicationServer{
|
||||
State: ApplicationState{
|
||||
HTTPServer: state.HTTPServer,
|
||||
Handler: state.Handler,
|
||||
Config: state.Config,
|
||||
},
|
||||
}
|
||||
s.registerHandlers()
|
||||
return &s
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/diaid83/pokemongotemplates/internal/domain/pokemongotemplates"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (s *ApplicationServer) pokemonPageHandler() func(*gin.Context) {
|
||||
data, err := pokemongotemplates.GetPokemons()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
return func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "views/Pokemons.gohtml", pokemongotemplates.PageData{
|
||||
NavigationLinks: pokemongotemplates.NavigationLinks,
|
||||
Data: *data,
|
||||
})
|
||||
}
|
||||
}
|
||||
func (s *ApplicationServer) customPokemonPageHandler() func(*gin.Context) {
|
||||
return func(c *gin.Context) {
|
||||
c.HTML(http.StatusOK, "views/CustomPokemons.gohtml", pokemongotemplates.PageData{
|
||||
NavigationLinks: pokemongotemplates.NavigationLinks,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user