-
Notifications
You must be signed in to change notification settings - Fork 0
/
passwordgen-demo.go
65 lines (57 loc) · 1.39 KB
/
passwordgen-demo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"log"
"net/http"
"os"
echotemplate "github.com/foolin/echo-template"
passwordgen "github.com/goavega-software/passwordgenerator"
"github.com/labstack/echo"
)
func main() {
e := echo.New()
e.Renderer = echotemplate.Default()
e.Static("/js", "js")
e.Static("/css", "css")
e.GET("/", func(c echo.Context) error {
return c.Render(http.StatusOK, "index.html", echo.Map{
"passwords": "",
})
})
e.POST("/api/generate", func(c echo.Context) error {
var response struct {
Data struct {
Passwords []string `json:"passwords"`
} `json:"data"`
Errors []string `json:"errors"`
}
request := new(passwordgen.GenerateRequest)
if reqErr := c.Bind(request); reqErr != nil {
fmt.Print(reqErr)
response.Errors = append(response.Errors, "Invalid request")
return c.JSON(http.StatusBadRequest, response)
}
i := 0
if request.NumPasswords > 8 {
request.NumPasswords = 8
}
var (
passwords []string
password string
)
for i < request.NumPasswords {
password = passwordgen.Generate(*request)
passwords = append(passwords, password)
i++
}
response.Data.Passwords = passwords
return c.JSON(http.StatusOK, response)
})
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), e))
}