66 lines
1.1 KiB
Go
66 lines
1.1 KiB
Go
|
package hn
|
||
|
|
||
|
import (
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/jinzhu/gorm"
|
||
|
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||
|
"github.com/labstack/echo"
|
||
|
"github.com/labstack/echo/middleware"
|
||
|
"github.com/labstack/gommon/log"
|
||
|
"github.com/ziflex/lecho/v2"
|
||
|
)
|
||
|
|
||
|
// required for orm
|
||
|
|
||
|
type App struct {
|
||
|
version string
|
||
|
buildarch string
|
||
|
e *echo.Echo
|
||
|
logger *string
|
||
|
db *gorm.DB
|
||
|
startup time.Time
|
||
|
}
|
||
|
|
||
|
func RunServer(version string, buildarch string) int {
|
||
|
a := new(App)
|
||
|
a.version = version
|
||
|
a.buildarch = buildarch
|
||
|
a.startup = time.Now()
|
||
|
a.runForever()
|
||
|
return 0
|
||
|
}
|
||
|
|
||
|
func (a *App) runForever() {
|
||
|
|
||
|
// Echo instance
|
||
|
a.e = echo.New()
|
||
|
|
||
|
lev := log.INFO
|
||
|
if os.Getenv("DEBUG") != "" {
|
||
|
lev = log.DEBUG
|
||
|
}
|
||
|
|
||
|
logger := lecho.New(
|
||
|
os.Stdout,
|
||
|
lecho.WithLevel(lev),
|
||
|
lecho.WithTimestamp(),
|
||
|
lecho.WithCaller(),
|
||
|
)
|
||
|
a.e.Logger = logger
|
||
|
a.e.Use(middleware.RequestID())
|
||
|
|
||
|
// Middleware
|
||
|
a.e.Use(middleware.Logger())
|
||
|
a.e.Use(middleware.Recover())
|
||
|
|
||
|
a.e.Renderer = NewTemplate("./view/")
|
||
|
|
||
|
// Routes
|
||
|
a.e.GET("/", indexHandler)
|
||
|
|
||
|
// Start server
|
||
|
a.e.Logger.Fatal(a.e.Start(":8080"))
|
||
|
}
|