Skip to content
Snippets Groups Projects
Select Git revision
  • a604811febf614b97f58a0b743e3fbd68bca3c9a
  • main default protected
  • devcontainer-support
  • print-signal
  • v0.0.6
  • v0.0.5
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
10 results

app.go

Blame
  • app.go 1.83 KiB
    package main
    
    import (
    	"fmt"
    	"log"
    	"net"
    	"os"
    	"sync/atomic"
    
    	"github.com/gofiber/contrib/hcaptcha"
    	"github.com/gofiber/fiber/v3"
    	"github.com/gofiber/fiber/v3/middleware/cors"
    	"github.com/syndtr/goleveldb/leveldb"
    )
    
    func serve(sig chan os.Signal, db *leveldb.DB) error {
    	app := fiber.New()
    
    	// cors
    	if conf[allowedURL] != "unset" {
    		app.Use(cors.New(cors.Config{
    			AllowOrigins: []string{conf[allowedURL]},
    			AllowHeaders: []string{"Origin", "Content-Type", "Accept"},
    		}))
    	} else {
    		log.Println("CORS disabled")
    	}
    
    	var captcha fiber.Handler
    	hCaptchaEnable := conf[hCaptchaSiteKey] != "unset" && conf[hCaptchaSecretKey] != "unset"
    
    	if hCaptchaEnable {
    		// create hCaptcha middleware if enabled
    		captcha = hcaptcha.New(hcaptcha.Config{
    			SecretKey: conf[hCaptchaSecretKey],
    		})
    
    		log.Printf("hCaptcha enabled with site key %q", conf[hCaptchaSiteKey])
    	} else {
    		// empty middleware if disabled
    		captcha = func(c fiber.Ctx) error {
    			return c.Next()
    		}
    
    		log.Printf("hCaptcha disabled because one or both of %q and %q are unset",
    			confEnv[hCaptchaSiteKey][0], confEnv[hCaptchaSecretKey][0])
    	}
    
    	count := new(atomic.Uint64)
    
    	routeHCaptchaSiteKey(app, "/api", !hCaptchaEnable, conf[hCaptchaSiteKey])
    	routeRegister(app, "/api/register", db, count, captcha)
    
    	if err := routeCount(app, "/api/count", db, count); err != nil {
    		return err
    	}
    
    	// graceful shutdown
    	go func() {
    		<-sig
    		log.Println("shutting down")
    		if err := app.Shutdown(); err != nil {
    			fmt.Printf("cannot shutdown: %v", err)
    		}
    	}()
    
    	if conf[listen] == "unset" {
    		return app.Listen(conf[listenAddr])
    	} else {
    		if l, err := net.Listen("unix", conf[listen]); err != nil {