-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (61 loc) · 2.17 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
package main
import (
"database/sql"
"log"
"os"
"github.com/abhishekshree/iitk-coin/db"
"github.com/abhishekshree/iitk-coin/routes"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/logger"
jwtware "github.com/gofiber/jwt/v2"
_ "github.com/mattn/go-sqlite3"
)
func main() {
DB, _ := sql.Open("sqlite3", "./Users.db")
db.DB = DB
defer DB.Close()
// Creates the table User if it does not exists already.
_, err := DB.Exec("CREATE TABLE IF NOT EXISTS User (rollno TEXT PRIMARY KEY, name TEXT, password TEXT, coins REAL, Admin BOOLEAN DEFAULT 0)")
if err != nil {
log.Fatal(err)
}
log.Println("Table 'User' is Ready!")
// Creates the table Transactions if it does not exists already.
_, err = DB.Exec("CREATE TABLE IF NOT EXISTS Transactions (id INTEGER PRIMARY KEY, from_roll TEXT, to_roll TEXT, type TEXT, timestamp TEXT,amount_before_tax REAL, tax REAL)")
if err != nil {
log.Fatal(err)
}
log.Println("Table 'Transactions' is Ready!")
//Creates the table RedeemRequests if it does not exists already.
_, err = DB.Exec("CREATE TABLE IF NOT EXISTS RedeemRequests (id INTEGER PRIMARY KEY, rollno TEXT, item TEXT, timestamp TEXT, status INTEGER DEFAULT 0)")
// status 0 -> Pending
// status 1 -> Redeemed
// status 2 -> Declined
if err != nil {
log.Fatal(err)
}
log.Println("Table 'RedeemRequests' is Ready!")
// App setup
app := fiber.New()
app.Use(logger.New())
// Routes
app.Get("/", routes.Hello)
app.Post("/signup", routes.Signup)
app.Post("/login", routes.Login)
app.Get("/getCoins", routes.GetCoins)
app.Get("/getRedeemList", routes.GetRedeemList)
// JWT Middleware
app.Use(jwtware.New(jwtware.Config{
SigningKey: []byte("secret"),
}))
// These will need a JWT bearer token.
app.Get("/secretpage", routes.Secret)
app.Post("/awardCoins", routes.AwardCoins)
app.Post("/transferCoins", routes.TransferCoins)
// Redeem part which needs a JWT Token.
app.Post("/redeemRequest", routes.RequestItem)
app.Post("/rejectRedeemRequest", routes.RejectRedeemRequest)
app.Post("/acceptRedeemRequest", routes.AcceptRedeemRequest)
app.Post("/rejectPendingRequests", routes.RejectPendingRequests)
log.Fatal(app.Listen(os.Getenv("PORT")))
}