diff --git a/api/handlers.go b/api/handlers.go new file mode 100644 index 0000000..2729b7f --- /dev/null +++ b/api/handlers.go @@ -0,0 +1,43 @@ +package api + +import ( + "log" + "net/http" + + "github.com/go-chi/render" + + "github.com/rjNemo/go-pass-gen/passgen" +) + +func (s Server) HandleNewPassword(w http.ResponseWriter, r *http.Request) { + params := &PasswordParams{} + if err := render.Bind(r, params); err != nil { + log.Fatal(err) + } + + password := passgen.NewPasswordGenerator(passgen.Options{Length: params.Length, + WithNumbers: params.WithNumbers}).NewPassword() + + render.Status(r, http.StatusAccepted) + render.Render(w, r, &PasswordResponse{Password: password}) +} + +type PasswordParams struct { + Length int `json:"length"` + WithNumbers bool `json:"with_numbers"` +} + +func (p PasswordParams) Bind(*http.Request) error { + return nil +} + +type PasswordResponse struct { + Success bool `json:"success"` + Error *string `json:"error"` + Password string `json:"password"` +} + +func (resp PasswordResponse) Render(http.ResponseWriter, *http.Request) error { + resp.Success = true + return nil +} diff --git a/api/routes.go b/api/routes.go new file mode 100644 index 0000000..3bade64 --- /dev/null +++ b/api/routes.go @@ -0,0 +1,5 @@ +package api + +func (s Server) routes() { + s.Router.Post("/new", s.HandleNewPassword) +} diff --git a/api/server.go b/api/server.go new file mode 100644 index 0000000..e83ba89 --- /dev/null +++ b/api/server.go @@ -0,0 +1,23 @@ +package api + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" +) + +type Server struct { + Router *chi.Mux +} + +func NewServer() *Server { + s := &Server{Router: chi.NewRouter()} + s.Router.Use(middleware.Logger) + s.routes() + return s +} + +func (s Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { + s.Router.ServeHTTP(w, r) +} diff --git a/go.mod b/go.mod index f5fe70d..2495e2d 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,8 @@ go 1.17 require ( github.com/fatih/color v1.13.0 + github.com/go-chi/chi/v5 v5.0.4 + github.com/go-chi/render v1.0.1 github.com/spf13/cobra v1.2.1 ) diff --git a/go.sum b/go.sum index fbfaabe..f5949f5 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,10 @@ github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/go-chi/chi/v5 v5.0.4 h1:5e494iHzsYBiyXQAHHuI4tyJS9M3V84OuX3ufIIGHFo= +github.com/go-chi/chi/v5 v5.0.4/go.mod h1:DslCQbL2OYiznFReuXYUmQ2hGd1aDpCnlMNITLSKoi8= +github.com/go-chi/render v1.0.1 h1:4/5tis2cKaNdnv9zFLfXzcquC9HbeZgCnxGnKrltBS8= +github.com/go-chi/render v1.0.1/go.mod h1:pq4Rr7HbnsdaeHagklXub+p6Wd16Af5l9koip1OvJns= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= diff --git a/main.go b/main.go index 701b512..40544a8 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,22 @@ package main -import "github.com/rjNemo/go-pass-gen/cmd" +import ( + "net/http" + + "github.com/rjNemo/go-pass-gen/api" + "github.com/rjNemo/go-pass-gen/cmd" +) func main() { + //serveWeb() + cli() +} + +func cli() { cmd.Execute() } + +func serveWeb() { + s := api.NewServer() + http.ListenAndServe(":8080", s) +}