Skip to content

Commit

Permalink
Made structure simpler and fixed docker builds
Browse files Browse the repository at this point in the history
  • Loading branch information
gnur committed Jun 12, 2023
1 parent 055abdb commit e34e476
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ COPY go.* /workdir/
RUN go mod download
COPY . /workdir/

RUN CGO_ENABLED=0 go build -ldflags="-s -w" -a -installsuffix cgo -o tobab .
RUN CGO_ENABLED=0 go build -ldflags="-s -w" -o tobab ./cmd/tobab/


FROM gcr.io/distroless/static-debian11
Expand Down
2 changes: 2 additions & 0 deletions bin/dev
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

export TOBAB_TOML="config.toml"

air &

npx tailwindcss -i ./css/in.css -o ./cmd/tobab/static/main.css --watch
Expand Down
153 changes: 115 additions & 38 deletions cmd/tobab/main.go
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")
}
}
130 changes: 0 additions & 130 deletions cmd/tobab/server.go

This file was deleted.

1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ go 1.20

require (
github.com/BurntSushi/toml v1.3.0
github.com/alecthomas/kong v0.7.1
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2
github.com/asdine/storm v2.1.2+incompatible
github.com/gin-contrib/gzip v0.0.6
Expand Down
5 changes: 0 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ github.com/DataDog/zstd v1.4.5/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/Sereal/Sereal v0.0.0-20200820125258-a016b7cda3f3 h1:XgiXcABXIRyuLNyKHIk6gICrVXcGooDUxR+XMRr2QDM=
github.com/Sereal/Sereal v0.0.0-20200820125258-a016b7cda3f3/go.mod h1:D0JMgToj/WdxCgd30Kc1UcA9E+WdZoJqeVOuYW7iTBM=
github.com/alecthomas/assert/v2 v2.1.0 h1:tbredtNcQnoSd3QBhQWI7QZ3XHOVkw1Moklp2ojoH/0=
github.com/alecthomas/kong v0.7.1 h1:azoTh0IOfwlAX3qN9sHWTxACE2oV8Bg2gAwBsMwDQY4=
github.com/alecthomas/kong v0.7.1/go.mod h1:n1iCIO2xS46oE8ZfYCNDqdR0b0wZNrXAIAqro/2132U=
github.com/alecthomas/repr v0.1.0 h1:ENn2e1+J3k09gyj2shc0dHr/yjaWSHRlrJ4DPMevDqE=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
Expand Down Expand Up @@ -122,7 +118,6 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmg
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
github.com/grpc-ecosystem/grpc-gateway v1.9.0/go.mod h1:vNeuVxBJEsws4ogUvrchl83t/GYV9WGTSLVdBhOQFDY=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
Expand Down
2 changes: 0 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ type Config struct {
DefaultTokenAge string
MaxTokenAge string
CookieScope string `valid:"required"`
Secret string `valid:"required"`
Salt string `valid:"required"`
Loglevel string
DatabasePath string `valid:"required"`
AdminGlobs []Glob `valid:"required"`
Expand Down

0 comments on commit e34e476

Please sign in to comment.