hydra-werther/cmd/werther/main.go

94 lines
2.6 KiB
Go
Raw Normal View History

2019-02-18 14:57:54 +01:00
/*
2019-05-24 15:13:15 +02:00
Copyright (c) JSC iCore.
2019-02-18 14:57:54 +01:00
2019-05-24 15:13:15 +02:00
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
2019-02-18 14:57:54 +01:00
*/
2019-05-24 15:13:15 +02:00
package main // import "github.com/i-core/werther/cmd/werther"
2019-02-18 14:57:54 +01:00
import (
"flag"
"fmt"
"net/http"
2019-08-06 13:19:02 +02:00
"net/url"
2019-02-18 14:57:54 +01:00
"os"
2019-05-24 15:13:15 +02:00
"github.com/i-core/rlog"
"github.com/i-core/routegroup"
"github.com/i-core/werther/internal/identp"
"github.com/i-core/werther/internal/ldapclient"
"github.com/i-core/werther/internal/stat"
"github.com/i-core/werther/internal/web"
2019-05-15 14:03:05 +02:00
"github.com/justinas/nosurf"
2019-02-18 14:57:54 +01:00
"github.com/kelseyhightower/envconfig"
"go.uber.org/zap"
)
2019-05-24 15:13:15 +02:00
// version will be filled at compile time.
var version = ""
2019-05-15 14:03:05 +02:00
// Config is a server's configuration.
type Config struct {
DevMode bool `envconfig:"dev_mode" default:"false" desc:"a development mode"`
Listen string `default:":8080" desc:"a host and port to listen on (<host>:<port>)"`
Identp identp.Config
LDAP ldapclient.Config
2019-05-24 15:13:15 +02:00
Web web.Config
2019-05-15 14:03:05 +02:00
}
2019-02-18 14:57:54 +01:00
func main() {
flag.Usage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
fmt.Fprintf(flag.CommandLine.Output(), "\n")
2019-05-15 14:03:05 +02:00
if err := envconfig.Usagef("werther", &Config{}, flag.CommandLine.Output(), envconfig.DefaultListFormat); err != nil {
2019-02-18 14:57:54 +01:00
panic(err)
}
}
verflag := flag.Bool("version", false, "print a version")
flag.Parse()
if *verflag {
2019-05-24 15:13:15 +02:00
fmt.Println("werther", version)
2019-02-18 14:57:54 +01:00
os.Exit(0)
}
2019-05-15 14:03:05 +02:00
var cnf Config
2019-02-18 14:57:54 +01:00
if err := envconfig.Process("werther", &cnf); err != nil {
fmt.Fprintf(os.Stderr, "Invalid configuration: %s\n", err)
os.Exit(1)
}
2019-08-06 13:19:02 +02:00
if _, ok := cnf.Identp.ClaimScopes[url.QueryEscape(cnf.LDAP.RoleClaim)]; !ok {
fmt.Fprintf(os.Stderr, "Roles claim %q has no mapping to an OpenID Connect scope\n", cnf.LDAP.RoleClaim)
os.Exit(1)
}
2019-02-18 14:57:54 +01:00
logFunc := zap.NewProduction
if cnf.DevMode {
logFunc = zap.NewDevelopment
}
log, err := logFunc()
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create logger: %s\n", err)
os.Exit(1)
}
2019-05-15 14:03:05 +02:00
htmlRenderer, err := web.NewHTMLRenderer(cnf.Web)
2019-02-18 14:57:54 +01:00
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to start the server: %s\n", err)
os.Exit(1)
}
2019-05-15 14:03:05 +02:00
ldap := ldapclient.New(cnf.LDAP)
2019-05-24 15:13:15 +02:00
router := routegroup.NewRouter(nosurf.NewPure, rlog.NewMiddleware(log))
2019-05-15 14:03:05 +02:00
router.AddRoutes(web.NewStaticHandler(cnf.Web), "/static")
router.AddRoutes(identp.NewHandler(cnf.Identp, ldap, htmlRenderer), "/auth")
2019-05-24 15:13:15 +02:00
router.AddRoutes(stat.NewHandler(version), "/stat")
2019-05-15 14:03:05 +02:00
2019-02-18 14:57:54 +01:00
log = log.Named("main")
2019-05-24 15:13:15 +02:00
log.Info("Werther started", zap.Any("config", cnf), zap.String("version", version))
2019-05-15 14:03:05 +02:00
log.Fatal("Werther finished", zap.Error(http.ListenAndServe(cnf.Listen, router)))
2019-02-18 14:57:54 +01:00
}