2021-02-26 15:36:46 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-02 13:51:59 +01:00
|
|
|
"embed"
|
|
|
|
"io/fs"
|
2021-02-26 15:36:46 +01:00
|
|
|
"net/http"
|
2021-02-27 09:31:59 +01:00
|
|
|
"time"
|
|
|
|
|
2022-07-06 18:22:59 +02:00
|
|
|
"github.com/mr-karan/doggo/internal/hub"
|
2021-02-27 09:31:59 +01:00
|
|
|
"github.com/mr-karan/doggo/pkg/utils"
|
2022-07-01 03:59:14 +02:00
|
|
|
"github.com/mr-karan/logf"
|
2021-02-26 15:36:46 +01:00
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
2021-03-02 13:51:59 +01:00
|
|
|
"github.com/go-chi/chi/middleware"
|
2021-02-26 15:36:46 +01:00
|
|
|
"github.com/knadh/koanf"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2022-07-01 04:15:46 +02:00
|
|
|
// Version of the build. This is injected at build-time.
|
|
|
|
buildString = "unknown"
|
|
|
|
|
2021-02-26 15:36:46 +01:00
|
|
|
logger = utils.InitLogger()
|
2021-02-27 09:31:59 +01:00
|
|
|
ko = koanf.New(".")
|
2021-03-02 13:51:59 +01:00
|
|
|
//go:embed assets/*
|
|
|
|
assetsDir embed.FS
|
|
|
|
//go:embed index.html
|
|
|
|
html []byte
|
2021-02-26 15:36:46 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-02-27 09:31:59 +01:00
|
|
|
initConfig()
|
2021-02-26 15:36:46 +01:00
|
|
|
|
2021-02-27 09:31:59 +01:00
|
|
|
// Initialize app.
|
2022-07-06 18:22:59 +02:00
|
|
|
app := hub.New(logger, buildString)
|
2021-02-26 15:36:46 +01:00
|
|
|
|
2021-03-02 13:51:59 +01:00
|
|
|
// Register router instance.
|
2021-02-27 09:31:59 +01:00
|
|
|
r := chi.NewRouter()
|
2021-03-02 13:51:59 +01:00
|
|
|
|
|
|
|
// Register middlewares
|
|
|
|
r.Use(middleware.RequestID)
|
|
|
|
r.Use(middleware.RealIP)
|
|
|
|
r.Use(middleware.Logger)
|
|
|
|
r.Use(middleware.Recoverer)
|
|
|
|
|
|
|
|
// Frontend Handlers.
|
|
|
|
assets, _ := fs.Sub(assetsDir, "assets")
|
|
|
|
r.Get("/assets/*", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
fs := http.StripPrefix("/assets/", http.FileServer(http.FS(assets)))
|
|
|
|
fs.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Add("Content-Type", "text/html")
|
|
|
|
w.Write(html)
|
|
|
|
})
|
|
|
|
|
|
|
|
// API Handlers.
|
|
|
|
r.Get("/api/", wrap(app, handleIndexAPI))
|
|
|
|
r.Get("/api/ping/", wrap(app, handleHealthCheck))
|
|
|
|
r.Post("/api/lookup/", wrap(app, handleLookup))
|
2021-02-27 09:31:59 +01:00
|
|
|
|
|
|
|
// HTTP Server.
|
|
|
|
srv := &http.Server{
|
|
|
|
Addr: ko.String("server.address"),
|
|
|
|
Handler: r,
|
|
|
|
ReadTimeout: ko.Duration("server.read_timeout") * time.Millisecond,
|
|
|
|
WriteTimeout: ko.Duration("server.write_timeout") * time.Millisecond,
|
|
|
|
IdleTimeout: ko.Duration("server.keepalive_timeout") * time.Millisecond,
|
2021-02-26 15:36:46 +01:00
|
|
|
}
|
|
|
|
|
2022-07-01 03:59:14 +02:00
|
|
|
logger.WithFields(logf.Fields{
|
2021-02-27 09:31:59 +01:00
|
|
|
"address": srv.Addr,
|
2022-07-01 04:15:46 +02:00
|
|
|
"version": buildString,
|
2021-02-27 09:31:59 +01:00
|
|
|
}).Info("starting server")
|
2021-02-26 15:36:46 +01:00
|
|
|
|
2021-02-27 09:31:59 +01:00
|
|
|
if err := srv.ListenAndServe(); err != nil {
|
2022-07-01 03:59:14 +02:00
|
|
|
logger.WithError(err).Fatal("couldn't start server")
|
2021-02-27 09:31:59 +01:00
|
|
|
}
|
2021-02-26 15:36:46 +01:00
|
|
|
}
|