feat: automate demo-site deployment and configuration (#42)

* add demo env variable

* setup initialization when demo

* disable password when in demo mode

* expose demo status to API

* improve UI for demo instance
This commit is contained in:
Hayden 2022-10-12 12:53:22 -08:00 committed by GitHub
parent eca071f974
commit 92368dabf8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 166 additions and 45 deletions

62
backend/app/api/demo.go Normal file
View file

@ -0,0 +1,62 @@
package main
import (
"context"
"encoding/csv"
"strings"
"github.com/hay-kot/homebox/backend/internal/services"
"github.com/rs/zerolog/log"
)
func (a *app) SetupDemo() {
csvText := `Import Ref,Location,Labels,Quantity,Name,Description,Insured,Serial Number,Model Number,Manufacturer,Notes,Purchase From,Purchased Price,Purchased Time,Lifetime Warranty,Warranty Expires,Warranty Details,Sold To,Sold Price,Sold Time,Sold Notes
,Garage,IOT;Home Assistant; Z-Wave,1,Zooz Universal Relay ZEN17,"Zooz 700 Series Z-Wave Universal Relay ZEN17 for Awnings, Garage Doors, Sprinklers, and More | 2 NO-C-NC Relays (20A, 10A) | Signal Repeater | Hub Required (Compatible with SmartThings and Hubitat)",,,ZEN17,Zooz,,Amazon,39.95,10/13/2021,,,,,,,
,Living Room,IOT;Home Assistant; Z-Wave,1,Zooz Motion Sensor,"Zooz Z-Wave Plus S2 Motion Sensor ZSE18 with Magnetic Mount, Works with Vera and SmartThings",,,ZSE18,Zooz,,Amazon,29.95,10/15/2021,,,,,,,
,Office,IOT;Home Assistant; Z-Wave,1,Zooz 110v Power Switch,"Zooz Z-Wave Plus Power Switch ZEN15 for 110V AC Units, Sump Pumps, Humidifiers, and More",,,ZEN15,Zooz,,Amazon,39.95,10/13/2021,,,,,,,
,Downstairs,IOT;Home Assistant; Z-Wave,1,Ecolink Z-Wave PIR Motion Sensor,"Ecolink Z-Wave PIR Motion Detector Pet Immune, White (PIRZWAVE2.5-ECO)",,,PIRZWAVE2.5-ECO,Ecolink,,Amazon,35.58,10/21/2020,,,,,,,
,Entry,IOT;Home Assistant; Z-Wave,1,Yale Security Touchscreen Deadbolt,"Yale Security YRD226-ZW2-619 YRD226ZW2619 Touchscreen Deadbolt, Satin Nickel",,,YRD226ZW2619,Yale,,Amazon,120.39,10/14/2020,,,,,,,
,Kitchen,IOT;Home Assistant; Z-Wave,1,Smart Rocker Light Dimmer,"UltraPro Z-Wave Smart Rocker Light Dimmer with QuickFit and SimpleWire, 3-Way Ready, Compatible with Alexa, Google Assistant, ZWave Hub Required, Repeater/Range Extender, White Paddle Only, 39351",,,39351,Honeywell,,Amazon,65.98,09/30/0202,,,,,,,
`
var (
registration = services.UserRegistration{
Email: "demo@email.com",
Name: "Demo",
Password: "demo",
}
)
// First check if we've already setup a demo user and skip if so
_, err := a.services.User.Login(context.Background(), registration.Email, registration.Password)
if err == nil {
return
}
_, err = a.services.User.RegisterUser(context.Background(), registration)
if err != nil {
log.Err(err).Msg("Failed to register demo user")
log.Fatal().Msg("Failed to setup demo")
}
token, _ := a.services.User.Login(context.Background(), registration.Email, registration.Password)
self, _ := a.services.User.GetSelf(context.Background(), token.Raw)
// Read CSV Text
reader := csv.NewReader(strings.NewReader(csvText))
reader.Comma = ','
records, err := reader.ReadAll()
if err != nil {
log.Err(err).Msg("Failed to read CSV")
log.Fatal().Msg("Failed to setup demo")
}
err = a.services.Items.CsvImport(context.Background(), self.GroupID, records)
if err != nil {
log.Err(err).Msg("Failed to import CSV")
log.Fatal().Msg("Failed to setup demo")
}
log.Info().Msg("Demo setup complete")
}

View file

@ -1580,6 +1580,9 @@ const docTemplate = `{
"build": {
"$ref": "#/definitions/v1.Build"
},
"demo": {
"type": "boolean"
},
"health": {
"type": "boolean"
},

View file

@ -1572,6 +1572,9 @@
"build": {
"$ref": "#/definitions/v1.Build"
},
"demo": {
"type": "boolean"
},
"health": {
"type": "boolean"
},

View file

@ -335,6 +335,8 @@ definitions:
properties:
build:
$ref: '#/definitions/v1.Build'
demo:
type: boolean
health:
type: boolean
message:

View file

@ -144,5 +144,11 @@ func run(cfg *config.Config) error {
}
})
// TODO: Remove through external API that does setup
if cfg.Demo {
log.Info().Msg("Running in demo mode, creating demo data")
app.SetupDemo()
}
return app.server.Start(routes)
}

View file

@ -42,8 +42,10 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
// API Version 1
v1Base := v1.BaseUrlFunc(prefix)
v1Ctrl := v1.NewControllerV1(a.services, v1.WithMaxUploadSize(a.conf.Web.MaxUploadSize))
{
v1Ctrl := v1.NewControllerV1(a.services,
v1.WithMaxUploadSize(a.conf.Web.MaxUploadSize),
v1.WithDemoStatus(a.conf.Demo), // Disable Password Change in Demo Mode
)
r.Get(v1Base("/status"), v1Ctrl.HandleBase(func() bool { return true }, v1.Build{
Version: Version,
Commit: Commit,
@ -93,7 +95,6 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux {
r.Put(v1Base("/items/{id}/attachments/{attachment_id}"), v1Ctrl.HandleItemAttachmentUpdate())
r.Delete(v1Base("/items/{id}/attachments/{attachment_id}"), v1Ctrl.HandleItemAttachmentDelete())
})
}
r.NotFound(notFoundHandler())
return r

View file

@ -13,9 +13,16 @@ func WithMaxUploadSize(maxUploadSize int64) func(*V1Controller) {
}
}
func WithDemoStatus(demoStatus bool) func(*V1Controller) {
return func(ctrl *V1Controller) {
ctrl.isDemo = demoStatus
}
}
type V1Controller struct {
svc *services.AllServices
maxUploadSize int64
isDemo bool
}
type (
@ -30,7 +37,8 @@ type (
Versions []string `json:"versions"`
Title string `json:"title"`
Message string `json:"message"`
Build Build
Build Build `json:"build"`
Demo bool `json:"demo"`
}
)
@ -48,6 +56,10 @@ func NewControllerV1(svc *services.AllServices, options ...func(*V1Controller))
svc: svc,
}
for _, opt := range options {
opt(ctrl)
}
return ctrl
}
@ -66,6 +78,7 @@ func (ctrl *V1Controller) HandleBase(ready ReadyFunc, build Build) http.HandlerF
Title: "Go API Template",
Message: "Welcome to the Go API Template Application!",
Build: build,
Demo: ctrl.isDemo,
})
}
}

View file

@ -136,6 +136,11 @@ type (
// @Security Bearer
func (ctrl *V1Controller) HandleUserSelfChangePassword() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if ctrl.isDemo {
server.RespondError(w, http.StatusForbidden, nil)
return
}
var cp ChangePassword
err := server.Decode(r, &cp)
if err != nil {

View file

@ -23,6 +23,7 @@ type Config struct {
Log LoggerConf `yaml:"logger"`
Mailer MailerConf `yaml:"mailer"`
Swagger SwaggerConf `yaml:"swagger"`
Demo bool `yaml:"demo"`
}
type SwaggerConf struct {

View file

@ -8,6 +8,7 @@ processes = []
[env]
PORT = "7745"
HBOX_DEMO = "true"
[experimental]
allowed_public_ports = []

View file

@ -227,6 +227,7 @@ export interface UserRegistration {
export interface ApiSummary {
build: Build;
demo: boolean;
health: boolean;
message: string;
title: string;

View file

@ -11,6 +11,24 @@
const api = usePublicApi();
const toast = useNotifier();
const { data: status } = useAsyncData(async () => {
const { data } = await api.status();
if (data) {
console.log(data);
username.value = "demo@email.com";
password.value = "demo";
}
return data;
});
whenever(status, status => {
if (status?.demo) {
email.value = "demo@email.com";
loginPassword.value = "demo";
}
});
const authStore = useAuthStore();
if (!authStore.isTokenExpired) {
navigateTo("/home");
@ -178,6 +196,11 @@
<Icon name="heroicons-user" class="mr-1 w-7 h-7" />
Login
</h2>
<template v-if="status && status.demo">
<p class="text-xs italic text-center">This is a demo instance</p>
<p class="text-xs text-center"><b>Email</b> demo@email.com</p>
<p class="text-xs text-center"><b>Password</b> demo</p>
</template>
<FormTextField v-model="email" label="Email" />
<FormTextField v-model="loginPassword" label="Password" type="password" />
<div class="card-actions justify-end mt-2">