first commit
This commit is contained in:
59
cmd/server/api/api.go
Normal file
59
cmd/server/api/api.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mirror-sync/pkg/constants"
|
||||
"mirror-sync/pkg/remote/obj"
|
||||
"net/http"
|
||||
"runtime"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/go-chi/chi/v5/middleware"
|
||||
)
|
||||
|
||||
type (
|
||||
HTTPServer struct {
|
||||
Server *http.Server
|
||||
}
|
||||
)
|
||||
|
||||
func NewServer(port int) *HTTPServer {
|
||||
s := &HTTPServer{}
|
||||
router := chi.NewRouter()
|
||||
router.NotFound(func(writer http.ResponseWriter, request *http.Request) {
|
||||
notFound("id not found", writer, request)
|
||||
})
|
||||
router.MethodNotAllowed(func(writer http.ResponseWriter, request *http.Request) {
|
||||
methodNotAllowed(writer, request)
|
||||
})
|
||||
router.Use(middleware.Logger)
|
||||
router.Use(recoverMiddleware)
|
||||
router.Use(middleware.GetHead)
|
||||
router.Use(middleware.Compress(5, "application/gzip"))
|
||||
router.Use(middleware.Heartbeat("/heartbeat"))
|
||||
router.Route("/api", func(routerAPI chi.Router) {
|
||||
routerAPI.Route("/v1", func(r chi.Router) {
|
||||
// Get information about the server
|
||||
r.Get("/version", s.Information)
|
||||
r.Route("/sync", func(r chi.Router) {
|
||||
|
||||
})
|
||||
})
|
||||
})
|
||||
s.Server = &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", port),
|
||||
Handler: router,
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *HTTPServer) Information(w http.ResponseWriter, r *http.Request) {
|
||||
info := obj.SystemInformation{
|
||||
Version: constants.Version,
|
||||
APIVersion: constants.ApiVersion,
|
||||
GoVersion: runtime.Version(),
|
||||
OSName: runtime.GOOS,
|
||||
OSArchitecture: runtime.GOARCH,
|
||||
}
|
||||
ok(info, w, r)
|
||||
}
|
||||
15
cmd/server/api/middlewares.go
Normal file
15
cmd/server/api/middlewares.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import "net/http"
|
||||
|
||||
func recoverMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
121
cmd/server/api/responses.go
Normal file
121
cmd/server/api/responses.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"mirror-sync/pkg/remote/obj"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func internalServerError(w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusInternalServerError,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Error: "Internal Server Error",
|
||||
Message: "The server encountered an unexpected condition that prevented it from fulfilling the request.",
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(payload); err != nil {
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func notFound(message string, w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusNotFound,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Error: "Not Found",
|
||||
Message: message,
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(payload); err != nil {
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusMethodNotAllowed,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Error: "Method Not Allowed",
|
||||
Message: "The server knows the request method, but the target resource doesn't support this method",
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(payload); err != nil {
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func unauthorized(w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusUnauthorized,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Error: "Unauthorized",
|
||||
Message: "The request has not been completed because it lacks valid authentication credentials for the requested resource.",
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.Header().Add("WWW-Authenticate", "Custom realm=\"loginUserHandler via /api/login\"")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(payload); err != nil {
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func ok(o interface{}, w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPObject{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusOK,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Data: o,
|
||||
}
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(payload); err != nil {
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func badRequest(message string, w http.ResponseWriter, r *http.Request) {
|
||||
payload := obj.HTTPError{
|
||||
HTTPCore: obj.HTTPCore{
|
||||
Status: http.StatusBadRequest,
|
||||
Path: r.RequestURI,
|
||||
Timestamp: time.Now(),
|
||||
},
|
||||
Error: "Bad Request",
|
||||
Message: message,
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
e := json.NewEncoder(w)
|
||||
if err := e.Encode(payload); err != nil {
|
||||
slog.Error(err.Error())
|
||||
}
|
||||
}
|
||||
73
cmd/server/core/git/git.go
Normal file
73
cmd/server/core/git/git.go
Normal file
@@ -0,0 +1,73 @@
|
||||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/go-git/go-git/v6"
|
||||
"github.com/go-git/go-git/v6/config"
|
||||
"github.com/go-git/go-git/v6/plumbing/transport"
|
||||
"github.com/go-git/go-git/v6/plumbing/transport/http"
|
||||
"github.com/go-git/go-git/v6/storage/memory"
|
||||
)
|
||||
|
||||
type (
|
||||
Repository struct {
|
||||
src string
|
||||
dst string
|
||||
auth Authentication
|
||||
}
|
||||
|
||||
Authentication interface {
|
||||
Value() transport.AuthMethod
|
||||
}
|
||||
|
||||
TokenAuthentication struct {
|
||||
username string
|
||||
token string
|
||||
}
|
||||
|
||||
NoAuthentication struct{}
|
||||
)
|
||||
|
||||
func Sync(r Repository) error {
|
||||
repo, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
|
||||
URL: r.src,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to clone repository from source: %w", err)
|
||||
}
|
||||
|
||||
m, err := repo.CreateRemote(&config.RemoteConfig{
|
||||
Name: "mirror",
|
||||
Mirror: true,
|
||||
URLs: []string{
|
||||
r.dst,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create remote: %w", err)
|
||||
}
|
||||
|
||||
err = m.Push(&git.PushOptions{
|
||||
RemoteName: "mirror",
|
||||
Auth: r.auth.Value(),
|
||||
RefSpecs: []config.RefSpec{"+refs/*:refs/*"},
|
||||
Force: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to push to mirror server: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a TokenAuthentication) Value() transport.AuthMethod {
|
||||
return &http.BasicAuth{
|
||||
Username: a.username,
|
||||
Password: a.token,
|
||||
}
|
||||
}
|
||||
|
||||
func (NoAuthentication) Value() transport.AuthMethod {
|
||||
return nil
|
||||
}
|
||||
21
cmd/server/main.go
Normal file
21
cmd/server/main.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"mirror-sync/cmd/server/api"
|
||||
"mirror-sync/pkg/constants"
|
||||
"os"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("mirror-sync daemon -- v%s.%s.%s\n\n", constants.Version, runtime.GOOS, runtime.GOARCH)
|
||||
|
||||
s := api.NewServer(8080)
|
||||
|
||||
fmt.Println("daemon listening to :8080")
|
||||
if err := s.Server.ListenAndServe(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "failed to start server:", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user