85 lines
1.8 KiB
Go
85 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"example.com/henne/client"
|
|
"example.com/henne/templates"
|
|
"github.com/gin-gonic/gin"
|
|
flag "github.com/spf13/pflag"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func main() {
|
|
flag.String("token", "", "Vault Token to use")
|
|
_ = viper.BindPFlags(flag.CommandLine)
|
|
|
|
flag.Parse()
|
|
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))
|
|
viper.AutomaticEnv()
|
|
client.Init(viper.GetString("token"))
|
|
router := gin.New()
|
|
router.GET("/", index)
|
|
router.GET("/bootstrap.min.css", bs)
|
|
router.POST("/", save)
|
|
router.Run("[::]:3000")
|
|
}
|
|
|
|
func getUsers() (Users, error) {
|
|
users := map[string]User{}
|
|
ctx := context.Background()
|
|
l, err := client.Client.List(ctx, "/maschinenlock")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if keys, ok := l.Data["keys"]; ok {
|
|
if list, ok := keys.([]interface{}); ok {
|
|
for _, s := range list {
|
|
u := User{}
|
|
u.Get(s.(string))
|
|
users[s.(string)] = u
|
|
}
|
|
}
|
|
}
|
|
return users, err
|
|
}
|
|
func bs(ctx *gin.Context) {
|
|
ctx.FileFromFS("templates/bootstrap.min.css", http.FS(templates.Res))
|
|
}
|
|
|
|
func index(ctx *gin.Context) {
|
|
users, err := getUsers()
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
ctx.String(http.StatusInternalServerError, "text", "Internal Error")
|
|
return
|
|
}
|
|
templates.Templates.ExecuteTemplate(ctx.Writer, "index", users)
|
|
}
|
|
|
|
func save(ctx *gin.Context) {
|
|
var form User
|
|
if err := ctx.ShouldBind(&form); err != nil {
|
|
log.Print(err)
|
|
ctx.String(http.StatusInternalServerError, "could not bind data")
|
|
return
|
|
}
|
|
err := form.Save(form.ID)
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
ctx.String(http.StatusInternalServerError, "text", "Internal Error")
|
|
return
|
|
}
|
|
|
|
users, err := getUsers()
|
|
if err != nil {
|
|
log.Printf("%v", err)
|
|
ctx.String(http.StatusInternalServerError, "text", "Internal Error")
|
|
return
|
|
}
|
|
templates.Templates.ExecuteTemplate(ctx.Writer, "index", users)
|
|
}
|