huge refactoring

This commit is contained in:
Julien Neuhart
2018-03-29 20:13:19 +02:00
parent 2f97a44bed
commit e847c86baa
24 changed files with 1039 additions and 783 deletions

59
app/app.go Normal file
View File

@@ -0,0 +1,59 @@
package app
import (
"fmt"
"net/http"
"time"
"github.com/gulien/gotenberg/app/config"
"github.com/gulien/gotenberg/app/handlers"
"github.com/gulien/gotenberg/app/handlers/converter/process"
"github.com/gulien/gotenberg/app/logger"
"github.com/gorilla/mux"
)
type App struct {
version string
config *config.AppConfig
Server *http.Server
}
func NewApp(version string) (*App, error) {
c, err := config.NewAppConfig()
if err != nil {
logger.Error(err)
return nil, &appConfigError{}
}
a := &App{}
a.version = version
a.config = c
// defines our application logging.
logger.SetLevel(a.config.Logs.Level)
logger.SetFormatter(a.config.Logs.Formatter)
// defines our application router.
r := mux.NewRouter()
r.Handle("/", handlers.GetHandlersChain())
a.Server = &http.Server{
Addr: fmt.Sprintf(":%s", a.config.Port),
// good practice to set timeouts to avoid Slowloris attacks.
WriteTimeout: time.Second * 15,
ReadTimeout: time.Second * 15,
IdleTimeout: time.Second * 60,
Handler: r,
}
return a, nil
}
func (a *App) Run() error {
process.Load(a.config.CommandsConfig)
logger.Infof("Gotenberg %s", a.version)
logger.Infof("Application is starting on %s", a.Server.Addr)
return a.Server.ListenAndServe()
}