aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 9ff4c544dfb83a6ec60cfdaa35174ae1ee32745a (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
145
146
147
148
149
150
151
152
153
154
package main

import (
	"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/route"
	"github.com/FChannel0/FChannel-Server/route/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"
)

func main() {

	Init()

	defer db.Close()

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

	route.TemplateFunctions(template)

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

	app.Use(logger.New())

	cookieKey, err := util.GetCookieKey()

	if err != nil {
		config.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.Get("/"+config.Key+"/follow", routes.AdminFollow)
	app.Post("/"+config.Key+"/addboard", routes.AdminAddBoard)
	app.Get("/"+config.Key+"/newspost", routes.NewsPost)
	app.Get("/"+config.Key+"/newsdelete", routes.NewsDelete)
	app.All("/"+config.Key+"/:actor/follow", routes.AdminFollow)
	app.Get("/"+config.Key+"/:actor", routes.AdminActorIndex)

	// News routes
	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("/addtoindex", routes.BoardAddToIndex)
	app.Get("/poparchive", routes.BoardPopArchive)
	app.Get("/autosubscribe", routes.BoardAutoSubscribe)
	app.Get("/blacklist", routes.BoardBlacklist)
	app.All("/report", routes.BoardReport)

	// Webfinger routes
	app.Get("/.well-known/webfinger", routes.Webfinger)

	// API routes
	app.Get("/api/media", routes.Media)

	// Board actor routes
	app.Get("/:actor/catalog", routes.ActorCatalog)
	app.Post("/:actor/inbox", routes.ActorInbox)
	app.All("/:actor/outbox", routes.ActorOutbox)
	app.Get("/:actor/following", routes.ActorFollowing)
	app.All("/:actor/followers", routes.ActorFollowers)
	app.Get("/:actor/archive", routes.ActorArchive)
	app.Get("/:actor", routes.ActorOutboxGet)
	app.Post("/:actor", routes.ActorPost)
	app.Get("/:actor/:post", routes.ActorPostGet)

	db.PrintAdminAuth()

	app.Listen(config.Port)
}

func Init() {
	var actor activitypub.Actor
	var err error

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

	if err = util.CreatedNeededDirectories(); err != nil {
		config.Log.Println(err)
	}

	if err = db.Connect(); err != nil {
		config.Log.Println(err)
	}

	if err = db.RunDatabaseSchema(); err != nil {
		config.Log.Println(err)
	}

	if err = db.InitInstance(); err != nil {
		config.Log.Println(err)
	}

	if actor, err = activitypub.GetActorFromDB(config.Domain); err != nil {
		config.Log.Println(err)
	}

	if webfinger.FollowingBoards, err = actor.GetFollowing(); err != nil {
		config.Log.Println(err)
	}

	if webfinger.Boards, err = webfinger.GetBoardCollection(); err != nil {
		config.Log.Println(err)
	}

	if config.Key, err = util.CreateKey(32); err != nil {
		config.Log.Println(err)
	}

	if err = util.LoadThemes(); err != nil {
		config.Log.Println(err)
	}

	go webfinger.StartupArchive()

	go util.MakeCaptchas(100)

	go db.CheckInactive()
}