2017-09-13 15:09:13 +00:00
/ *
*
* Gosora Main File
* Copyright Azareal 2016 - 2018
*
* /
2016-12-02 07:38:54 +00:00
package main
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-07-28 12:52:23 +00:00
"strconv"
2017-12-01 02:04:29 +00:00
"strings"
2018-07-15 10:29:31 +00:00
"sync/atomic"
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
2017-11-11 04:06:16 +00:00
"./common"
2018-02-19 04:26:01 +00:00
"./common/counters"
2018-05-14 08:56:56 +00:00
"./query_gen/lib"
2018-05-15 05:59:52 +00:00
"./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
)
2017-11-11 04:06:16 +00:00
var version = common . Version { Major : 0 , Minor : 1 , Patch : 0 , Tag : "dev" }
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-09-03 04:50:31 +00:00
var logWriter = io . MultiWriter ( os . Stderr )
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-05-14 08:56:56 +00:00
acc := qgen . Builder . Accumulator ( )
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
}
err = common . InitTemplates ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2017-11-23 05:37:08 +00:00
}
err = common . InitPhrases ( )
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" )
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
menuHold . Build ( & b , & common . GuestUser )
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-03-11 09:33:49 +00:00
log . Print ( "Loading the word filters" )
2017-11-23 05:37:08 +00:00
err = common . LoadWordFilters ( )
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
log . Print ( "Initialising the stores" )
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-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
}
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
}
common . Attachments , err = common . NewDefaultAttachmentStore ( )
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
}
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
}
2018-02-19 04:26:01 +00:00
counters . AgentViewCounter , err = counters . NewDefaultAgentViewCounter ( )
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
}
2018-02-19 04:26:01 +00:00
counters . OSViewCounter , err = counters . NewDefaultOSViewCounter ( )
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
}
2018-03-08 03:59:47 +00:00
counters . LangViewCounter , err = counters . NewDefaultLangViewCounter ( )
if err != nil {
2018-07-13 11:27:58 +00:00
return errors . WithStack ( err )
2018-03-08 03:59:47 +00:00
}
2018-02-19 04:26:01 +00:00
counters . RouteViewCounter , err = counters . NewDefaultRouteViewCounter ( )
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
}
} ( ) * /
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()
2018-04-03 04:34:07 +00:00
f , err := os . OpenFile ( "./logs/ops.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 )
}
2017-09-03 04:50:31 +00:00
logWriter = io . MultiWriter ( os . Stderr , f )
2017-08-13 11:22:34 +00:00
log . SetOutput ( logWriter )
2017-05-07 08:31:41 +00:00
log . Print ( "Running Gosora v" + version . String ( ) )
fmt . Println ( "" )
2018-05-27 09:36:35 +00:00
common . StartTime = time . Now ( )
2017-07-17 10:23:42 +00:00
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
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 )
}
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-05-27 09:36:35 +00:00
log . Print ( "Initialising the file watcher" )
2017-12-01 02:04:29 +00:00
watcher , err := fsnotify . NewWatcher ( )
if err != nil {
log . Fatal ( err )
}
defer watcher . Close ( )
go func ( ) {
2017-12-10 03:43:30 +00:00
var modifiedFileEvent = func ( path string ) error {
var pathBits = strings . Split ( path , "\\" )
if len ( pathBits ) == 0 {
return nil
}
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 ( )
}
}
}
return nil
}
2018-07-28 12:52:23 +00:00
// TODO: Expand this to more types of files
2017-12-10 03:43:30 +00:00
var err error
2017-12-01 02:04:29 +00:00
for {
select {
case event := <- watcher . Events :
log . Println ( "event:" , event )
2017-12-10 03:43:30 +00:00
// TODO: Handle file deletes (and renames more graciously by removing the old version of it)
2017-12-01 02:04:29 +00:00
if event . Op & fsnotify . Write == fsnotify . Write {
log . Println ( "modified file:" , event . Name )
2017-12-10 03:43:30 +00:00
err = modifiedFileEvent ( event . Name )
2017-12-01 02:04:29 +00:00
} else if event . Op & fsnotify . Create == fsnotify . Create {
log . Println ( "new file:" , event . Name )
2017-12-10 03:43:30 +00:00
err = modifiedFileEvent ( event . Name )
2017-12-01 02:04:29 +00:00
}
2017-12-10 03:43:30 +00:00
if err != nil {
common . LogError ( err )
}
case err = <- watcher . Errors :
common . LogError ( err )
2017-12-01 02:04:29 +00:00
}
}
} ( )
2017-12-10 03:43:30 +00:00
// TODO: Keep tabs on the (non-resource) theme stuff, and the langpacks
2017-12-01 02:04:29 +00:00
err = watcher . Add ( "./public" )
if err != nil {
log . Fatal ( err )
}
err = watcher . Add ( "./templates" )
if err != nil {
log . Fatal ( err )
}
for _ , theme := range common . Themes {
err = watcher . Add ( "./themes/" + theme . Name + "/public" )
if err != nil {
log . Fatal ( err )
}
}
2018-05-27 09:36:35 +00:00
log . Print ( "Initialising the task system" )
2018-07-15 10:29:31 +00:00
// TODO: Name the tasks so we can figure out which one it was when something goes wrong? Or maybe toss it up WithStack down there?
2017-12-24 22:08:35 +00:00
var runTasks = func ( tasks [ ] func ( ) error ) {
for _ , task := range tasks {
if task ( ) != nil {
common . LogError ( err )
}
}
}
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 )
go func ( ) {
acc := qgen . Builder . Accumulator ( )
for {
// Put this goroutine to sleep until we have work to do
<- thumbChan
// TODO: Use a real queue
err := acc . Select ( "users_avatar_queue" ) . Columns ( "uid" ) . Limit ( "0,5" ) . EachInt ( func ( uid int ) error {
//log.Print("uid: ", uid)
// TODO: Do a bulk user fetch instead?
user , err := common . Users . Get ( uid )
if err != nil {
return errors . WithStack ( err )
}
//log.Print("user.RawAvatar: ", user.RawAvatar)
// Has the avatar been removed or already been processed by the thumbnailer?
if len ( user . RawAvatar ) < 2 || user . RawAvatar [ 1 ] == '.' {
_ , _ = acc . Delete ( "users_avatar_queue" ) . Where ( "uid = ?" ) . Run ( uid )
return nil
}
// This means it's an external image, they aren't currently implemented, but this is here for when they are
if user . RawAvatar [ 0 ] != '.' {
return nil
}
/ * if user . RawAvatar == ".gif" {
return nil
} * /
2018-07-28 14:51:24 +00:00
if user . RawAvatar != ".png" && user . RawAvatar != ".jpg" && user . RawAvatar != ".jpeg" && user . RawAvatar != ".gif" {
2018-07-28 12:52:23 +00:00
return nil
}
2018-07-28 14:51:24 +00:00
err = common . Thumbnailer . Resize ( user . RawAvatar [ 1 : ] , "./uploads/avatar_" + strconv . Itoa ( user . ID ) + user . RawAvatar , "./uploads/avatar_" + strconv . Itoa ( user . ID ) + "_tmp" + user . RawAvatar , "./uploads/avatar_" + strconv . Itoa ( user . ID ) + "_w48" + user . RawAvatar , 48 )
2018-07-28 12:52:23 +00:00
if err != nil {
return errors . WithStack ( err )
}
err = user . ChangeAvatar ( "." + user . RawAvatar )
if err != nil {
return errors . WithStack ( err )
}
_ , err = acc . Delete ( "users_avatar_queue" ) . Where ( "uid = ?" ) . Run ( uid )
return errors . WithStack ( err )
} )
if err != nil {
common . LogError ( err )
}
}
} ( )
2018-06-17 07:28:18 +00:00
// TODO: Write tests for these
2018-01-11 08:03:17 +00:00
// Run this goroutine once every half second
halfSecondTicker := time . NewTicker ( time . Second / 2 )
2018-03-08 03:59:47 +00:00
secondTicker := time . NewTicker ( time . Second )
2017-09-03 04:50:31 +00:00
fifteenMinuteTicker := time . NewTicker ( 15 * time . Minute )
2018-05-31 06:51:31 +00:00
hourTicker := time . NewTicker ( time . Hour )
2017-08-20 09:39:02 +00:00
go func ( ) {
2018-07-15 10:29:31 +00:00
var startTick = func ( ) ( abort bool ) {
var isDBDown = atomic . LoadInt32 ( & common . IsDBDown )
err := db . Ping ( )
if err != nil {
// TODO: There's a bit of a race here, but it doesn't matter if this error appears multiple times in the logs as it's capped at three times, we just want to cut it down 99% of the time
if isDBDown == 0 {
common . LogWarning ( err )
common . LogWarning ( errors . New ( "The database is down" ) )
}
atomic . StoreInt32 ( & common . IsDBDown , 1 )
return true
}
if isDBDown == 1 {
log . Print ( "The database is back" )
}
atomic . StoreInt32 ( & common . IsDBDown , 0 )
return false
}
2018-02-15 13:15:27 +00:00
var runHook = func ( name string ) {
err := common . RunTaskHook ( name )
if err != nil {
2018-07-13 11:27:58 +00:00
common . LogError ( err , "Failed at task '" + name + "'" )
2018-02-15 13:15:27 +00:00
}
}
2017-08-20 09:39:02 +00:00
for {
select {
2018-01-11 08:03:17 +00:00
case <- halfSecondTicker . C :
2018-07-15 10:29:31 +00:00
if startTick ( ) {
continue
}
2018-02-15 13:15:27 +00:00
runHook ( "before_half_second_tick" )
2018-01-11 08:03:17 +00:00
runTasks ( common . ScheduledHalfSecondTasks )
2018-02-15 13:15:27 +00:00
runHook ( "after_half_second_tick" )
2017-09-03 04:50:31 +00:00
case <- secondTicker . C :
2018-07-15 10:29:31 +00:00
if startTick ( ) {
continue
}
2018-02-15 13:15:27 +00:00
runHook ( "before_second_tick" )
2018-07-28 12:52:23 +00:00
go func ( ) { thumbChan <- true } ( )
2017-12-24 22:08:35 +00:00
runTasks ( common . ScheduledSecondTasks )
2017-09-22 02:21:17 +00:00
2017-12-24 22:08:35 +00:00
// TODO: Stop hard-coding this
2017-11-11 04:06:16 +00:00
err := common . HandleExpiredScheduledGroups ( )
2017-09-03 04:50:31 +00:00
if err != nil {
2017-11-11 04:06:16 +00:00
common . LogError ( err )
2017-09-03 04:50:31 +00:00
}
2017-09-10 16:57:22 +00:00
// TODO: Handle delayed moderation tasks
// Sync with the database, if there are any changes
2017-11-11 04:06:16 +00:00
err = common . HandleServerSync ( )
2017-09-10 16:57:22 +00:00
if err != nil {
2017-11-11 04:06:16 +00:00
common . LogError ( err )
2017-09-10 16:57:22 +00:00
}
// TODO: Manage the TopicStore, UserStore, and ForumStore
// TODO: Alert the admin, if CPU usage, RAM usage, or the number of posts in the past second are too high
// TODO: Clean-up alerts with no unread matches which are over two weeks old. Move this to a 24 hour task?
2017-09-23 19:57:13 +00:00
// TODO: Rescan the static files for changes
2018-02-15 13:15:27 +00:00
runHook ( "after_second_tick" )
2017-09-03 04:50:31 +00:00
case <- fifteenMinuteTicker . C :
2018-07-15 10:29:31 +00:00
if startTick ( ) {
continue
}
2018-02-15 13:15:27 +00:00
runHook ( "before_fifteen_minute_tick" )
2017-12-24 22:08:35 +00:00
runTasks ( common . ScheduledFifteenMinuteTasks )
2017-12-10 03:43:30 +00:00
2017-09-10 16:57:22 +00:00
// TODO: Automatically lock topics, if they're really old, and the associated setting is enabled.
// TODO: Publish scheduled posts.
2018-02-15 13:15:27 +00:00
runHook ( "after_fifteen_minute_tick" )
2018-05-31 06:51:31 +00:00
case <- hourTicker . C :
2018-07-15 10:29:31 +00:00
if startTick ( ) {
continue
}
2018-05-31 06:51:31 +00:00
runHook ( "before_hour_tick" )
2018-06-17 07:28:18 +00:00
2018-05-31 06:51:31 +00:00
jsToken , err := common . GenerateSafeString ( 80 )
if err != nil {
common . LogError ( err )
}
common . JSTokenBox . Store ( jsToken )
2018-06-17 07:28:18 +00:00
common . OldSessionSigningKeyBox . Store ( common . SessionSigningKeyBox . Load ( ) . ( string ) ) // TODO: We probably don't need this type conversion
sessionSigningKey , err := common . GenerateSafeString ( 80 )
if err != nil {
common . LogError ( err )
}
common . SessionSigningKeyBox . Store ( sessionSigningKey )
2018-05-31 06:51:31 +00:00
runTasks ( common . ScheduledHourTasks )
runHook ( "after_hour_tick" )
2017-08-20 09:39:02 +00:00
}
2017-12-24 22:08:35 +00:00
// TODO: Handle the daily clean-up.
2017-08-20 09:39:02 +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-07-29 03:31:09 +00:00
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 ( )
2017-01-17 07:55:46 +00:00
//if profiling {
// pprof.StopCPUProfile()
//}
2018-07-29 03:31:09 +00:00
startServer ( )
args := <- stopServerChan
// Why did the server stop?
log . Fatal ( args ... )
}
// TODO: Add a graceful shutdown function
func stoppedServer ( msg ... interface { } ) {
log . Print ( "stopped server" )
stopServerChan <- msg
}
var stopServerChan = make ( chan [ ] interface { } )
2017-06-05 11:57:27 +00:00
2018-07-29 03:31:09 +00:00
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 {
return & http . Server {
Addr : addr ,
Handler : handler ,
ReadTimeout : 5 * time . Second ,
WriteTimeout : 10 * time . Second ,
IdleTimeout : 120 * 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 ( ) {
stoppedServer ( newServer ( ":" + common . Site . Port , router ) . ListenAndServe ( ) )
} ( )
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" )
stoppedServer ( newServer ( ":80" , & routes . HTTPSRedirect { } ) . ListenAndServe ( ) )
} ( )
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 ( ) {
stoppedServer ( newServer ( ":" + common . Site . Port , router ) . ListenAndServeTLS ( common . Config . SslFullchain , common . Config . SslPrivkey ) )
} ( )
2017-04-13 15:01:30 +00:00
}