diff --git a/http.go b/api/http.go similarity index 95% rename from http.go rename to api/http.go index f9e6461..e8245bc 100644 --- a/http.go +++ b/api/http.go @@ -1,4 +1,4 @@ -package gpaste +package api import ( "encoding/json" @@ -6,6 +6,7 @@ import ( "net/http" "strings" + "git.t-juice.club/torjus/gpaste" "git.t-juice.club/torjus/gpaste/files" "git.t-juice.club/torjus/gpaste/users" "github.com/go-chi/chi/v5" @@ -17,14 +18,14 @@ import ( type HTTPServer struct { Files files.FileStore Users users.UserStore - Auth *AuthService - config *ServerConfig + Auth *gpaste.AuthService + config *gpaste.ServerConfig Logger *zap.SugaredLogger AccessLogger *zap.SugaredLogger http.Server } -func NewHTTPServer(cfg *ServerConfig) *HTTPServer { +func NewHTTPServer(cfg *gpaste.ServerConfig) *HTTPServer { srv := &HTTPServer{ config: cfg, Logger: zap.NewNop().Sugar(), @@ -32,7 +33,7 @@ func NewHTTPServer(cfg *ServerConfig) *HTTPServer { } srv.Files = files.NewMemoryFileStore() srv.Users = users.NewMemoryUserStore() - srv.Auth = NewAuthService(srv.Users, []byte(srv.config.SigningSecret)) + srv.Auth = gpaste.NewAuthService(srv.Users, []byte(srv.config.SigningSecret)) // Create initial user // TODO: Do properly @@ -202,7 +203,7 @@ func (s *HTTPServer) HandlerAPIUserCreate(w http.ResponseWriter, r *http.Request defer r.Body.Close() level, err := AuthLevelFromRequest(r) - if err != nil || level < AuthLevelAdmin { + if err != nil || level < gpaste.AuthLevelAdmin { w.WriteHeader(http.StatusUnauthorized) return } diff --git a/http_test.go b/api/http_test.go similarity index 97% rename from http_test.go rename to api/http_test.go index 3093f31..6309384 100644 --- a/http_test.go +++ b/api/http_test.go @@ -1,4 +1,4 @@ -package gpaste_test +package api_test import ( "bytes" @@ -11,6 +11,7 @@ import ( "testing" "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/api" "git.t-juice.club/torjus/gpaste/users" ) @@ -22,7 +23,7 @@ func TestHandlers(t *testing.T) { }, URL: "http://localhost:8080", } - hs := gpaste.NewHTTPServer(cfg) + hs := api.NewHTTPServer(cfg) t.Run("HandlerIndex", func(t *testing.T) { rr := httptest.NewRecorder() diff --git a/middleware.go b/api/middleware.go similarity index 86% rename from middleware.go rename to api/middleware.go index 31b5ff0..c5296b6 100644 --- a/middleware.go +++ b/api/middleware.go @@ -1,4 +1,4 @@ -package gpaste +package api import ( "context" @@ -7,6 +7,7 @@ import ( "strings" "time" + "git.t-juice.club/torjus/gpaste" "github.com/go-chi/chi/v5/middleware" ) @@ -65,7 +66,7 @@ func (s *HTTPServer) MiddlewareAuthentication(next http.Handler) http.Handler { } ctx := context.WithValue(r.Context(), authCtxUsername, claims.Subject) - ctx = context.WithValue(ctx, authCtxAuthLevel, AuthLevelUser) + ctx = context.WithValue(ctx, authCtxAuthLevel, gpaste.AuthLevelUser) withCtx := r.WithContext(ctx) s.Logger.Debugw("Request is authenticated.", "req_id", reqID, "username", claims.Subject) @@ -88,14 +89,14 @@ func UsernameFromRequest(r *http.Request) (string, error) { return username, nil } -func AuthLevelFromRequest(r *http.Request) (AuthLevel, error) { +func AuthLevelFromRequest(r *http.Request) (gpaste.AuthLevel, error) { rawLevel := r.Context().Value(authCtxAuthLevel) if rawLevel == nil { - return AuthLevelUnset, fmt.Errorf("no username") + return gpaste.AuthLevelUnset, fmt.Errorf("no username") } - level, ok := rawLevel.(AuthLevel) + level, ok := rawLevel.(gpaste.AuthLevel) if !ok { - return AuthLevelUnset, fmt.Errorf("no username") + return gpaste.AuthLevelUnset, fmt.Errorf("no username") } return level, nil } diff --git a/cmd/client/client.go b/cmd/client/client.go index 02dd944..538a88c 100644 --- a/cmd/client/client.go +++ b/cmd/client/client.go @@ -13,7 +13,7 @@ import ( "syscall" "time" - "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/api" "github.com/google/uuid" "github.com/urfave/cli/v2" "golang.org/x/term" @@ -201,7 +201,7 @@ func ActionUserCreate(c *cli.Context) error { defer cancel() body := new(bytes.Buffer) - requestData := &gpaste.RequestAPIUserCreate{ + requestData := &api.RequestAPIUserCreate{ Username: username, Password: password, } diff --git a/cmd/server/server.go b/cmd/server/server.go index 7539a21..2ba9aac 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -10,6 +10,7 @@ import ( "time" "git.t-juice.club/torjus/gpaste" + "git.t-juice.club/torjus/gpaste/api" "github.com/urfave/cli/v2" "go.uber.org/zap" "go.uber.org/zap/zapcore" @@ -68,7 +69,7 @@ func ActionServe(c *cli.Context) error { defer httpShutdownCancel() go func() { - srv := gpaste.NewHTTPServer(cfg) + srv := api.NewHTTPServer(cfg) srv.Addr = cfg.ListenAddr srv.Logger = serverLogger srv.AccessLogger = accessLogger