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"
"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"
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
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/common"
"github.com/Azareal/Gosora/common/counters"
2018-11-01 06:43:56 +00:00
"github.com/Azareal/Gosora/common/phrases"
2018-10-27 03:21:02 +00:00
"github.com/Azareal/Gosora/query_gen"
2017-12-01 02:04:29 +00:00
"github.com/fsnotify/fsnotify"
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
}
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?
2017-11-23 05:37:08 +00:00
func afterDBInit ( ) ( err error ) {
2018-08-04 11:46:36 +00:00
acc := qgen . NewAcc ( )
2018-05-14 08:56:56 +00:00
common . Rstore , err = common . NewSQLReplyStore ( acc )
2017-11-23 05:37:08 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-05-14 08:56:56 +00:00
common . Prstore , err = common . NewSQLProfileReplyStore ( acc )
2017-11-23 05:37:08 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-11-01 06:43:56 +00:00
err = phrases . InitPhrases ( common . Site . Language )
2017-11-23 05:37:08 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
log . Print ( "Loading the static files." )
2018-03-11 09:33:49 +00:00
err = common . Themes . LoadStaticFiles ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-03-11 09:33:49 +00:00
}
2017-11-23 05:37:08 +00:00
err = common . StaticFiles . Init ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-05-14 08:56:56 +00:00
err = common . StaticFiles . JSTmplInit ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
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-01-21 12:27:59 +00:00
common . Widgets = common . NewDefaultWidgetStore ( )
2017-11-23 05:37:08 +00:00
err = common . InitWidgets ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
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" )
common . Menus = common . NewDefaultMenuStore ( )
err = common . Menus . Load ( 1 ) // 1 = the default menu
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-04-22 12:33:56 +00:00
}
2018-05-11 05:41:51 +00:00
menuHold , err := common . Menus . Get ( 1 )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
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
2018-10-14 05:08:44 +00:00
menuHold . Build ( & b , & common . 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" )
common . Auth , err = common . NewDefaultAuth ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-08-04 11:46:36 +00:00
log . Print ( "Initialising the stores" )
common . WordFilters , err = common . NewDefaultWordFilterStore ( acc )
2017-11-23 05:37:08 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-06-17 07:28:18 +00:00
common . MFAstore , err = common . NewSQLMFAStore ( acc )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-06-17 07:28:18 +00:00
}
2018-06-06 00:21:22 +00:00
common . Pages , err = common . NewDefaultPageStore ( acc )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-06-06 00:21:22 +00:00
}
2018-05-27 09:36:35 +00:00
common . Reports , err = common . NewDefaultReportStore ( acc )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-05-27 09:36:35 +00:00
}
common . Emails , err = common . NewDefaultEmailStore ( acc )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-05-27 09:36:35 +00:00
}
2018-12-17 04:58:55 +00:00
common . LoginLogs , err = common . NewLoginLogStore ( acc )
if err != nil {
return errors . WithStack ( err )
}
2018-05-16 10:46:14 +00:00
common . RegLogs , err = common . NewRegLogStore ( acc )
2017-11-23 05:37:08 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-05-16 10:46:14 +00:00
common . ModLogs , err = common . NewModLogStore ( acc )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-05-16 10:46:14 +00:00
}
common . AdminLogs , err = common . NewAdminLogStore ( acc )
2017-11-23 05:37:08 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
2018-01-21 11:17:43 +00:00
common . IPSearch , err = common . NewDefaultIPSearcher ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-21 11:17:43 +00:00
}
2019-02-23 06:55:34 +00:00
if common . Config . Search == "" || common . Config . Search == "sql" {
common . RepliesSearch , err = common . NewSQLSearcher ( acc )
if err != nil {
return errors . WithStack ( err )
}
2019-02-23 06:29:19 +00:00
}
2018-01-22 08:15:45 +00:00
common . Subscriptions , err = common . NewDefaultSubscriptionStore ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-22 08:15:45 +00:00
}
2019-04-13 11:54:22 +00:00
common . Attachments , err = common . NewDefaultAttachmentStore ( acc )
2018-01-22 08:15:45 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-22 08:15:45 +00:00
}
2018-01-25 04:57:33 +00:00
common . Polls , err = common . NewDefaultPollStore ( common . NewMemoryPollCache ( 100 ) ) // TODO: Max number of polls held in cache, make this a config item
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-25 04:57:33 +00:00
}
2018-02-10 15:07:21 +00:00
common . TopicList , err = common . NewDefaultTopicList ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-02-10 15:07:21 +00:00
}
2019-03-11 08:47:45 +00:00
common . PasswordResetter , err = common . NewDefaultPasswordResetter ( acc )
if err != nil {
return errors . WithStack ( err )
}
2018-07-28 12:52:23 +00:00
// TODO: Let the admin choose other thumbnailers, maybe ones defined in plugins
common . Thumbnailer = common . NewCaireThumbnailer ( )
2018-01-21 11:17:43 +00:00
2018-05-27 09:36:35 +00:00
log . Print ( "Initialising the view counters" )
counters . GlobalViewCounter , err = counters . NewGlobalViewCounter ( acc )
2017-12-19 03:53:13 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-09 07:39:29 +00:00
}
2019-03-12 09:13:57 +00:00
counters . AgentViewCounter , err = counters . NewDefaultAgentViewCounter ( acc )
2018-01-09 07:39:29 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-12-19 03:53:13 +00:00
}
2019-03-21 22:59:41 +00:00
counters . OSViewCounter , err = counters . NewDefaultOSViewCounter ( acc )
2018-02-04 08:15:20 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-02-04 08:15:20 +00:00
}
2019-03-21 22:59:41 +00:00
counters . LangViewCounter , err = counters . NewDefaultLangViewCounter ( acc )
2018-03-08 03:59:47 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-03-08 03:59:47 +00:00
}
2019-03-21 22:59:41 +00:00
counters . RouteViewCounter , err = counters . NewDefaultRouteViewCounter ( acc )
2017-12-24 07:38:46 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-12-24 07:38:46 +00:00
}
2018-02-19 04:26:01 +00:00
counters . PostCounter , err = counters . NewPostCounter ( )
2018-01-14 12:03:20 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-14 12:03:20 +00:00
}
2018-02-19 04:26:01 +00:00
counters . TopicCounter , err = counters . NewTopicCounter ( )
2018-01-18 12:31:25 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-18 12:31:25 +00:00
}
2018-02-19 04:26:01 +00:00
counters . TopicViewCounter , err = counters . NewDefaultTopicViewCounter ( )
2017-12-10 03:43:30 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-12-10 03:43:30 +00:00
}
2018-02-22 02:27:17 +00:00
counters . ForumViewCounter , err = counters . NewDefaultForumViewCounter ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-02-22 02:27:17 +00:00
}
2018-02-19 04:26:01 +00:00
counters . ReferrerTracker , err = counters . NewDefaultReferrerTracker ( )
2018-02-05 10:29:13 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-02-05 10:29:13 +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
/ * defer func ( ) {
r := recover ( )
if r != nil {
log . Print ( r )
debug . PrintStack ( )
return
}
} ( ) * /
2019-02-10 05:52:26 +00:00
common . 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()
2019-02-10 05:52:26 +00:00
f , err := os . OpenFile ( "./logs/ops-" + strconv . FormatInt ( common . 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 )
}
2019-02-23 06:29:19 +00:00
common . LogWriter = io . MultiWriter ( os . Stderr , f )
log . SetOutput ( common . LogWriter )
2018-10-02 05:03:20 +00:00
log . Print ( "Running Gosora v" + common . 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 {
2018-11-01 06:43:56 +00:00
f , err := os . Create ( "./logs/cpu.prof" )
2018-08-22 01:32:07 +00:00
if err != nil {
log . Fatal ( err )
}
pprof . StartCPUProfile ( f )
}
2018-05-31 06:51:31 +00:00
jsToken , err := common . GenerateSafeString ( 80 )
if err != nil {
log . Fatal ( err )
}
common . JSTokenBox . Store ( jsToken )
2018-06-17 07:28:18 +00:00
log . Print ( "Loading the configuration data" )
err = common . LoadConfig ( )
if err != nil {
log . Fatal ( err )
}
2017-08-13 11:22:34 +00:00
log . Print ( "Processing configuration data" )
2017-11-11 04:06:16 +00:00
err = common . ProcessConfig ( )
2017-10-14 07:39:22 +00:00
if err != nil {
log . Fatal ( err )
}
2017-06-05 11:57:27 +00:00
2019-02-10 05:52:26 +00:00
err = common . InitTemplates ( )
if err != nil {
log . Fatal ( err )
}
2018-05-27 09:36:35 +00:00
common . Themes , err = common . NewThemeList ( )
2017-04-12 10:10:36 +00:00
if err != nil {
log . Fatal ( err )
}
2018-11-19 23:06:15 +00:00
common . TopicListThaw = common . 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 {
err = common . CompileTemplates ( )
if err != nil {
log . Fatal ( err )
}
2018-04-22 12:33:56 +00:00
err = common . CompileJSTemplates ( )
if err != nil {
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
}
2017-11-11 04:06:16 +00:00
err = common . VerifyConfig ( )
2017-09-23 19:57:13 +00:00
if err != nil {
log . Fatal ( err )
}
2018-11-17 02:36:02 +00:00
if ! common . Dev . NoFsnotify {
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 ( ) {
var modifiedFileEvent = func ( path string ) error {
var pathBits = strings . Split ( path , "\\" )
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?
theme , ok := common . Themes [ themeName ]
if ok {
return theme . LoadStaticFiles ( )
}
2017-12-10 03:43:30 +00:00
}
}
2018-11-17 02:36:02 +00:00
return nil
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 {
case event := <- watcher . Events :
// TODO: Handle file deletes (and renames more graciously by removing the old version of it)
if event . Op & fsnotify . Write == fsnotify . Write {
log . Println ( "modified file:" , event . Name )
err = modifiedFileEvent ( event . Name )
} else if event . Op & fsnotify . Create == fsnotify . Create {
log . Println ( "new file:" , event . Name )
err = modifiedFileEvent ( event . Name )
2019-02-10 05:52:26 +00:00
} else {
log . Println ( "unknown event:" , event )
err = nil
2018-11-17 02:36:02 +00:00
}
if err != nil {
common . LogError ( err )
}
case err = <- watcher . Errors :
2019-02-10 05:52:26 +00:00
common . 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 )
}
2018-11-17 02:36:02 +00:00
for _ , theme := range common . Themes {
err = watcher . Add ( "./themes/" + theme . Name + "/public" )
if err != nil {
log . Fatal ( err )
}
}
2017-12-01 02:04:29 +00:00
}
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 )
2018-07-29 04:34:28 +00:00
go common . ThumbTask ( thumbChan )
2018-12-31 09:03:49 +00:00
go tickLoop ( thumbChan )
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 ( ) {
2018-09-13 07:41:01 +00:00
ucache := common . Users . GetCache ( )
tcache := common . Topics . GetCache ( )
if ucache == nil && tcache == nil {
return
}
2018-09-19 06:09:03 +00:00
var lastEvictedCount int
var couldNotDealloc bool
2018-12-31 09:03:49 +00:00
var 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?
if ucache != nil {
ucap := ucache . GetCapacity ( )
if ucache . Length ( ) <= ucap || common . Users . GlobalCount ( ) <= ucap {
2018-11-22 07:21:43 +00:00
couldNotDealloc = false
2018-09-13 07:41:01 +00:00
continue
}
2018-11-22 07:21:43 +00:00
lastEvictedCount = ucache . DeallocOverflow ( couldNotDealloc )
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" )
2018-04-03 04:34:07 +00:00
router , err = NewGenRouter ( http . FileServer ( http . Dir ( "./uploads" ) ) )
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" )
2017-11-11 04:06:16 +00:00
common . 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
2017-11-29 02:34:02 +00:00
sigs := make ( chan os . Signal , 1 )
signal . Notify ( sigs , syscall . SIGINT , syscall . SIGTERM )
go func ( ) {
sig := <- sigs
// TODO: Gracefully shutdown the HTTP server
2017-12-24 22:08:35 +00:00
runTasks ( common . ShutdownTasks )
2018-08-22 01:32:07 +00:00
common . StoppedServer ( "Received a signal to shutdown: " , sig )
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
common . WsHub . Start ( )
2018-11-01 06:43:56 +00:00
if false {
f , err := os . Create ( "./logs/cpu.prof" )
if err != nil {
log . Fatal ( err )
}
pprof . StartCPUProfile ( f )
}
2017-01-17 07:55:46 +00:00
//if profiling {
// pprof.StopCPUProfile()
//}
2018-07-29 03:31:09 +00:00
startServer ( )
2018-08-22 01:32:07 +00:00
args := <- common . StopServerChan
if false {
pprof . StopCPUProfile ( )
2018-11-17 02:36:02 +00:00
f , err := os . Create ( "./logs/mem.prof" )
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 ( ) {
2017-12-22 03:32:23 +00:00
// We might not need the timeouts, if we're behind a reverse-proxy like Nginx
var newServer = func ( addr string , handler http . Handler ) * http . Server {
2019-03-16 12:50:31 +00:00
rtime := common . Config . ReadTimeout
if rtime == 0 {
2019-03-21 22:59:41 +00:00
rtime = 8
2019-03-16 23:14:47 +00:00
} else if rtime == - 1 {
rtime = 0
2019-03-16 12:50:31 +00:00
}
wtime := common . Config . WriteTimeout
if wtime == 0 {
wtime = 10
2019-03-16 23:14:47 +00:00
} else if wtime == - 1 {
wtime = 0
2019-03-16 12:50:31 +00:00
}
itime := common . Config . IdleTimeout
if itime == 0 {
itime = 120
2019-03-16 23:14:47 +00:00
} else if itime == - 1 {
itime = 0
2019-03-16 12:50:31 +00:00
}
2017-12-22 03:32:23 +00:00
return & http . Server {
Addr : addr ,
Handler : handler ,
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" )
2017-11-11 04:06:16 +00:00
if ! common . Site . EnableSsl {
if common . Site . Port == "" {
common . Site . Port = "80"
2017-01-03 07:47:31 +00:00
}
2017-11-11 04:06:16 +00:00
log . Print ( "Listening on port " + common . Site . Port )
2018-07-29 03:31:09 +00:00
go func ( ) {
2018-08-22 01:32:07 +00:00
common . StoppedServer ( newServer ( ":" + common . Site . Port , router ) . ListenAndServe ( ) )
2018-07-29 03:31:09 +00:00
} ( )
return
2017-07-17 10:23:42 +00:00
}
2018-07-29 03:31:09 +00:00
if common . Site . Port == "" {
common . Site . Port = "443"
}
if common . Site . Port == "80" || common . Site . Port == "443" {
// We should also run the server on port 80
// TODO: Redirect to port 443
go func ( ) {
log . Print ( "Listening on port 80" )
2019-03-21 22:59:41 +00:00
common . StoppedServer ( newServer ( ":80" , & HTTPSRedirect { } ) . ListenAndServe ( ) )
2018-07-29 03:31:09 +00:00
} ( )
2017-01-03 07:47:31 +00:00
}
2018-07-29 03:31:09 +00:00
log . Printf ( "Listening on port %s" , common . Site . Port )
go func ( ) {
2018-08-22 01:32:07 +00:00
common . StoppedServer ( newServer ( ":" + common . Site . Port , router ) . ListenAndServeTLS ( common . Config . SslFullchain , common . Config . SslPrivkey ) )
2018-07-29 03:31:09 +00:00
} ( )
2017-04-13 15:01:30 +00:00
}