-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
86 lines (79 loc) · 2.7 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"log"
"os"
"github.com/thanhpk/randstr"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/bcrypt"
"github.com/caris-events/tunalog/handler"
"github.com/caris-events/tunalog/store"
"github.com/caris-events/tunalog/system"
)
func main() {
fmt.Println(`
████████╗██╗ ██╗███╗ ██╗ █████╗ ██╗ ██████╗ ██████╗
╚══██╔══╝██║ ██║████╗ ██║██╔══██╗██║ ██╔═══██╗██╔════╝
██║ ██║ ██║██╔██╗ ██║███████║██║ ██║ ██║██║ ███╗
██║ ╚██████╔╝██║ ╚████║██║ ██║███████╗╚██████╔╝╚██████╔╝
╚═╝ ╚═════╝ ╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝ ╚═════╝ ╚═════╝`)
app := &cli.App{
Name: "tunalog",
Version: system.Version,
Usage: "A simple blogging system written in Golang ✨",
Action: start,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "port",
Usage: "port to listen on",
Aliases: []string{"p"},
Value: ":8080",
},
&cli.StringFlag{
Name: "tls-key",
Usage: "path to TLS key file",
Value: "",
},
&cli.StringFlag{
Name: "tls-crt",
Usage: "path to TLS certificate file",
Value: "",
},
},
Commands: []*cli.Command{
{
Name: "reset-password",
Usage: "reset the password of a user, email address is required ",
Action: resetUser,
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func start(c *cli.Context) error {
if c.String("tls-crt") != "" && c.String("tls-key") != "" {
fmt.Printf("👋 Visit https://localhost%s to use Tunalog\n", c.String("port"))
return handler.Router.RunTLS(c.String("port"), c.String("tls-crt"), c.String("tls-key"))
} else {
fmt.Printf("👋 Visit http://localhost%s to use Tunalog\n", c.String("port"))
return handler.Router.Run(c.String("port"))
}
}
func resetUser(c *cli.Context) error {
u, err := store.GetUserByEmail(c.Args().First())
if err != nil {
return err
}
pwd := randstr.String(16, randstr.Base64Chars)
b, err := bcrypt.GenerateFromPassword([]byte(pwd), bcrypt.DefaultCost)
if err != nil {
return err
}
if err := store.UpdateUserPassword(u.ID, string(b)); err != nil {
return err
}
log.Printf(`Password for user %s has been reset to: "%s"`, u.Email, pwd)
return nil
}