-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (100 loc) · 2.69 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"fiberdemo/database"
"fmt"
"log"
"time"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/gofiber/template/html/v2"
)
// RenderForm renders the HTML form.
func RenderForm(c *fiber.Ctx) error {
session, err := Store.Get(c)
if err != nil {
println(err)
}
email := session.Get(EMAIL)
fmt.Println(email)
if email == nil {
return c.Render("login", fiber.Map{})
}
books, err := database.GetAllBookList()
if err != nil {
log.Fatal("GetAllBookList ERROR => ", err)
}
return c.Render("form", fiber.Map{"bookList": books})
}
// RenderLoginForm renders the HTML form.
func RenderLoginForm(c *fiber.Ctx) error {
// books, err := database.GetAllBookList()
// if err != nil {
// log.Fatal("GetAllBookList ERROR => ", err)
// }
return c.Render("login", fiber.Map{})
}
// Process processes the form submission for Login
func ProcessLoginCheck(c *fiber.Ctx) error {
session, err := Store.Get(c)
if err != nil {
println(err)
}
email := c.FormValue("email")
password := c.FormValue("password")
if email == "devjethava909@gmail.com" && password == "Dev@123" {
session.Set(EMAIL, email)
fmt.Println(session.Get("email"))
greeting := fmt.Sprintf("Hello, %s!", email)
err = session.Save()
if err != nil {
fmt.Println(err)
}
return c.Render("greeting", fiber.Map{"Greeting": greeting})
}
// println(email, password)
// greeting := fmt.Sprintf("Hello, %s!", name)
return c.Render("login", fiber.Map{
"Error": "Username/password incorrect",
})
}
// ProcessForm processes the form submission.
func ProcessForm(c *fiber.Ctx) error {
name := c.FormValue("name")
greeting := fmt.Sprintf("Hello, %s!", name)
return c.Render("greeting", fiber.Map{"Greeting": greeting})
}
func GetAllBook(c *fiber.Ctx) error {
books, err := database.GetAllBookList()
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(err.Error())
}
// log.Println("book data All: ", books)
return c.Status(fiber.StatusOK).JSON(books)
}
var (
Store *session.Store
AUTH_KEY string = "authenticated"
EMAIL string = "email"
)
func main() {
database.ConnectDb()
app := fiber.New(fiber.Config{
Views: html.New("./views", ".html"),
})
/* Sessions Config */
Store = session.New(session.Config{
CookieHTTPOnly: true,
// CookieSecure: true, for https
Expiration: time.Hour * 1,
})
// Serve static files (HTML templates and stylesheets).
app.Static("/", "./static")
// Define routes.
app.Get("/", RenderForm)
app.Get("/login", RenderLoginForm)
app.Post("/loginCheck", ProcessLoginCheck)
app.Post("/submit", ProcessForm)
app.Get("/api/v1/books", GetAllBook)
// Start the Fiber app on port 8080.
app.Listen(":3080")
}