mirror of
https://github.com/rjNemo/go-pass-gen
synced 2026-06-06 02:46:40 +00:00
feat: add web server
This commit is contained in:
parent
6f04c27041
commit
aae4fbda0c
6 changed files with 93 additions and 1 deletions
43
api/handlers.go
Normal file
43
api/handlers.go
Normal file
|
|
@ -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
|
||||
}
|
||||
5
api/routes.go
Normal file
5
api/routes.go
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package api
|
||||
|
||||
func (s Server) routes() {
|
||||
s.Router.Post("/new", s.HandleNewPassword)
|
||||
}
|
||||
23
api/server.go
Normal file
23
api/server.go
Normal file
|
|
@ -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)
|
||||
}
|
||||
2
go.mod
2
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
|
||||
)
|
||||
|
||||
|
|
|
|||
4
go.sum
4
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=
|
||||
|
|
|
|||
17
main.go
17
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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue