48 lines
1.1 KiB
Go
48 lines
1.1 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"path/filepath"
|
|
"time"
|
|
|
|
"git.oblat.lv/alex/triggerssmith/api/invoke"
|
|
"git.oblat.lv/alex/triggerssmith/internal/config"
|
|
"git.oblat.lv/alex/triggerssmith/internal/vars"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type Router struct {
|
|
r chi.Router
|
|
|
|
cfg *config.Config
|
|
}
|
|
|
|
func NewRouter(cfg *config.Config) *Router {
|
|
r := chi.NewRouter()
|
|
return &Router{
|
|
r: r,
|
|
cfg: cfg,
|
|
}
|
|
}
|
|
|
|
func (r *Router) RouteHandler() chi.Router {
|
|
r.r.Get("/", func(w http.ResponseWriter, req *http.Request) {
|
|
http.ServeFile(w, req, filepath.Join(r.cfg.Server.StaticFilesPath, "index.html"))
|
|
})
|
|
fs := http.FileServer(http.Dir("static"))
|
|
r.r.Handle("/static/*", http.StripPrefix("/static/", fs))
|
|
r.r.Get("/health", func(w http.ResponseWriter, r *http.Request) {
|
|
b, _ := json.Marshal(struct {
|
|
Status string `json:"status"`
|
|
Uptime string `json:"uptime"`
|
|
}{
|
|
Status: "ok",
|
|
Uptime: time.Since(vars.START_TIME).String(),
|
|
})
|
|
w.Write([]byte(b))
|
|
})
|
|
r.r.Handle("/i/invoke/function/{function_id}/{function_version}", invoke.InvokeHandler(r.cfg))
|
|
return r.r
|
|
}
|