2023-07-22 00:59:45 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-11-17 09:09:11 +00:00
|
|
|
"errors"
|
2023-07-22 00:59:45 +01:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
2023-08-21 00:27:54 +01:00
|
|
|
"github.com/1f349/site-hosting/config"
|
|
|
|
"github.com/1f349/site-hosting/serve"
|
2023-07-22 00:59:45 +01:00
|
|
|
"github.com/1f349/site-hosting/upload"
|
2023-08-21 00:27:54 +01:00
|
|
|
"github.com/MrMelon54/exit-reload"
|
2023-07-22 00:59:45 +01:00
|
|
|
"github.com/julienschmidt/httprouter"
|
2023-08-21 00:27:54 +01:00
|
|
|
"github.com/spf13/afero"
|
2023-07-22 00:59:45 +01:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
listenFlag string
|
|
|
|
storageFlag string
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
flag.StringVar(&listenFlag, "listen", "", "Address to listen on")
|
|
|
|
flag.StringVar(&storageFlag, "storage", "", "Path site files are stored in")
|
2023-11-17 09:09:11 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2023-07-22 00:59:45 +01:00
|
|
|
if listenFlag == "" {
|
|
|
|
log.Fatal("[SiteHosting] Missing listen flag")
|
|
|
|
}
|
|
|
|
if storageFlag == "" {
|
|
|
|
log.Fatal("[SiteHosting] Missing storage flag")
|
|
|
|
}
|
|
|
|
_, err := os.Stat(storageFlag)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("[SiteHosting] Failed to stat storage path, does the directory exist? Error: ", err)
|
|
|
|
}
|
|
|
|
|
2023-08-21 00:27:54 +01:00
|
|
|
storageFs := afero.NewBasePathFs(afero.NewOsFs(), storageFlag)
|
|
|
|
liveConf := config.New(storageFs)
|
|
|
|
|
|
|
|
uploadHandler := upload.New(storageFs, liveConf)
|
|
|
|
serveHandler := serve.New(storageFs, liveConf)
|
2023-07-22 00:59:45 +01:00
|
|
|
|
|
|
|
router := httprouter.New()
|
|
|
|
router.POST("/u/:site", uploadHandler.Handle)
|
2023-08-21 00:27:54 +01:00
|
|
|
router.GET("/", serveHandler.Handle)
|
2023-07-22 00:59:45 +01:00
|
|
|
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: listenFlag,
|
|
|
|
Handler: router,
|
|
|
|
}
|
|
|
|
log.Printf("[SiteHosting] Starting server on: '%s'\n", srv.Addr)
|
|
|
|
go func() {
|
|
|
|
err := srv.ListenAndServe()
|
|
|
|
if err != nil {
|
2023-11-17 09:09:11 +00:00
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
2023-07-22 00:59:45 +01:00
|
|
|
log.Printf("[SiteHosting] The http server shutdown successfully\n")
|
|
|
|
} else {
|
|
|
|
log.Printf("[SiteHosting] Error trying to host the http server: %s\n", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-21 00:27:54 +01:00
|
|
|
exit_reload.ExitReload("SiteHosting", func() {
|
|
|
|
|
|
|
|
}, func() {
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
exitSig := make(chan struct{}, 1)
|
2023-07-22 00:59:45 +01:00
|
|
|
scReload := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(scReload, syscall.SIGHUP)
|
2023-08-21 00:27:54 +01:00
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-exitSig:
|
|
|
|
case <-scReload:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2023-07-22 00:59:45 +01:00
|
|
|
|
|
|
|
// Wait for exit signal
|
|
|
|
sc := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
|
|
|
<-sc
|
2023-08-21 00:27:54 +01:00
|
|
|
close(exitSig)
|
2023-07-22 00:59:45 +01:00
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
// Stop server
|
|
|
|
log.Printf("[SiteHosting] Stopping...")
|
|
|
|
n := time.Now()
|
|
|
|
|
|
|
|
// close http server
|
|
|
|
_ = srv.Close()
|
|
|
|
|
|
|
|
log.Printf("[SiteHosting] Took '%s' to shutdown\n", time.Now().Sub(n))
|
|
|
|
log.Println("[SiteHosting] Goodbye")
|
|
|
|
}
|