lure/cmd/lure-api-server/main.go

119 lines
2.9 KiB
Go
Raw Normal View History

/*
* LURE - Linux User REpository
* Copyright (C) 2023 Arsen Musayelyan
*
* 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"
"github.com/twitchtv/twirp"
2023-04-21 03:01:05 +00:00
"go.elara.ws/logger"
"go.elara.ws/logger/log"
"go.elara.ws/lure/internal/api"
"go.elara.ws/lure/internal/repos"
"github.com/go-chi/chi/v5"
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))
}
2022-12-21 05:46:04 +00:00
err := repos.Pull(ctx, gdb, cfg.Repos)
if err != nil {
log.Fatal("Error pulling repositories").Err(err).Send()
}
sigCh := make(chan struct{}, 200)
go repoPullWorker(ctx, sigCh)
apiServer := api.NewAPIServer(
2022-12-18 05:01:50 +00:00
lureWebAPI{db: gdb},
twirp.WithServerPathPrefix(""),
)
r := chi.NewRouter()
r.With(allowAllCORSHandler, withAcceptLanguage).Handle("/*", apiServer)
r.Post("/webhook", handleWebhook(sigCh))
r.Get("/badge/{repo}/{pkg}", handleBadge(gdb))
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
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)
})
}