2022-03-24 23:25:02 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-04-06 22:16:56 +03:00
|
|
|
"flag"
|
2022-03-24 23:25:02 +03:00
|
|
|
"html/template"
|
2024-04-06 20:06:43 +03:00
|
|
|
"log/slog"
|
2022-03-24 23:25:02 +03:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
2024-04-06 20:06:43 +03:00
|
|
|
"gitrepo.ru/neonxp/middleware"
|
2022-03-24 23:25:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-04-06 20:06:43 +03:00
|
|
|
templates = template.Must(template.ParseFS(tplFS, "tpl/*.gohtml"))
|
2022-03-24 23:25:02 +03:00
|
|
|
packages = Packages{}
|
2024-04-06 22:16:56 +03:00
|
|
|
bindAddr = flag.String("bind", ":8080", "Address to bind")
|
|
|
|
config = flag.String("config", "/etc/pkg/config.json", "Config file path")
|
2022-03-24 23:25:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-04-06 22:24:09 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
2022-03-24 23:25:02 +03:00
|
|
|
cfg := &Config{}
|
|
|
|
|
2024-04-06 22:16:56 +03:00
|
|
|
fp, err := os.Open(*config)
|
2022-03-24 23:25:02 +03:00
|
|
|
if err != nil {
|
2024-04-06 20:06:43 +03:00
|
|
|
panic(err)
|
2022-03-24 23:25:02 +03:00
|
|
|
}
|
|
|
|
if err := json.NewDecoder(fp).Decode(cfg); err != nil {
|
2024-04-06 20:06:43 +03:00
|
|
|
panic(err)
|
2022-03-24 23:25:02 +03:00
|
|
|
}
|
|
|
|
|
2024-04-06 20:06:43 +03:00
|
|
|
mux := http.NewServeMux()
|
2022-03-24 23:25:02 +03:00
|
|
|
|
2024-04-06 20:06:43 +03:00
|
|
|
mux.HandleFunc("GET /{$}", func(w http.ResponseWriter, r *http.Request) {
|
2022-03-24 23:25:02 +03:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
templates.ExecuteTemplate(w, "index.gohtml", indexRenderContext{
|
|
|
|
Title: cfg.Title,
|
|
|
|
Packages: cfg.Packages,
|
|
|
|
Host: cfg.Host,
|
|
|
|
Doc: "//pkg.go.dev/",
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-04-06 20:06:43 +03:00
|
|
|
mux.HandleFunc("GET /{pkg}", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
pkgID := r.PathValue("pkg")
|
2022-03-24 23:25:02 +03:00
|
|
|
pkg, ok := (*cfg.Packages)[pkgID]
|
|
|
|
if !ok {
|
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
templates.ExecuteTemplate(w, "package.gohtml", pageRenderContext{
|
|
|
|
Title: cfg.Title,
|
|
|
|
Package: &pkg,
|
|
|
|
Host: cfg.Host,
|
|
|
|
Doc: "//pkg.go.dev/",
|
|
|
|
})
|
|
|
|
})
|
2024-04-06 20:06:43 +03:00
|
|
|
mux.Handle("GET /static/", http.FileServerFS(staticFS))
|
2022-03-24 23:25:02 +03:00
|
|
|
|
2024-04-06 20:06:43 +03:00
|
|
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{}))
|
2024-04-06 20:44:41 +03:00
|
|
|
h := middleware.Use(mux,
|
|
|
|
middleware.Logger(logger),
|
|
|
|
middleware.Recover(logger),
|
|
|
|
middleware.RequestID,
|
|
|
|
)
|
2022-03-24 23:25:02 +03:00
|
|
|
|
|
|
|
srv := &http.Server{
|
2024-04-06 20:06:43 +03:00
|
|
|
Handler: h,
|
2024-04-06 22:16:56 +03:00
|
|
|
Addr: *bindAddr,
|
2022-03-24 23:25:02 +03:00
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
<-ctx.Done()
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
2024-04-06 22:16:56 +03:00
|
|
|
logger.Info("server started", slog.String("bind", *bindAddr))
|
2024-04-06 20:06:43 +03:00
|
|
|
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
|
|
|
|
panic(err)
|
2022-03-24 23:25:02 +03:00
|
|
|
}
|
|
|
|
}
|