Browse Source

When refreshing templates, restart the entire webserver

tags/v0.5.1b
Morgan Bazalgette 3 years ago
parent
commit
a4a2c28abc
5 changed files with 96 additions and 53 deletions
  1. 41
    21
      main.go
  2. 40
    23
      startuato_linux.go
  3. 3
    4
      startuato_windows.go
  4. 8
    3
      templates.go
  5. 4
    2
      templates/team.html

+ 41
- 21
main.go View File

@@ -126,26 +126,13 @@ func main() {
}
}

// even if it's not release, we say that it's release
// so that gin doesn't spam
gin.SetMode(gin.ReleaseMode)

schiavo.Prefix = "hanayo"
schiavo.Bunker.Send(fmt.Sprintf("STARTUATO, mode: %s", gin.Mode()))

fmt.Println("Starting session system...")
var store sessions.Store
if config.RedisMaxConnections != 0 {
store, err = sessions.NewRedisStore(
config.RedisMaxConnections,
config.RedisNetwork,
config.RedisAddress,
config.RedisPassword,
[]byte(config.CookieSecret),
)
if err != nil {
fmt.Println(err)
store = sessions.NewCookieStore([]byte(config.CookieSecret))
}
} else {
store = sessions.NewCookieStore([]byte(config.CookieSecret))
}
gobRegisters := []interface{}{
[]message{},
errorMessage{},
@@ -164,7 +151,42 @@ func main() {
fmt.Println("Setting up rate limiter...")
setUpLimiter()

fmt.Println("Starting webserver...")
fmt.Println("Exporting configuration...")

conf.Export(config, "hanayo.conf")

httpLoop()
}

func httpLoop() {
for {
e := generateEngine()
fmt.Println("Starting webserver...")
if !startuato(e) {
break
}
}
}

func generateEngine() *gin.Engine {
fmt.Println("Starting session system...")
var store sessions.Store
if config.RedisMaxConnections != 0 {
var err error
store, err = sessions.NewRedisStore(
config.RedisMaxConnections,
config.RedisNetwork,
config.RedisAddress,
config.RedisPassword,
[]byte(config.CookieSecret),
)
if err != nil {
fmt.Println(err)
store = sessions.NewCookieStore([]byte(config.CookieSecret))
}
} else {
store = sessions.NewCookieStore([]byte(config.CookieSecret))
}

r := gin.Default()

@@ -200,9 +222,7 @@ func main() {

r.NoRoute(notFound)

conf.Export(config, "hanayo.conf")

startuato(r)
return r
}

const alwaysRespondText = `Ooops! Looks like something went really wrong while trying to process your request.

+ 40
- 23
startuato_linux.go View File

@@ -12,18 +12,30 @@ import (
"os"
"os/exec"
"syscall"
"time"

"git.zxq.co/ripple/schiavolib"
"github.com/gin-gonic/gin"
"github.com/rcrowley/goagain"
)

func startuato(engine *gin.Engine) {
var l net.Listener

func startuato(engine *gin.Engine) bool {
engine.GET("/51/update", updateFromRemote)

returnCh := make(chan bool)
// whether it was from this very thing or not
var iZingri bool
hs := func(l net.Listener, h http.Handler) {
err := http.Serve(l, h)
if f, ok := err.(*net.OpError); ok && f.Err.Error() == "use of closed network connection" && !iZingri {
returnCh <- true
}
}

var err error
// Inherit a net.Listener from our parent process or listen anew.
l, err := goagain.Listener()
l, err = goagain.Listener()
if err != nil {

// Listen on a TCP or a UNIX domain socket (TCP here).
@@ -40,13 +52,13 @@ func startuato(engine *gin.Engine) {
schiavo.Bunker.Send(fmt.Sprint("LISTENINGU STARTUATO ON ", l.Addr()))

// Accept connections in a new goroutine.
go http.Serve(l, engine)
go hs(l, engine)

} else {

// Resume accepting connections in a new goroutine.
schiavo.Bunker.Send(fmt.Sprint("LISTENINGU RESUMINGU ON ", l.Addr()))
go http.Serve(l, engine)
go hs(l, engine)

// Kill the parent, now that the child has started successfully.
if err := goagain.Kill(); err != nil {
@@ -56,25 +68,30 @@ func startuato(engine *gin.Engine) {

}

// Block the main goroutine awaiting signals.
if _, err := goagain.Wait(l); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
go func() {
// Block the main goroutine awaiting signals.
if _, err := goagain.Wait(l); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}

// Do whatever's necessary to ensure a graceful exit like waiting for
// goroutines to terminate or a channel to become closed.
//
// In this case, we'll simply stop listening and wait one second.
if err := l.Close(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
if err := db.Close(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
time.Sleep(time.Second * 1)
// Do whatever's necessary to ensure a graceful exit like waiting for
// goroutines to terminate or a channel to become closed.
//
// In this case, we'll simply stop listening and wait one second.
iZingri = true
if err := l.Close(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
if err := db.Close(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
returnCh <- false
}()

return <-returnCh
}

func updateFromRemote(c *gin.Context) {

+ 3
- 4
startuato_windows.go View File

@@ -10,11 +10,10 @@ import (
"github.com/gin-gonic/gin"
)

var l net.Listener

func startuato(engine *gin.Engine) {
var (
l net.Listener
err error
)
var err error

// Listen on a TCP or a UNIX domain socket (TCP here).
if config.Unix {

+ 8
- 3
templates.go View File

@@ -11,14 +11,13 @@ import (
"net/http"
"os"
"strings"
"time"

"git.zxq.co/ripple/rippleapi/common"

"github.com/rjeczalik/notify"

"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"github.com/pariz/gountries"
"github.com/rjeczalik/notify"
"github.com/thehowl/conf"
)

@@ -219,10 +218,16 @@ func reloader() error {
return err
}
go func() {
var last time.Time
for range c {
if time.Since(last) < time.Second*3 {
continue
}
fmt.Println("Change detected! Refreshing templates")
simplePages = []templateConfig{}
loadTemplates("")
l.Close()
last = time.Now()
}
defer notify.Stop(c)
}()

+ 4
- 2
templates/team.html View File

@@ -12,8 +12,10 @@ Include=user_group.html
<div class="ui center aligned segment">
<h1 class="ui heading">Community Managers</h1>
<p>
Community Managers deal with bans, silences, name changes and pretty much everything that has to do with the community.<br>
They are take care of our Discord server and reply to emails sent to <a href="mailto:support@ripple.moe">support@ripple.moe</a>. Community Managers have a red name in the in-game chat.
Community Managers deal with bans, silences, name changes and
pretty much everything that has to do with the community.<br>
They are take care of our Discord server and reply to emails
sent to <a href="mailto:support@ripple.moe">support@ripple.moe</a>. Community Managers have a red name in the in-game chat.
</p>
{{ template "userGroup" (.Get "users?sort=username,asc&privilege_group=%s" "community%20manager") }}
</div>

Loading…
Cancel
Save