Skip to content

Commit

Permalink
* 🐛 fix(main.go): handle error when creating a new generator
Browse files Browse the repository at this point in the history
* ✨ feat(main.go): add generator import/export functionality
The error handling has been improved when creating a new generator. The generator import/export functionality has been added, which allows the generator's options to be exported and imported into a new generator. This feature improves the flexibility of the application as it allows the generator's options to be easily transferred between instances.

* ♻️ refactor(main.go): rename gen variable to gen2
* 🐛 fix(main.go): fix validation and generation intervals
The variable gen has been renamed to gen2 to improve readability. The validation and generation intervals have been fixed to match the comments in the code. The validation interval is now 3 seconds and the generation interval is removed as it is not needed.
  • Loading branch information
Mobilpadde committed Mar 12, 2023
1 parent d1bb4cc commit bd8df93
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,42 @@ import (
)

func main() {
secret := os.Getenv("MOTHS_SECRET")
amount := 8
secret := os.Getenv("MOTHS_SECRET") // created as specified in the README: https://github.com/Mobilpadde/moths#how-
amount := 8 // we want to generate a code with 8 emojies

validationInterval := time.Second * 3
generationInterval := time.Second * 4

generationTicker := time.NewTicker(generationInterval)
validationTicker := time.NewTicker(validationInterval)

var err error
var gen *token.Generator
if gen, err = token.NewGenerator(
// Instantiate a new generator
gen, err := token.NewGenerator(
option.OptionWithSecret(secret),
option.OptionWithPeriod(generationInterval),
option.OptionWithAmount(amount),
option.OptionWithEmojies(emojies.CATS),
option.OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)),
); err != nil {
)
if err != nil {
log.Fatalln(err)
}

// Check if everything is working
if err := gen.Check(); err != nil {
log.Fatalln(err)
}

// Exports the generator's options
s := gen.Export()

// Instantiate a second generator
gen2, _ := token.NewGenerator()

// Import the encoded options into the new generator
if err = gen2.Import(s); err != nil {
log.Println(err)
}

// Check if everything is still working
if err := gen2.Check(); err != nil {
log.Fatalln(err)
}

Expand All @@ -40,20 +58,26 @@ func main() {
)
log.Printf("Every code is %d emoji long", amount)

// Flow:
//
// 10 Validate a code after 3 seconds (validationInterval)
// 20 Try validating the code again after 3 more seconds (validationInterval)
// 30 Generate a new code
//
// 40 GOTO 10
for {
log.Println()
code, err := gen.Next()
code, err := gen2.Next()
if err != nil {
log.Fatalln(err)
}

log.Printf(`Your code is "%s" and code is %s`, code.SpacedString(), code.Token())
<-validationTicker.C
<-time.NewTicker(validationInterval).C

log.Printf("Is this still valid after %s? %t", validationInterval, gen.Validate(code.String()))
<-validationTicker.C
log.Printf("Is this still valid after %s? %t", validationInterval, gen2.Validate(code.String()))
<-time.NewTicker(validationInterval).C

log.Printf("Is this still valid after %s? %t", validationInterval*2, gen.Validate(code.String()))
<-generationTicker.C
log.Printf("Is this still valid after %s? %t", validationInterval*2, gen2.Validate(code.String()))
}
}

0 comments on commit bd8df93

Please sign in to comment.