aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: c1ebcef31cc0dd3a774c72c8994ed181c8f41d84 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main

import (
	"log"
	"math/rand"
	"time"

	"github.com/FChannel0/FChannel-Server/activitypub"
	"github.com/FChannel0/FChannel-Server/config"
	"github.com/FChannel0/FChannel-Server/db"
	"github.com/FChannel0/FChannel-Server/routes"
	"github.com/FChannel0/FChannel-Server/util"
	"github.com/FChannel0/FChannel-Server/webfinger"
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/encryptcookie"
	"github.com/gofiber/fiber/v2/middleware/logger"
	"github.com/gofiber/template/html"

	_ "github.com/lib/pq"
)

func main() {

	Init()

	defer db.Close()

	// Routing and templates
	template := html.New("./views", ".html")
	template.Debug(true)

	routes.TemplateFunctions(template)

	app := fiber.New(fiber.Config{
		AppName: "FChannel",
		Views:   template,
	})

	app.Use(logger.New())

	cookieKey, err := util.GetCookieKey()

	if err != nil {
		log.Println(err)
	}

	app.Use(encryptcookie.New(encryptcookie.Config{
		Key: cookieKey,
	}))

	app.Static("/static", "./views")
	app.Static("/static", "./static")
	app.Static("/public", "./public")

	// Main actor
	app.Get("/", routes.Index)
	app.Post("/inbox", routes.Inbox)
	app.Post("/outbox", routes.Outbox)
	app.Get("/following", routes.Following)
	app.Get("/followers", routes.Followers)

	// Admin routes
	app.Post("/verify", routes.AdminVerify)
	app.Post("/auth", routes.AdminAuth)
	app.All("/"+config.Key+"/", routes.AdminIndex)
	app.Post("/"+config.Key+"/follow", routes.AdminFollow)
	app.Post("/"+config.Key+"/addboard", routes.AdminAddBoard)
	app.Get("/"+config.Key+"/postnews", routes.AdminPostNews)
	app.Get("/"+config.Key+"/newsdelete", routes.AdminNewsDelete)
	app.Post("/"+config.Key+"/:actor/follow", routes.AdminActorIndex)
	app.Get("/"+config.Key+"/:actor", routes.AdminActorIndex)
	app.Get("/news", routes.NewsGet)

	// Board managment
	app.Get("/banmedia", routes.BoardBanMedia)
	app.Get("/delete", routes.BoardDelete)
	app.Get("/deleteattach", routes.BoardDeleteAttach)
	app.Get("/marksensitive", routes.BoardMarkSensitive)
	app.Get("/remove", routes.BoardRemove)
	app.Get("/removeattach", routes.BoardRemoveAttach)
	app.Get("/addtoindex", routes.BoardAddToIndex)
	app.Get("/poparchive", routes.BoardPopArchive)
	app.Get("/autosubscribe", routes.BoardAutoSubscribe)
	app.Get("/blacklist", routes.BoardBlacklist)
	app.Get("/report", routes.BoardBlacklist)
	app.Get("/.well-known/webfinger", routes.Webfinger)
	app.Get("/api/media", routes.Media)

	// Board actor
	app.Get("/:actor/catalog", routes.CatalogGet)
	app.Post("/:actor/inbox", routes.ActorInbox)
	app.Post("/:actor/outbox", routes.ActorOutbox)
	app.Get("/:actor/following", routes.ActorFollowing)
	app.All("/:actor/followers", routes.ActorFollowers)
	app.Get("/:actor/reported", routes.ActorReported)
	app.Get("/:actor/archive", routes.ActorArchive)
	app.Get("/:actor", routes.OutboxGet)
	app.Post("/:actor", routes.ActorPost)
	app.Get("/:actor/:post", routes.PostGet)

	//404 handler
	app.Use(routes.NotFound)

	db.PrintAdminAuth()

	app.Listen(config.Port)
}

func Init() {
	var err error

	rand.Seed(time.Now().UnixNano())

	util.CreatedNeededDirectories()

	db.ConnectDB()

	db.RunDatabaseSchema()

	actor, _ := activitypub.GetActorFromDB(config.Domain)
	webfinger.FollowingBoards, err = actor.GetFollowing()

	if err != nil {
		panic(err)
	}

	webfinger.Boards, err = webfinger.GetBoardCollection()

	if err != nil {
		panic(err)
	}

	config.Key = util.CreateKey(32)

	go db.MakeCaptchas(100)

	go db.StartupArchive()

	go db.CheckInactive()

	db.InitInstance()

	util.LoadThemes()
}