2023-05-25 17:37:25 +00:00
|
|
|
package main
|
|
|
|
|
2023-05-26 14:12:53 +00:00
|
|
|
import (
|
2023-05-26 14:20:40 +00:00
|
|
|
"context"
|
2023-05-27 07:57:01 +00:00
|
|
|
"embed"
|
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
2023-05-26 14:20:40 +00:00
|
|
|
"sync"
|
|
|
|
|
2023-06-12 11:13:16 +00:00
|
|
|
"github.com/gofiber/template/html/v2"
|
2023-05-26 14:12:53 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"github.com/medium.rip/api"
|
2023-05-26 14:20:40 +00:00
|
|
|
"github.com/medium.rip/internal/config"
|
|
|
|
"github.com/medium.rip/internal/lifecycle"
|
2023-05-26 14:12:53 +00:00
|
|
|
)
|
2023-05-25 17:37:25 +00:00
|
|
|
|
2023-05-27 07:57:01 +00:00
|
|
|
//go:embed frontend
|
|
|
|
var frontend embed.FS
|
|
|
|
|
|
|
|
//go:embed frontend/dist/assets
|
|
|
|
var static embed.FS
|
|
|
|
|
2023-05-25 17:37:25 +00:00
|
|
|
func main() {
|
2023-05-26 14:20:40 +00:00
|
|
|
// load config
|
|
|
|
config.Load()
|
|
|
|
|
2023-05-27 07:57:01 +00:00
|
|
|
// create template engine
|
|
|
|
tmplFs, err := fs.Sub(frontend, "frontend/dist")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error loading template: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
engine := html.NewFileSystem(http.FS(tmplFs), ".html")
|
|
|
|
|
|
|
|
// create static file server
|
|
|
|
staticFs, err := fs.Sub(static, "frontend/dist/assets")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("error loading static assets: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
staticHttp := http.FS(staticFs)
|
|
|
|
|
2023-05-26 14:20:40 +00:00
|
|
|
// cancellable context
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
wg.Add(1)
|
2023-05-27 07:57:01 +00:00
|
|
|
go api.RegisterRoutes(ctx, &wg, engine, staticHttp)
|
2023-05-26 14:20:40 +00:00
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go lifecycle.ShutdownListener(&wg, &cancelFunc)
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
log.Info("Graceful shutdown complete")
|
2023-05-25 17:37:25 +00:00
|
|
|
}
|