-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made structure simpler and fixed docker builds
- Loading branch information
Showing
7 changed files
with
118 additions
and
177 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,136 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"html/template" | ||
"os" | ||
"time" | ||
|
||
"github.com/alecthomas/kong" | ||
"github.com/gin-contrib/gzip" | ||
"github.com/gin-gonic/gin" | ||
"github.com/gnur/tobab" | ||
"github.com/gnur/tobab/storm" | ||
"github.com/go-webauthn/webauthn/webauthn" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type Globals struct { | ||
Debug bool | ||
Config string `help:"config location" type:"existingfile" short:"c"` | ||
} | ||
var version = "manual build" | ||
|
||
type RunCmd struct { | ||
type Tobab struct { | ||
fqdn string | ||
config tobab.Config | ||
logger *logrus.Entry | ||
maxAge time.Duration | ||
defaultAge time.Duration | ||
templates *template.Template | ||
confLoc string | ||
db tobab.Database | ||
webauthn *webauthn.WebAuthn | ||
} | ||
|
||
func (r *RunCmd) Run(ctx *Globals) error { | ||
run(ctx.Config) | ||
return errors.New("Server exited") | ||
} | ||
func main() { | ||
logger := logrus.New() | ||
logger.SetFormatter(&logrus.TextFormatter{ | ||
ForceColors: true, | ||
FullTimestamp: true, | ||
}) | ||
|
||
type ValidateCmd struct { | ||
} | ||
confLoc := os.Getenv("TOBAB_TOML") | ||
if confLoc == "" { | ||
confLoc = "/etc/tobab/tobab.toml" | ||
} | ||
|
||
func (r *ValidateCmd) Run(ctx *Globals) error { | ||
_, err := tobab.LoadConf(ctx.Config) | ||
if err == nil { | ||
fmt.Println("Config ok") | ||
cfg, err := tobab.LoadConf(confLoc) | ||
if err != nil { | ||
logger.WithError(err).Fatal("Failed loading config") | ||
} | ||
|
||
return err | ||
} | ||
if lvl, err := logrus.ParseLevel(cfg.Loglevel); err == nil { | ||
logger.SetLevel(lvl) | ||
} | ||
|
||
type VersionCmd struct { | ||
} | ||
if version == "" { | ||
version = "unknown" | ||
} | ||
|
||
func (r *VersionCmd) Run(ctx *Globals) error { | ||
fmt.Println(version) | ||
return nil | ||
} | ||
db, err := storm.New(cfg.DatabasePath) | ||
if err != nil { | ||
logger.WithError(err).WithField("location", cfg.DatabasePath).Fatal("Unable to initialize database") | ||
} | ||
defer db.Close() | ||
|
||
var cli struct { | ||
Globals | ||
wconfig := &webauthn.Config{ | ||
RPDisplayName: cfg.Displayname, | ||
RPID: cfg.CookieScope, | ||
RPOrigins: []string{"https://" + cfg.Hostname}, | ||
} | ||
|
||
w, err := webauthn.New(wconfig) | ||
if err != nil { | ||
logger.WithError(err).Fatal("Unable to initialize webauthn") | ||
} | ||
|
||
app := Tobab{ | ||
config: cfg, | ||
logger: logger.WithField("version", version), | ||
maxAge: 720 * time.Hour, | ||
fqdn: "https://" + cfg.Hostname, | ||
confLoc: confLoc, | ||
db: db, | ||
webauthn: w, | ||
} | ||
|
||
//check if admin is created already, otherwise set it to false | ||
hasAdmin, err := app.db.KVGetBool(ADMIN_REGISTERED_KEY) | ||
if err != nil || !hasAdmin { | ||
logger.Warning("Setting flag so first user to register will be admin") | ||
app.db.KVSet(ADMIN_REGISTERED_KEY, false) | ||
} | ||
|
||
if age, err := time.ParseDuration(cfg.DefaultTokenAge); err != nil { | ||
app.defaultAge = 720 * time.Hour | ||
} else { | ||
app.defaultAge = age | ||
} | ||
|
||
if age, err := time.ParseDuration(cfg.MaxTokenAge); err != nil { | ||
app.maxAge = 24 * 365 * time.Hour | ||
} else { | ||
app.maxAge = age | ||
} | ||
|
||
app.templates, err = loadTemplates() | ||
if err != nil { | ||
logger.WithError(err).Fatal("unable to load templates") | ||
} | ||
app.startServer() | ||
|
||
Run RunCmd `cmd:"" help:"start tobab server"` | ||
Validate ValidateCmd `cmd:"" help:"validate tobab config"` | ||
Version VersionCmd `cmd:"" help:"print tobab version"` | ||
} | ||
|
||
func main() { | ||
ctx := kong.Parse(&cli, kong.UsageOnError()) | ||
err := ctx.Run(&Globals{ | ||
Debug: cli.Debug, | ||
Config: cli.Config, | ||
}) | ||
ctx.FatalIfErrorf(err) | ||
func (app *Tobab) startServer() { | ||
app.logger.Info("starting server") | ||
|
||
if app.config.Dev { | ||
gin.SetMode(gin.DebugMode) | ||
} else { | ||
gin.SetMode(gin.ReleaseMode) | ||
} | ||
|
||
r := gin.Default() | ||
|
||
if app.config.Dev { | ||
gin.SetMode(gin.DebugMode) | ||
r.LoadHTMLGlob("cmd/tobab/templates/*.html") | ||
} else { | ||
gin.SetMode(gin.ReleaseMode) | ||
r.SetHTMLTemplate(app.templates) | ||
} | ||
|
||
r.Use(gzip.Gzip(gzip.DefaultCompression)) | ||
r.Use(app.getSessionMiddleware()) | ||
app.setTobabRoutes(r) | ||
|
||
err := r.Run() | ||
if err != nil { | ||
app.logger.WithError(err).Error("Failed to start web server") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters