-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (57 loc) · 1.64 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
package main
import (
"fmt"
"net/http"
"github.com/0xmuralik/file-share/database"
"github.com/0xmuralik/file-share/file"
"github.com/0xmuralik/file-share/user"
fiber "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/jinzhu/gorm"
)
func routes(app *fiber.App) {
app.Get("/home", user.Home)
userAPI := app.Group("/user")
userAPI.Post("/login", user.LogIn)
userAPI.Post("/logout", user.LogOut)
userAPI.Post("/register", user.Register)
fileAPI := app.Group("/file")
fileAPI.Get("/id/:file_id", file.GetFileById)
fileAPI.Get("/name/:name", file.GetFileByName)
fileAPI.Get("/:owner", file.GetFiles)
fileAPI.Post("/new", file.NewFile)
fileAPI.Delete("/:file_id", file.DeleteFile)
}
func initDatabase() {
var err error
database.FileDBConn, err = gorm.Open("sqlite3", "files.db")
if err != nil {
panic("failed to connect file database")
}
fmt.Println("connected to file database")
database.FileDBConn.AutoMigrate(&file.File{})
fmt.Println("file database migrated")
database.UserDBConn, err = gorm.Open("sqlite3", "users.db")
if err != nil {
panic("failed to connect user database")
}
fmt.Println("connected to user database")
database.UserDBConn.AutoMigrate(&user.User{})
fmt.Println("user database migrated")
user.Store = session.New()
fmt.Println("Initialized session store")
}
func main() {
app := fiber.New()
app.Use(filesystem.New(filesystem.Config{
Root: http.Dir("./templates"),
Browse: true,
}))
initDatabase()
defer database.FileDBConn.Close()
routes(app)
if err := app.Listen(":3000"); err != nil {
panic(err)
}
}