golang-lib/web/main.go

61 lines
1.4 KiB
Go
Raw Normal View History

2021-06-01 10:51:35 +02:00
package web
import (
"github.com/bdlm/log"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
// acme
"github.com/gin-gonic/autotls"
"golang.org/x/crypto/acme/autocert"
)
// Service to store Configuration and Webserver wide objects
// (like DB Connection)
type Service struct {
// config
Listen string `toml:"listen"`
AccessLog bool `toml:"access_log"`
Webroot string `toml:"webroot"`
ACME struct {
Enable bool `toml:"enable"`
Domains []string `toml:"domains"`
Cache string `toml:"cache"`
} `toml:"acme"`
Session struct {
Name string `toml:"name"`
Secret string `toml:"secret"`
} `toml:"session"`
// internal
DB *gorm.DB `toml:"-"`
}
// Run to startup all related web parts
// (e.g. configure the server, metrics, and finally bind routing)
func (config *Service) Run() error {
gin.EnableJsonDecoderDisallowUnknownFields()
gin.SetMode(gin.ReleaseMode)
r := gin.New()
// catch crashed
r.Use(gin.Recovery())
if config.AccessLog {
r.Use(gin.Logger())
log.Debug("request logging enabled")
}
config.LoadSession(r)
config.Bind(r)
if config.ACME.Enable {
if config.Listen != "" {
log.Panic("For ACME / Let's Encrypt it is not possible to set `listen`")
}
m := autocert.Manager{
Prompt: autocert.AcceptTOS,
HostPolicy: autocert.HostWhitelist(config.ACME.Domains...),
Cache: autocert.DirCache(config.ACME.Cache),
}
return autotls.RunWithManager(r, &m)
}
2021-06-01 17:41:05 +02:00
return r.Run(config.Listen)
2021-06-01 10:51:35 +02:00
}