Skip to content

Commit

Permalink
feat(cmd/random/server): Create server with /v1/slu_random_password e…
Browse files Browse the repository at this point in the history
…ndpoint
  • Loading branch information
ondrejsika committed Apr 8, 2024
1 parent c1ce0fd commit c11433f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ import (
_ "github.com/sikalabs/slu/cmd/random"
_ "github.com/sikalabs/slu/cmd/random/int"
_ "github.com/sikalabs/slu/cmd/random/password"
_ "github.com/sikalabs/slu/cmd/random/server"
_ "github.com/sikalabs/slu/cmd/random/string"
_ "github.com/sikalabs/slu/cmd/rke2"
_ "github.com/sikalabs/slu/cmd/rmline"
Expand Down
52 changes: 52 additions & 0 deletions cmd/random/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package example_server

import (
"fmt"
"net/http"
"os"
"strconv"

random_cmd "github.com/sikalabs/slu/cmd/random"
"github.com/sikalabs/slu/utils/random_utils"
"github.com/sikalabs/slu/version"
"github.com/spf13/cobra"
)

var FlagPort int

var Cmd = &cobra.Command{
Use: "server",
Short: "Server with random data endpoints",
Args: cobra.NoArgs,
Run: func(c *cobra.Command, args []string) {
portStr := strconv.Itoa(FlagPort)
hostname, _ := os.Hostname()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "[slu "+version.Version+"] Server with random data endpoints! %s %s \n", hostname, portStr)
fmt.Printf("RemoteAddr=%s\n", r.RemoteAddr)
})

http.HandleFunc("/v1/slu_random_password", func(w http.ResponseWriter, r *http.Request) {
password, err := random_utils.RandomPassword()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "%s\n", password)
})

fmt.Println("[slu " + version.Version + "] Server started on 0.0.0.0:" + portStr + ", see http://127.0.0.1:" + portStr)
http.ListenAndServe(":"+portStr, nil)
},
}

func init() {
random_cmd.Cmd.AddCommand(Cmd)
Cmd.PersistentFlags().IntVarP(
&FlagPort,
"port",
"p",
8000,
"Listen on port",
)
}

0 comments on commit c11433f

Please sign in to comment.