2017-09-13 15:09:13 +00:00
/ *
*
* Gosora Main File
2019-02-23 06:29:19 +00:00
* Copyright Azareal 2016 - 2020
2017-09-13 15:09:13 +00:00
*
* /
2019-01-21 12:27:59 +00:00
// Package main contains the main initialisation logic for Gosora
2018-10-27 03:21:02 +00:00
package main // import "github.com/Azareal/Gosora"
2016-12-02 07:38:54 +00:00
import (
2018-04-22 12:33:56 +00:00
"bytes"
2018-07-26 06:15:49 +00:00
"crypto/tls"
2018-03-21 05:56:33 +00:00
"flag"
2017-05-07 08:31:41 +00:00
"fmt"
2016-12-17 03:39:53 +00:00
"io"
2017-09-03 04:50:31 +00:00
"log"
2021-01-02 21:46:24 +00:00
"mime"
2017-09-03 04:50:31 +00:00
"net/http"
2017-08-13 11:22:34 +00:00
"os"
2017-11-29 02:34:02 +00:00
"os/signal"
2018-11-17 02:36:02 +00:00
"runtime"
2021-05-05 09:23:58 +00:00
"runtime/debug"
2018-08-22 01:32:07 +00:00
"runtime/pprof"
2019-02-10 05:52:26 +00:00
"strconv"
2017-12-01 02:04:29 +00:00
"strings"
2017-11-29 02:34:02 +00:00
"syscall"
2017-09-03 04:50:31 +00:00
"time"
2018-02-19 04:26:01 +00:00
2019-04-19 08:20:10 +00:00
c "github.com/Azareal/Gosora/common"
2019-10-06 00:34:09 +00:00
co "github.com/Azareal/Gosora/common/counters"
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
meta "github.com/Azareal/Gosora/common/meta"
2020-02-23 09:27:35 +00:00
p "github.com/Azareal/Gosora/common/phrases"
_ "github.com/Azareal/Gosora/extend"
qgen "github.com/Azareal/Gosora/query_gen"
2019-05-17 08:40:41 +00:00
"github.com/Azareal/Gosora/routes"
2021-05-14 07:56:54 +00:00
"github.com/Azareal/Gosora/uutils"
2017-12-01 02:04:29 +00:00
"github.com/fsnotify/fsnotify"
2020-02-26 10:34:38 +00:00
2020-02-23 09:27:35 +00:00
//"github.com/lucas-clemente/quic-go/http3"
2018-07-13 11:27:58 +00:00
"github.com/pkg/errors"
2016-12-02 07:38:54 +00:00
)
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
var router * GenRouter
2017-01-01 15:45:43 +00:00
2017-11-05 09:55:34 +00:00
// TODO: Wrap the globals in here so we can pass pointers to them to subpackages
var globs * Globs
type Globs struct {
stmts * Stmts
}
2019-04-19 09:34:16 +00:00
// Temporary alias for renderTemplate
func init ( ) {
c . RenderTemplateAlias = routes . RenderTemplate
}
2019-05-17 08:40:41 +00:00
func afterDBInit ( ) ( err error ) {
2019-10-18 00:35:13 +00:00
if err := storeInit ( ) ; err != nil {
2019-05-17 08:40:41 +00:00
return err
}
2019-06-19 03:16:03 +00:00
log . Print ( "Exitted storeInit" )
2019-05-17 08:40:41 +00:00
2020-03-07 02:59:06 +00:00
c . GzipStartEtag = "\"" + strconv . FormatInt ( c . StartTime . Unix ( ) , 10 ) + "-ng\""
c . StartEtag = "\"" + strconv . FormatInt ( c . StartTime . Unix ( ) , 10 ) + "-n\""
2019-05-17 08:40:41 +00:00
var uids [ ] int
2020-02-23 09:27:35 +00:00
tc := c . Topics . GetCache ( )
if tc != nil {
2021-03-24 08:08:37 +00:00
log . Print ( "Preloading topics" )
2019-05-17 08:40:41 +00:00
// Preload ten topics to get the wheels going
var count = 10
2020-02-23 09:27:35 +00:00
if tc . GetCapacity ( ) <= 10 {
2019-05-17 08:40:41 +00:00
count = 2
2020-02-23 09:27:35 +00:00
if tc . GetCapacity ( ) <= 2 {
2019-05-17 08:40:41 +00:00
count = 0
}
}
2019-05-19 01:01:11 +00:00
group , err := c . Groups . Get ( c . GuestUser . Group )
if err != nil {
return err
}
2019-05-17 08:40:41 +00:00
// TODO: Use the same cached data for both the topic list and the topic fetches...
2020-03-03 04:25:18 +00:00
tList , _ , _ , err := c . TopicList . GetListByCanSee ( group . CanSee , 1 , 0 , nil )
2019-05-19 01:01:11 +00:00
if err != nil {
return err
}
ctList := make ( [ ] * c . TopicsRow , len ( tList ) )
copy ( ctList , tList )
2020-03-03 04:25:18 +00:00
tList , _ , _ , err = c . TopicList . GetListByCanSee ( group . CanSee , 2 , 0 , nil )
2019-05-17 08:40:41 +00:00
if err != nil {
return err
}
2019-05-19 01:01:11 +00:00
for _ , tItem := range tList {
ctList = append ( ctList , tItem )
}
2020-03-03 04:25:18 +00:00
tList , _ , _ , err = c . TopicList . GetListByCanSee ( group . CanSee , 3 , 0 , nil )
2019-05-19 01:01:11 +00:00
if err != nil {
return err
}
for _ , tItem := range tList {
ctList = append ( ctList , tItem )
}
if count > len ( ctList ) {
count = len ( ctList )
2019-05-17 08:40:41 +00:00
}
for i := 0 ; i < count ; i ++ {
2019-05-19 01:01:11 +00:00
_ , _ = c . Topics . Get ( ctList [ i ] . ID )
2019-05-17 08:40:41 +00:00
}
}
2020-02-23 09:27:35 +00:00
uc := c . Users . GetCache ( )
if uc != nil {
2019-05-17 08:40:41 +00:00
// Preload associated users too...
for _ , uid := range uids {
_ , _ = c . Users . Get ( uid )
}
}
2019-06-19 03:16:03 +00:00
log . Print ( "Exitted afterDBInit" )
2019-05-17 08:40:41 +00:00
return nil
}
2018-07-13 11:27:58 +00:00
// Experimenting with a new error package here to try to reduce the amount of debugging we have to do
// TODO: Dynamically register these items to avoid maintaining as much code here?
2021-04-07 02:10:05 +00:00
func storeInit ( ) ( e error ) {
2018-08-04 11:46:36 +00:00
acc := qgen . NewAcc ( )
2021-04-07 02:10:05 +00:00
ws := errors . WithStack
2019-05-17 08:40:41 +00:00
var rcache c . ReplyCache
if c . Config . ReplyCache == "static" {
rcache = c . NewMemoryReplyCache ( c . Config . ReplyCacheCapacity )
}
2021-04-07 02:10:05 +00:00
c . Rstore , e = c . NewSQLReplyStore ( acc , rcache )
if e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
c . Prstore , e = c . NewSQLProfileReplyStore ( acc )
if e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
c . Likes , e = c . NewDefaultLikeStore ( acc )
if e != nil {
return ws ( e )
2019-06-05 06:00:40 +00:00
}
2021-04-07 14:23:11 +00:00
c . ForumActionStore , e = c . NewDefaultForumActionStore ( acc )
2021-04-07 02:10:05 +00:00
if e != nil {
return ws ( e )
2021-04-07 14:23:11 +00:00
}
2021-04-07 02:10:05 +00:00
c . Convos , e = c . NewDefaultConversationStore ( acc )
if e != nil {
return ws ( e )
2019-10-18 00:35:13 +00:00
}
2021-04-07 02:10:05 +00:00
c . UserBlocks , e = c . NewDefaultBlockStore ( acc )
if e != nil {
return ws ( e )
}
c . GroupPromotions , e = c . NewDefaultGroupPromotionStore ( acc )
if e != nil {
return ws ( e )
2019-09-29 04:56:39 +00:00
}
2017-11-23 05:37:08 +00:00
2021-04-07 02:10:05 +00:00
if e = p . InitPhrases ( c . Site . Language ) ; e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
if e = c . InitEmoji ( ) ; e != nil {
return ws ( e )
2019-07-11 07:20:33 +00:00
}
2021-04-07 02:10:05 +00:00
if e = c . InitWeakPasswords ( ) ; e != nil {
return ws ( e )
2020-06-17 22:03:36 +00:00
}
2017-11-23 05:37:08 +00:00
log . Print ( "Loading the static files." )
2021-04-07 02:10:05 +00:00
if e = c . Themes . LoadStaticFiles ( ) ; e != nil {
return ws ( e )
2018-03-11 09:33:49 +00:00
}
2021-04-07 02:10:05 +00:00
if e = c . StaticFiles . Init ( ) ; e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
if e = c . StaticFiles . JSTmplInit ( ) ; e != nil {
return ws ( e )
2018-05-14 08:56:56 +00:00
}
2018-03-11 09:33:49 +00:00
2017-11-23 05:37:08 +00:00
log . Print ( "Initialising the widgets" )
2019-04-19 08:20:10 +00:00
c . Widgets = c . NewDefaultWidgetStore ( )
2021-04-07 02:10:05 +00:00
if e = c . InitWidgets ( ) ; e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2018-03-11 09:33:49 +00:00
2018-04-22 12:33:56 +00:00
log . Print ( "Initialising the menu item list" )
2019-04-19 08:20:10 +00:00
c . Menus = c . NewDefaultMenuStore ( )
2021-04-07 02:10:05 +00:00
if e = c . Menus . Load ( 1 ) ; e != nil { // 1 = the default menu
return ws ( e )
2018-04-22 12:33:56 +00:00
}
2021-04-07 02:10:05 +00:00
menuHold , e := c . Menus . Get ( 1 )
if e != nil {
return ws ( e )
2018-05-11 05:41:51 +00:00
}
2018-04-23 21:08:31 +00:00
fmt . Printf ( "menuHold: %+v\n" , menuHold )
2018-04-22 12:33:56 +00:00
var b bytes . Buffer
2019-04-19 08:20:10 +00:00
menuHold . Build ( & b , & c . GuestUser , "/" )
2018-04-22 12:33:56 +00:00
fmt . Println ( "menuHold output: " , string ( b . Bytes ( ) ) )
2017-11-23 05:37:08 +00:00
log . Print ( "Initialising the authentication system" )
2021-04-07 02:10:05 +00:00
c . Auth , e = c . NewDefaultAuth ( )
if e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2018-08-04 11:46:36 +00:00
log . Print ( "Initialising the stores" )
2021-04-07 02:10:05 +00:00
c . WordFilters , e = c . NewDefaultWordFilterStore ( acc )
if e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
c . MFAstore , e = c . NewSQLMFAStore ( acc )
if e != nil {
return ws ( e )
2018-06-17 07:28:18 +00:00
}
2021-04-07 02:10:05 +00:00
c . Pages , e = c . NewDefaultPageStore ( acc )
if e != nil {
return ws ( e )
2018-06-06 00:21:22 +00:00
}
2021-04-07 02:10:05 +00:00
c . Reports , e = c . NewDefaultReportStore ( acc )
if e != nil {
return ws ( e )
2018-05-27 09:36:35 +00:00
}
2021-04-07 02:10:05 +00:00
c . Emails , e = c . NewDefaultEmailStore ( acc )
if e != nil {
return ws ( e )
2018-05-27 09:36:35 +00:00
}
2021-04-07 02:10:05 +00:00
c . LoginLogs , e = c . NewLoginLogStore ( acc )
if e != nil {
return ws ( e )
2018-12-17 04:58:55 +00:00
}
2021-04-07 02:10:05 +00:00
c . RegLogs , e = c . NewRegLogStore ( acc )
if e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
c . ModLogs , e = c . NewModLogStore ( acc )
if e != nil {
return ws ( e )
2018-05-16 10:46:14 +00:00
}
2021-04-07 02:10:05 +00:00
c . AdminLogs , e = c . NewAdminLogStore ( acc )
if e != nil {
return ws ( e )
2017-11-23 05:37:08 +00:00
}
2021-04-07 02:10:05 +00:00
c . IPSearch , e = c . NewDefaultIPSearcher ( )
if e != nil {
return ws ( e )
2018-01-21 11:17:43 +00:00
}
2019-04-19 08:20:10 +00:00
if c . Config . Search == "" || c . Config . Search == "sql" {
2021-04-07 02:10:05 +00:00
c . RepliesSearch , e = c . NewSQLSearcher ( acc )
if e != nil {
return ws ( e )
2019-02-23 06:55:34 +00:00
}
2019-02-23 06:29:19 +00:00
}
2021-04-07 02:10:05 +00:00
c . Subscriptions , e = c . NewDefaultSubscriptionStore ( )
if e != nil {
return ws ( e )
2018-01-22 08:15:45 +00:00
}
2021-04-07 02:10:05 +00:00
c . Attachments , e = c . NewDefaultAttachmentStore ( acc )
if e != nil {
return ws ( e )
2018-01-22 08:15:45 +00:00
}
2021-04-07 02:10:05 +00:00
c . Polls , e = c . NewDefaultPollStore ( c . NewMemoryPollCache ( 100 ) ) // TODO: Max number of polls held in cache, make this a config item
if e != nil {
return ws ( e )
2018-01-25 04:57:33 +00:00
}
2021-04-07 02:10:05 +00:00
c . TopicList , e = c . NewDefaultTopicList ( acc )
if e != nil {
return ws ( e )
2018-02-10 15:07:21 +00:00
}
2021-04-07 02:10:05 +00:00
c . PasswordResetter , e = c . NewDefaultPasswordResetter ( acc )
if e != nil {
return ws ( e )
2019-03-11 08:47:45 +00:00
}
2021-05-03 01:23:37 +00:00
c . Analytics = c . NewDefaultAnalytics ( )
2021-04-07 02:10:05 +00:00
c . Activity , e = c . NewDefaultActivityStream ( acc )
if e != nil {
return ws ( e )
2019-06-01 12:31:48 +00:00
}
2021-05-02 08:47:19 +00:00
c . ActivityMatches , e = c . NewDefaultActivityStreamMatches ( acc )
if e != nil {
return ws ( e )
}
2018-07-28 12:52:23 +00:00
// TODO: Let the admin choose other thumbnailers, maybe ones defined in plugins
2019-04-19 08:20:10 +00:00
c . Thumbnailer = c . NewCaireThumbnailer ( )
2021-04-07 02:10:05 +00:00
c . Recalc , e = c . NewDefaultRecalc ( acc )
if e != nil {
return ws ( e )
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
}
2018-01-21 11:17:43 +00:00
2021-01-02 21:46:24 +00:00
log . Print ( "Initialising the meta store" )
2021-04-07 02:10:05 +00:00
c . Meta , e = meta . NewDefaultMetaStore ( acc )
if e != nil {
return ws ( e )
2021-01-02 21:46:24 +00:00
}
2018-05-27 09:36:35 +00:00
log . Print ( "Initialising the view counters" )
2020-02-26 10:34:38 +00:00
if ! c . Config . DisableAnalytics {
2021-04-07 02:10:05 +00:00
co . GlobalViewCounter , e = co . NewGlobalViewCounter ( acc )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
2021-04-07 02:10:05 +00:00
co . AgentViewCounter , e = co . NewDefaultAgentViewCounter ( acc )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
2021-04-07 02:10:05 +00:00
co . OSViewCounter , e = co . NewDefaultOSViewCounter ( acc )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
2021-04-07 02:10:05 +00:00
co . LangViewCounter , e = co . NewDefaultLangViewCounter ( acc )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
if ! c . Config . RefNoTrack {
2021-04-07 02:10:05 +00:00
co . ReferrerTracker , e = co . NewDefaultReferrerTracker ( )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
}
2021-04-07 02:10:05 +00:00
co . MemoryCounter , e = co . NewMemoryCounter ( acc )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
2021-04-07 02:10:05 +00:00
co . PerfCounter , e = co . NewDefaultPerfCounter ( acc )
if e != nil {
return ws ( e )
2020-02-26 10:34:38 +00:00
}
2018-03-08 03:59:47 +00:00
}
2021-04-07 02:10:05 +00:00
co . RouteViewCounter , e = co . NewDefaultRouteViewCounter ( acc )
if e != nil {
return ws ( e )
2017-12-24 07:38:46 +00:00
}
2021-04-07 02:10:05 +00:00
co . PostCounter , e = co . NewPostCounter ( )
if e != nil {
return ws ( e )
2018-01-14 12:03:20 +00:00
}
2021-04-07 02:10:05 +00:00
co . TopicCounter , e = co . NewTopicCounter ( )
if e != nil {
return ws ( e )
2018-01-18 12:31:25 +00:00
}
2021-04-07 02:10:05 +00:00
co . TopicViewCounter , e = co . NewDefaultTopicViewCounter ( )
if e != nil {
return ws ( e )
2017-12-10 03:43:30 +00:00
}
2021-04-07 02:10:05 +00:00
co . ForumViewCounter , e = co . NewDefaultForumViewCounter ( )
if e != nil {
return ws ( e )
2018-02-22 02:27:17 +00:00
}
2019-06-19 03:16:03 +00:00
2017-11-23 05:37:08 +00:00
return nil
}
2017-11-06 04:02:35 +00:00
// TODO: Split this function up
2017-09-03 04:50:31 +00:00
func main ( ) {
2017-10-21 00:27:47 +00:00
// TODO: Recover from panics
2021-05-05 09:23:58 +00:00
defer func ( ) {
2021-01-02 21:46:24 +00:00
if r := recover ( ) ; r != nil {
2017-10-21 00:27:47 +00:00
log . Print ( r )
debug . PrintStack ( )
2021-05-05 09:23:58 +00:00
log . Fatal ( "Fatal error." )
2017-10-21 00:27:47 +00:00
}
2021-05-05 09:23:58 +00:00
} ( )
2019-04-19 08:20:10 +00:00
c . StartTime = time . Now ( )
2017-11-07 22:38:15 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Have a file for each run with the time/date the server started as the file name?
// TODO: Log panics with recover()
2021-03-24 10:50:22 +00:00
f , err := os . OpenFile ( "./logs/ops-" + strconv . FormatInt ( c . StartTime . Unix ( ) , 10 ) + ".log" , os . O_WRONLY | os . O_APPEND | os . O_CREATE , 0755 )
2017-08-13 11:22:34 +00:00
if err != nil {
log . Fatal ( err )
}
2021-04-07 02:10:05 +00:00
//c.LogWriter = io.MultiWriter(os.Stderr, f)
c . LogWriter = io . MultiWriter ( os . Stdout , f )
c . ErrLogWriter = io . MultiWriter ( os . Stderr , f )
2019-04-19 08:20:10 +00:00
log . SetOutput ( c . LogWriter )
2021-04-07 02:10:05 +00:00
c . ErrLogger = log . New ( c . ErrLogWriter , "" , log . LstdFlags )
2019-04-19 08:20:10 +00:00
log . Print ( "Running Gosora v" + c . SoftwareVersion . String ( ) )
2017-05-07 08:31:41 +00:00
fmt . Println ( "" )
2017-07-17 10:23:42 +00:00
2018-08-22 01:32:07 +00:00
// TODO: Add a flag for enabling the profiler
if false {
2021-03-24 08:08:37 +00:00
f , err := os . Create ( c . Config . LogDir + "cpu.prof" )
2018-08-22 01:32:07 +00:00
if err != nil {
log . Fatal ( err )
}
pprof . StartCPUProfile ( f )
}
2021-01-02 21:46:24 +00:00
err = mime . AddExtensionType ( ".avif" , "image/avif" )
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
jsToken , err := c . GenerateSafeString ( 80 )
2018-05-31 06:51:31 +00:00
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
c . JSTokenBox . Store ( jsToken )
2018-05-31 06:51:31 +00:00
2018-06-17 07:28:18 +00:00
log . Print ( "Loading the configuration data" )
2019-04-19 08:20:10 +00:00
err = c . LoadConfig ( )
2018-06-17 07:28:18 +00:00
if err != nil {
log . Fatal ( err )
}
2017-08-13 11:22:34 +00:00
log . Print ( "Processing configuration data" )
2019-04-19 08:20:10 +00:00
err = c . ProcessConfig ( )
2017-10-14 07:39:22 +00:00
if err != nil {
log . Fatal ( err )
}
2021-04-07 02:10:05 +00:00
if c . Config . DisableStdout {
c . LogWriter = f
log . SetOutput ( c . LogWriter )
}
if c . Config . DisableStderr {
c . ErrLogWriter = f
c . ErrLogger = log . New ( c . ErrLogWriter , "" , log . LstdFlags )
}
2021-05-03 00:36:29 +00:00
c . Tasks = c . NewScheduledTasks ( )
2017-06-05 11:57:27 +00:00
2019-04-19 08:20:10 +00:00
err = c . InitTemplates ( )
2019-02-10 05:52:26 +00:00
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
c . Themes , err = c . NewThemeList ( )
2017-04-12 10:10:36 +00:00
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
c . TopicListThaw = c . NewSingleServerThaw ( )
2017-06-05 11:57:27 +00:00
2017-11-11 04:06:16 +00:00
err = InitDatabase ( )
2017-09-03 04:50:31 +00:00
if err != nil {
log . Fatal ( err )
}
2017-11-29 02:34:02 +00:00
defer db . Close ( )
2017-09-03 04:50:31 +00:00
2018-03-21 05:56:33 +00:00
buildTemplates := flag . Bool ( "build-templates" , false , "build the templates" )
flag . Parse ( )
if * buildTemplates {
2021-05-03 10:50:01 +00:00
if err = c . CompileTemplates ( ) ; err != nil {
2018-03-21 05:56:33 +00:00
log . Fatal ( err )
}
2021-05-03 10:50:01 +00:00
if err = c . CompileJSTemplates ( ) ; err != nil {
2018-04-22 12:33:56 +00:00
log . Fatal ( err )
}
2018-03-21 05:56:33 +00:00
return
}
2017-11-23 05:37:08 +00:00
err = afterDBInit ( )
2017-08-27 09:33:45 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
log . Fatalf ( "%+v" , err )
2017-08-27 09:33:45 +00:00
}
2019-04-19 08:20:10 +00:00
err = c . VerifyConfig ( )
2017-09-23 19:57:13 +00:00
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
if ! c . Dev . NoFsnotify {
2018-11-17 02:36:02 +00:00
log . Print ( "Initialising the file watcher" )
watcher , err := fsnotify . NewWatcher ( )
if err != nil {
log . Fatal ( err )
}
defer watcher . Close ( )
2017-12-01 02:04:29 +00:00
2018-11-17 02:36:02 +00:00
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2021-01-02 21:46:24 +00:00
var ErrFileSkip = errors . New ( "skip mod file" )
2019-10-06 00:34:09 +00:00
modifiedFileEvent := func ( path string ) error {
pathBits := strings . Split ( path , "\\" )
2018-11-17 02:36:02 +00:00
if len ( pathBits ) == 0 {
return nil
2017-12-10 03:43:30 +00:00
}
2018-11-17 02:36:02 +00:00
if pathBits [ 0 ] == "themes" {
var themeName string
if len ( pathBits ) >= 2 {
themeName = pathBits [ 1 ]
}
if len ( pathBits ) >= 3 && pathBits [ 2 ] == "public" {
// TODO: Handle new themes freshly plopped into the folder?
2019-04-19 08:20:10 +00:00
theme , ok := c . Themes [ themeName ]
2018-11-17 02:36:02 +00:00
if ok {
return theme . LoadStaticFiles ( )
}
2017-12-10 03:43:30 +00:00
}
}
2021-01-02 21:46:24 +00:00
return ErrFileSkip
2017-12-10 03:43:30 +00:00
}
2018-11-17 02:36:02 +00:00
// TODO: Expand this to more types of files
var err error
for {
select {
2021-01-02 21:46:24 +00:00
case ev := <- watcher . Events :
2018-11-17 02:36:02 +00:00
// TODO: Handle file deletes (and renames more graciously by removing the old version of it)
2021-01-02 21:46:24 +00:00
if ev . Op & fsnotify . Write == fsnotify . Write {
err = modifiedFileEvent ( ev . Name )
if err != ErrFileSkip {
log . Println ( "modified file:" , ev . Name )
} else {
err = nil
}
} else if ev . Op & fsnotify . Create == fsnotify . Create {
log . Println ( "new file:" , ev . Name )
err = modifiedFileEvent ( ev . Name )
2019-02-10 05:52:26 +00:00
} else {
2021-01-02 21:46:24 +00:00
log . Println ( "unknown event:" , ev )
2019-02-10 05:52:26 +00:00
err = nil
2018-11-17 02:36:02 +00:00
}
if err != nil {
2019-04-19 08:20:10 +00:00
c . LogError ( err )
2018-11-17 02:36:02 +00:00
}
case err = <- watcher . Errors :
2019-04-19 08:20:10 +00:00
c . LogWarning ( err )
2017-12-10 03:43:30 +00:00
}
2017-12-01 02:04:29 +00:00
}
2018-11-17 02:36:02 +00:00
} ( )
2017-12-01 02:04:29 +00:00
2018-11-17 02:36:02 +00:00
// TODO: Keep tabs on the (non-resource) theme stuff, and the langpacks
err = watcher . Add ( "./public" )
if err != nil {
log . Fatal ( err )
}
err = watcher . Add ( "./templates" )
2017-12-01 02:04:29 +00:00
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
for _ , theme := range c . Themes {
2018-11-17 02:36:02 +00:00
err = watcher . Add ( "./themes/" + theme . Name + "/public" )
if err != nil {
log . Fatal ( err )
}
}
2017-12-01 02:04:29 +00:00
}
2021-01-02 21:46:24 +00:00
/ * if err = c . StaticFiles . GenJS ( ) ; err != nil {
c . LogError ( err )
} * /
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
log . Print ( "Checking for init tasks" )
2021-01-02 21:46:24 +00:00
if err = sched ( ) ; err != nil {
Cascade delete attachments properly.
Cascade delete replied to topic events for replies properly.
Cascade delete likes on topic posts properly.
Cascade delete replies and their children properly.
Recalculate user stats properly when items are deleted.
Users can now unlike topic opening posts.
Add a recalculator to fix abnormalities across upgrades.
Try fixing a last_ip daily update bug.
Add Existable interface.
Add Delete method to LikeStore.
Add Each, Exists, Create, CountUser, CountMegaUser and CountBigUser methods to ReplyStore.
Add CountUser, CountMegaUser, CountBigUser methods to TopicStore.
Add Each method to UserStore.
Add Add, Delete and DeleteResource methods to SubscriptionStore.
Add Delete, DeleteByParams, DeleteByParamsExtra and AidsByParamsExtra methods to ActivityStream.
Add Exists method to ProfileReplyStore.
Add DropColumn, RenameColumn and ChangeColumn to the database adapters.
Shorten ipaddress column names to ip.
- topics table.
- replies table
- users_replies table.
- polls_votes table.
Add extra column to activity_stream table.
Fix an issue upgrading sites to MariaDB 10.3 from older versions of Gosora. Please report any other issues you find.
You need to run the updater / patcher for this commit.
2020-01-31 07:22:08 +00:00
c . LogError ( err )
}
2018-05-27 09:36:35 +00:00
log . Print ( "Initialising the task system" )
2017-12-24 22:08:35 +00:00
2018-07-28 12:52:23 +00:00
// Thumbnailer goroutine, we only want one image being thumbnailed at a time, otherwise they might wind up consuming all the CPU time and leave no resources left to service the actual requests
// TODO: Could we expand this to attachments and other things too?
thumbChan := make ( chan bool )
2019-04-19 08:20:10 +00:00
go c . ThumbTask ( thumbChan )
2021-05-02 08:47:19 +00:00
if err = tickLoop ( thumbChan ) ; err != nil {
c . LogError ( err )
}
2021-05-05 09:23:58 +00:00
go TickLoop . Loop ( )
2017-08-20 09:39:02 +00:00
2018-09-13 07:41:01 +00:00
// Resource Management Goroutine
2018-11-22 07:21:43 +00:00
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2021-04-07 14:23:11 +00:00
uc , tc := c . Users . GetCache ( ) , c . Topics . GetCache ( )
2021-01-02 21:46:24 +00:00
if uc == nil && tc == nil {
2018-09-13 07:41:01 +00:00
return
}
2018-09-19 06:09:03 +00:00
var lastEvictedCount int
var couldNotDealloc bool
2019-10-06 00:34:09 +00:00
secondTicker := time . NewTicker ( time . Second )
2018-09-13 07:41:01 +00:00
for {
select {
case <- secondTicker . C :
// TODO: Add a LastRequested field to cached User structs to avoid evicting the same things which wind up getting loaded again anyway?
2021-01-02 21:46:24 +00:00
if uc != nil {
ucap := uc . GetCapacity ( )
if uc . Length ( ) <= ucap || c . Users . Count ( ) <= ucap {
2018-11-22 07:21:43 +00:00
couldNotDealloc = false
2018-09-13 07:41:01 +00:00
continue
}
2021-01-02 21:46:24 +00:00
lastEvictedCount = uc . DeallocOverflow ( couldNotDealloc )
2018-11-22 07:21:43 +00:00
couldNotDealloc = ( lastEvictedCount == 0 )
2018-09-13 07:41:01 +00:00
}
}
}
2018-11-22 07:21:43 +00:00
} ( )
2018-09-13 07:41:01 +00:00
2017-06-19 08:06:54 +00:00
log . Print ( "Initialising the router" )
2021-05-02 08:47:19 +00:00
router , err = NewGenRouter ( & RouterConfig {
Uploads : http . FileServer ( http . Dir ( "./uploads" ) ) ,
} )
2018-04-03 04:34:07 +00:00
if err != nil {
log . Fatal ( err )
}
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
log . Print ( "Initialising the plugins" )
2019-04-19 08:20:10 +00:00
c . InitPlugins ( )
Added the Social Groups plugin. This is still under construction.
Made a few improvements to the ForumStore, including bringing it's API closer in line with the other datastores, adding stubs for future subforum functionality, and improving efficiency in a few places.
The auth interface now handles all the authentication stuff.
Renamed the debug config variable to debug_mode.
Added the PluginPerms API.
Internal Errors will now dump the stack trace in the console.
Added support for installable plugins.
Refactored the routing logic so that the router now handles the common PreRoute logic(exc. /static/)
Added the CreateTable method to the query generator. It might need some tweaking to better support other database systems.
Added the same CreateTable method to the query builder.
Began work on PostgreSQL support.
Added the string-string hook type
Added the pre_render hook type.
Added the ParentID and ParentType fields to forums.
Added the get_forum_url_prefix function.
Added a more generic build_slug function.
Added the get_topic_url_prefix function.
Added the override_perms and override_forum_perms functions for bulk setting and unsetting permissions.
Added more ExtData fields in a few structs and removed them on the Perms struct as the PluginPerms API supersedes them there.
Plugins can now see the router instance.
The plugin initialisation handlers can now throw errors.
Plugins are now initialised after all the forum's subsystems are.
Refactored the unit test logic. For instance, we now use the proper .Log method rather than fmt.Println in many cases.
Sorry, we'll have to break Github's generated file detection, as the build instructions aren't working, unless I put them at the top, and they're far, far more important than getting Github to recognise the generated code as generated code.
Fixed an issue with mysql.go's _init_database() overwriting the dbpassword variable. Not a huge issue, but it is a "gotcha" for those not expecting a ':' at the start.
Fixed an issue with forum creation where the forum permissions didn't get cached.
Fixed a bug in plugin_bbcode where negative numbers in rand would crash Gosora.
Made the outputs of plugin_markdown and plugin_bbcode more compliant with the tests.
Revamped the phrase system to make it easier for us to add language pack related features in the future.
Added the WidgetMenu widget type.
Revamped the theme again. I'm experimenting to see which approach I like most.
- Excuse the little W3C rage. Some things about CSS drive me crazy :p
Tests:
Added 22 bbcode_full_parse tests.
Added 19 bbcode_regex_parse tests.
Added 27 markdown_parse tests.
Added four UserStore tests. More to come when the test database functionality is added.
Added 18 name_to_slug tests.
Hooks:
Added the pre_render hook.
Added the pre_render_forum_list hook.
Added the pre_render_view_forum hook.
Added the pre_render_topic_list hook.
Added the pre_render_view_topic hook.
Added the pre_render_profile hook.
Added the pre_render_custom_page hook.
Added the pre_render_overview hook.
Added the pre_render_create_topic hook.
Added the pre_render_account_own_edit_critical hook.
Added the pre_render_account_own_edit_avatar hook.
Added the pre_render_account_own_edit_username hook.
Added the pre_render_account_own_edit_email hook.
Added the pre_render_login hook.
Added the pre_render_register hook.
Added the pre_render_ban hook.
Added the pre_render_panel_dashboard hook.
Added the pre_render_panel_forums hook.
Added the pre_render_panel_delete_forum hook.
Added the pre_render_panel_edit_forum hook.
Added the pre_render_panel_settings hook.
Added the pre_render_panel_setting hook.
Added the pre_render_panel_plugins hook.
Added the pre_render_panel_users hook.
Added the pre_render_panel_edit_user hook.
Added the pre_render_panel_groups hook.
Added the pre_render_panel_edit_group hook.
Added the pre_render_panel_edit_group_perms hook.
Added the pre_render_panel_themes hook.
Added the pre_render_panel_mod_log hook.
Added the pre_render_error hook.
Added the pre_render_security_error hook.
Added the create_group_preappend hook.
Added the intercept_build_widgets hook.
Added the simple_forum_check_pre_perms hook.
Added the forum_check_pre_perms hook.
2017-07-09 12:06:04 +00:00
2019-06-19 03:16:03 +00:00
log . Print ( "Setting up the signal handler" )
2017-11-29 02:34:02 +00:00
sigs := make ( chan os . Signal , 1 )
signal . Notify ( sigs , syscall . SIGINT , syscall . SIGTERM )
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2017-11-29 02:34:02 +00:00
sig := <- sigs
2021-05-03 00:36:29 +00:00
log . Print ( "Received a signal to shutdown: " , sig )
2017-11-29 02:34:02 +00:00
// TODO: Gracefully shutdown the HTTP server
2021-05-14 07:56:54 +00:00
tw , cn := c . NewTickWatch ( ) , uutils . Nanotime ( )
tw . Name = "shutdown"
tw . Set ( & tw . Start , cn )
tw . Set ( & tw . DBCheck , cn )
tw . Run ( )
n , e := func ( ) ( string , error ) {
if e := runHook ( "before_shutdown_tick" ) ; e != nil {
return "before_shutdown_tick " , e
}
tw . Set ( & tw . StartHook , uutils . Nanotime ( ) )
log . Print ( "Running shutdown tasks" )
if e := c . Tasks . Shutdown . Run ( ) ; e != nil {
return "shutdown tasks " , e
}
tw . Set ( & tw . Tasks , uutils . Nanotime ( ) )
log . Print ( "Ran shutdown tasks" )
if e := runHook ( "after_shutdown_tick" ) ; e != nil {
return "after_shutdown_tick " , e
}
tw . Set ( & tw . EndHook , uutils . Nanotime ( ) )
return "" , nil
} ( )
if e != nil {
log . Print ( n + " err:" , e )
2021-05-03 10:50:01 +00:00
}
2021-05-14 07:56:54 +00:00
tw . Stop ( )
2021-05-03 10:50:01 +00:00
log . Print ( "Stopping server" )
2021-05-03 00:36:29 +00:00
c . StoppedServer ( "Stopped server" )
2017-11-29 02:34:02 +00:00
} ( )
2017-06-05 11:57:27 +00:00
2018-06-24 13:49:29 +00:00
// Start up the WebSocket ticks
2019-04-19 08:20:10 +00:00
c . WsHub . Start ( )
2018-06-24 13:49:29 +00:00
2018-11-01 06:43:56 +00:00
if false {
2021-05-02 08:47:19 +00:00
f , e := os . Create ( c . Config . LogDir + "cpu.prof" )
if e != nil {
log . Fatal ( e )
2018-11-01 06:43:56 +00:00
}
pprof . StartCPUProfile ( f )
}
2017-01-17 07:55:46 +00:00
//if profiling {
// pprof.StopCPUProfile()
//}
2018-07-29 03:31:09 +00:00
startServer ( )
2019-04-19 08:20:10 +00:00
args := <- c . StopServerChan
2018-08-22 01:32:07 +00:00
if false {
pprof . StopCPUProfile ( )
2021-03-24 08:08:37 +00:00
f , err := os . Create ( c . Config . LogDir + "mem.prof" )
2018-11-17 02:36:02 +00:00
if err != nil {
log . Fatal ( err )
}
defer f . Close ( )
runtime . GC ( )
err = pprof . WriteHeapProfile ( f )
if err != nil {
log . Fatal ( err )
}
2018-08-22 01:32:07 +00:00
}
2018-07-29 03:31:09 +00:00
// Why did the server stop?
log . Fatal ( args ... )
}
func startServer ( ) {
2021-05-14 07:56:54 +00:00
// We might not need timeouts, if we're behind a reverse-proxy like Nginx
2021-04-07 14:23:11 +00:00
newServer := func ( addr string , h http . Handler ) * http . Server {
2021-05-14 07:56:54 +00:00
f := func ( timeout , dval int ) int {
if timeout == 0 {
timeout = dval
} else if timeout == - 1 {
timeout = 0
}
return timeout
2019-03-16 12:50:31 +00:00
}
2021-05-14 07:56:54 +00:00
rtime := f ( c . Config . ReadTimeout , 8 )
wtime := f ( c . Config . WriteTimeout , 10 )
itime := f ( c . Config . IdleTimeout , 120 )
2017-12-22 03:32:23 +00:00
return & http . Server {
2021-04-07 14:23:11 +00:00
Addr : addr ,
Handler : h ,
ConnState : c . ConnWatch . StateChange ,
2017-12-22 03:32:23 +00:00
2019-03-16 12:50:31 +00:00
ReadTimeout : time . Duration ( rtime ) * time . Second ,
WriteTimeout : time . Duration ( wtime ) * time . Second ,
IdleTimeout : time . Duration ( itime ) * time . Second ,
2018-07-26 06:15:49 +00:00
TLSConfig : & tls . Config {
PreferServerCipherSuites : true ,
CurvePreferences : [ ] tls . CurveID {
tls . CurveP256 ,
tls . X25519 ,
} ,
} ,
2017-12-22 03:32:23 +00:00
}
}
2017-09-10 16:57:22 +00:00
// TODO: Let users run *both* HTTP and HTTPS
2017-06-19 08:06:54 +00:00
log . Print ( "Initialising the HTTP server" )
2020-02-23 09:27:35 +00:00
/ * if c . Dev . QuicPort != 0 {
sQuicPort := strconv . Itoa ( c . Dev . QuicPort )
log . Print ( "Listening on quic port " + sQuicPort )
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2020-02-23 09:27:35 +00:00
c . StoppedServer ( http3 . ListenAndServeQUIC ( ":" + sQuicPort , c . Config . SslFullchain , c . Config . SslPrivkey , router ) )
} ( )
} * /
2019-04-19 08:20:10 +00:00
if ! c . Site . EnableSsl {
if c . Site . Port == "" {
c . Site . Port = "80"
2017-01-03 07:47:31 +00:00
}
2019-04-19 08:20:10 +00:00
log . Print ( "Listening on port " + c . Site . Port )
2018-07-29 03:31:09 +00:00
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2019-04-19 08:20:10 +00:00
c . StoppedServer ( newServer ( ":" + c . Site . Port , router ) . ListenAndServe ( ) )
2018-07-29 03:31:09 +00:00
} ( )
return
2017-07-17 10:23:42 +00:00
}
2019-04-19 08:20:10 +00:00
if c . Site . Port == "" {
c . Site . Port = "443"
2018-07-29 03:31:09 +00:00
}
2019-04-19 08:20:10 +00:00
if c . Site . Port == "80" || c . Site . Port == "443" {
2018-07-29 03:31:09 +00:00
// We should also run the server on port 80
// TODO: Redirect to port 443
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2018-07-29 03:31:09 +00:00
log . Print ( "Listening on port 80" )
2019-04-19 08:20:10 +00:00
c . StoppedServer ( newServer ( ":80" , & HTTPSRedirect { } ) . ListenAndServe ( ) )
2018-07-29 03:31:09 +00:00
} ( )
2017-01-03 07:47:31 +00:00
}
2019-04-19 08:20:10 +00:00
log . Printf ( "Listening on port %s" , c . Site . Port )
2018-07-29 03:31:09 +00:00
go func ( ) {
2021-05-05 09:23:58 +00:00
defer c . EatPanics ( )
2019-04-19 08:20:10 +00:00
c . StoppedServer ( newServer ( ":" + c . Site . Port , router ) . ListenAndServeTLS ( c . Config . SslFullchain , c . Config . SslPrivkey ) )
2018-07-29 03:31:09 +00:00
} ( )
2017-04-13 15:01:30 +00:00
}