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
2019-04-19 08:20:10 +00:00
c "github.com/Azareal/Gosora/common"
2018-10-27 03:21:02 +00:00
"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"
2019-05-17 08:40:41 +00:00
"github.com/Azareal/Gosora/routes"
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
}
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 ) {
err = storeInit ( )
if err != nil {
return err
}
var uids [ ] int
tcache := c . Topics . GetCache ( )
if tcache != nil {
// Preload ten topics to get the wheels going
var count = 10
if tcache . GetCapacity ( ) <= 10 {
count = 2
if tcache . GetCapacity ( ) <= 2 {
count = 0
}
}
// TODO: Use the same cached data for both the topic list and the topic fetches...
tList , _ , _ , err := c . TopicList . GetList ( 1 , "" , nil )
if err != nil {
return err
}
if count > len ( tList ) {
count = len ( tList )
}
for i := 0 ; i < count ; i ++ {
_ , _ = c . Topics . Get ( tList [ i ] . ID )
}
}
ucache := c . Users . GetCache ( )
if ucache != nil {
// Preload associated users too...
for _ , uid := range uids {
_ , _ = c . Users . Get ( uid )
}
}
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?
2019-05-17 08:40:41 +00:00
func storeInit ( ) ( err error ) {
2018-08-04 11:46:36 +00:00
acc := qgen . NewAcc ( )
2019-05-17 08:40:41 +00:00
var rcache c . ReplyCache
if c . Config . ReplyCache == "static" {
rcache = c . NewMemoryReplyCache ( c . Config . ReplyCacheCapacity )
}
c . Rstore , err = c . NewSQLReplyStore ( acc , rcache )
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
}
2019-04-19 08:20:10 +00:00
c . Prstore , err = c . 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
}
2019-04-19 08:20:10 +00:00
err = phrases . InitPhrases ( c . 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." )
2019-04-19 08:20:10 +00:00
err = c . Themes . LoadStaticFiles ( )
2018-03-11 09:33:49 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-03-11 09:33:49 +00:00
}
2019-04-19 08:20:10 +00:00
err = c . StaticFiles . Init ( )
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
}
2019-04-19 08:20:10 +00:00
err = c . StaticFiles . JSTmplInit ( )
2018-05-14 08:56:56 +00:00
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-04-19 08:20:10 +00:00
c . Widgets = c . NewDefaultWidgetStore ( )
err = c . InitWidgets ( )
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-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 ( )
err = c . Menus . Load ( 1 ) // 1 = the default menu
2018-04-22 12:33:56 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-04-22 12:33:56 +00:00
}
2019-04-19 08:20:10 +00:00
menuHold , err := c . Menus . Get ( 1 )
2018-05-11 05:41:51 +00:00
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
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" )
2019-04-19 08:20:10 +00:00
c . Auth , err = c . NewDefaultAuth ( )
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-08-04 11:46:36 +00:00
log . Print ( "Initialising the stores" )
2019-04-19 08:20:10 +00:00
c . WordFilters , err = c . 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
}
2019-04-19 08:20:10 +00:00
c . MFAstore , err = c . NewSQLMFAStore ( acc )
2018-06-17 07:28:18 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-06-17 07:28:18 +00:00
}
2019-04-19 08:20:10 +00:00
c . Pages , err = c . NewDefaultPageStore ( acc )
2018-06-06 00:21:22 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-06-06 00:21:22 +00:00
}
2019-04-19 08:20:10 +00:00
c . Reports , err = c . NewDefaultReportStore ( acc )
2018-05-27 09:36:35 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
c . Emails , err = c . NewDefaultEmailStore ( acc )
2018-05-27 09:36:35 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-05-27 09:36:35 +00:00
}
2019-04-19 08:20:10 +00:00
c . LoginLogs , err = c . NewLoginLogStore ( acc )
2018-12-17 04:58:55 +00:00
if err != nil {
return errors . WithStack ( err )
}
2019-04-19 08:20:10 +00:00
c . RegLogs , err = c . 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
}
2019-04-19 08:20:10 +00:00
c . ModLogs , err = c . NewModLogStore ( acc )
2018-05-16 10:46:14 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-05-16 10:46:14 +00:00
}
2019-04-19 08:20:10 +00:00
c . AdminLogs , err = c . 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
}
2019-04-19 08:20:10 +00:00
c . IPSearch , err = c . NewDefaultIPSearcher ( )
2018-01-21 11:17:43 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-21 11:17:43 +00:00
}
2019-04-19 08:20:10 +00:00
if c . Config . Search == "" || c . Config . Search == "sql" {
c . RepliesSearch , err = c . NewSQLSearcher ( acc )
2019-02-23 06:55:34 +00:00
if err != nil {
return errors . WithStack ( err )
}
2019-02-23 06:29:19 +00:00
}
2019-04-19 08:20:10 +00:00
c . Subscriptions , err = c . NewDefaultSubscriptionStore ( )
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
}
2019-04-19 08:20:10 +00:00
c . Attachments , err = c . 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
}
2019-04-19 08:20:10 +00:00
c . Polls , err = c . NewDefaultPollStore ( c . NewMemoryPollCache ( 100 ) ) // TODO: Max number of polls held in cache, make this a config item
2018-01-25 04:57:33 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-01-25 04:57:33 +00:00
}
2019-04-19 08:20:10 +00:00
c . TopicList , err = c . NewDefaultTopicList ( )
2018-02-10 15:07:21 +00:00
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-02-10 15:07:21 +00:00
}
2019-04-19 08:20:10 +00:00
c . PasswordResetter , err = c . NewDefaultPasswordResetter ( acc )
2019-03-11 08:47:45 +00:00
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
2019-04-19 08:20:10 +00:00
c . Thumbnailer = c . 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
}
2019-05-01 06:59:51 +00:00
counters . MemoryCounter , err = counters . NewMemoryCounter ( acc )
if err != nil {
return errors . WithStack ( err )
}
2019-05-09 06:58:55 +00:00
c . Meta , err = c . NewDefaultMetaStore ( acc )
if err != nil {
return errors . WithStack ( err )
}
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-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()
2019-04-19 08:20:10 +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 )
}
2019-04-19 08:20:10 +00:00
c . LogWriter = io . MultiWriter ( os . Stderr , f )
log . SetOutput ( c . LogWriter )
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 {
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 )
}
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 )
}
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 {
2019-04-19 08:20:10 +00:00
err = c . CompileTemplates ( )
2018-03-21 05:56:33 +00:00
if err != nil {
log . Fatal ( err )
}
2019-04-19 08:20:10 +00:00
err = c . CompileJSTemplates ( )
2018-04-22 12:33:56 +00:00
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
}
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 ( ) {
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?
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
}
}
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 {
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
}
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 )
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 ( ) {
2019-04-19 08:20:10 +00:00
ucache := c . Users . GetCache ( )
tcache := c . Topics . GetCache ( )
2018-09-13 07:41:01 +00:00
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 ( )
2019-04-19 08:20:10 +00:00
if ucache . Length ( ) <= ucap || c . 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" )
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
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
2019-04-19 08:20:10 +00:00
runTasks ( c . ShutdownTasks )
c . 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
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 {
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 ( )
2019-04-19 08:20:10 +00:00
args := <- c . StopServerChan
2018-08-22 01:32:07 +00:00
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-04-19 08:20:10 +00:00
rtime := c . Config . ReadTimeout
2019-03-16 12:50:31 +00:00
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
}
2019-04-19 08:20:10 +00:00
wtime := c . Config . WriteTimeout
2019-03-16 12:50:31 +00:00
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
}
2019-04-19 08:20:10 +00:00
itime := c . Config . IdleTimeout
2019-03-16 12:50:31 +00:00
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" )
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 ( ) {
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 ( ) {
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 ( ) {
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
}