2023-01-31 07:12:29 +00:00
|
|
|
/*
|
|
|
|
* LURE - Linux User REpository
|
2023-09-20 22:38:22 +00:00
|
|
|
* Copyright (C) 2023 Elara Musayelyan
|
2023-01-31 07:12:29 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-12-18 05:01:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-12-21 05:46:04 +00:00
|
|
|
"context"
|
2022-12-18 05:01:50 +00:00
|
|
|
"flag"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
|
2023-07-11 20:15:06 +00:00
|
|
|
"github.com/go-chi/chi/v5"
|
2022-12-18 05:01:50 +00:00
|
|
|
"github.com/twitchtv/twirp"
|
2023-04-21 03:01:05 +00:00
|
|
|
"go.elara.ws/logger"
|
2023-09-22 22:17:12 +00:00
|
|
|
"go.elara.ws/lure/cmd/lure-api-server/internal/api"
|
2023-09-20 22:41:03 +00:00
|
|
|
"go.elara.ws/lure/internal/log"
|
2023-09-21 23:18:18 +00:00
|
|
|
"go.elara.ws/lure/pkg/repos"
|
2022-12-18 05:01:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
log.Logger = logger.NewPretty(os.Stderr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2022-12-21 05:46:04 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-12-18 05:01:50 +00:00
|
|
|
addr := flag.String("a", ":8080", "Listen address for API server")
|
|
|
|
logFile := flag.String("l", "", "Output file for JSON log")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if *logFile != "" {
|
|
|
|
fl, err := os.Create(*logFile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error creating log file").Err(err).Send()
|
|
|
|
}
|
|
|
|
defer fl.Close()
|
|
|
|
|
|
|
|
log.Logger = logger.NewMulti(log.Logger, logger.NewJSON(fl))
|
|
|
|
}
|
|
|
|
|
2023-09-22 22:21:34 +00:00
|
|
|
err := repos.Pull(ctx, nil)
|
2022-12-21 05:46:04 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error pulling repositories").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
sigCh := make(chan struct{}, 200)
|
|
|
|
go repoPullWorker(ctx, sigCh)
|
2023-07-11 20:15:06 +00:00
|
|
|
|
2023-05-16 19:14:37 +00:00
|
|
|
apiServer := api.NewAPIServer(
|
2023-09-19 21:28:05 +00:00
|
|
|
lureWebAPI{},
|
2022-12-18 05:01:50 +00:00
|
|
|
twirp.WithServerPathPrefix(""),
|
|
|
|
)
|
2023-07-11 20:15:06 +00:00
|
|
|
|
2023-05-16 19:14:37 +00:00
|
|
|
r := chi.NewRouter()
|
|
|
|
r.With(allowAllCORSHandler, withAcceptLanguage).Handle("/*", apiServer)
|
|
|
|
r.Post("/webhook", handleWebhook(sigCh))
|
2023-09-19 21:28:05 +00:00
|
|
|
r.Get("/badge/{repo}/{pkg}", handleBadge())
|
2022-12-18 05:01:50 +00:00
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", *addr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error starting listener").Err(err).Send()
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Starting HTTP API server").Str("addr", ln.Addr().String()).Send()
|
2022-12-21 05:46:04 +00:00
|
|
|
|
2023-05-16 19:14:37 +00:00
|
|
|
err = http.Serve(ln, r)
|
2022-12-18 05:01:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error while running server").Err(err).Send()
|
|
|
|
}
|
|
|
|
}
|
2022-12-20 22:02:57 +00:00
|
|
|
|
|
|
|
func allowAllCORSHandler(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
res.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
res.Header().Set("Access-Control-Allow-Headers", "*")
|
|
|
|
if req.Method == http.MethodOptions {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
h.ServeHTTP(res, req)
|
|
|
|
})
|
|
|
|
}
|
2023-01-12 02:07:49 +00:00
|
|
|
|
2023-01-29 05:08:35 +00:00
|
|
|
type (
|
|
|
|
acceptLanguageKey struct{}
|
|
|
|
langParameterKey struct{}
|
|
|
|
)
|
2023-01-12 02:07:49 +00:00
|
|
|
|
|
|
|
func withAcceptLanguage(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := req.Context()
|
|
|
|
|
|
|
|
langs := req.Header.Get("Accept-Language")
|
|
|
|
ctx = context.WithValue(ctx, acceptLanguageKey{}, langs)
|
2023-01-29 05:08:35 +00:00
|
|
|
|
2023-01-12 02:07:49 +00:00
|
|
|
lang := req.URL.Query().Get("lang")
|
|
|
|
ctx = context.WithValue(ctx, langParameterKey{}, lang)
|
2023-01-29 05:08:35 +00:00
|
|
|
|
2023-01-12 02:07:49 +00:00
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
h.ServeHTTP(res, req)
|
|
|
|
})
|
|
|
|
}
|