Skip to content

Commit

Permalink
Not perfect but it works
Browse files Browse the repository at this point in the history
  • Loading branch information
saintmalik committed Aug 6, 2022
1 parent f2343bb commit 2b941f0
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 34 deletions.
Empty file added README.md
Empty file.
79 changes: 46 additions & 33 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"embed"
"fmt"
"log"
"net/http"
Expand All @@ -12,6 +13,8 @@ import (
"github.com/saintmalik/dca-tool/model"
"github.com/spf13/cobra"
)
//go:embed all:templates/*.html
var tempFs embed.FS

// configCmd represents the config command
var configCmd = &cobra.Command{
Expand All @@ -25,31 +28,33 @@ var configCmd = &cobra.Command{
http.HandleFunc("/api", creds)
fmt.Println("Starting Server to set Binance API and Secret")
panic(http.ListenAndServe("localhost:4046", nil))
} else {
main()
} else {
main()
}
},
}

var tpl *template.Template

// var tpl *template.Template

func init() {
rootCmd.AddCommand(configCmd)
configCmd.PersistentFlags().String("credapi", "reset", "Set your Binance API and SecretKey credentials")
tpl = template.Must(template.ParseGlob("templates/*.html"))
// tpl = template.Must(template.ParseGlob("templates/*.html"))
}


func main() {
openbrowser("http://localhost:4046/")
http.HandleFunc("/", form)
fmt.Println("Starting Server to set Configurations")
panic(http.ListenAndServe("localhost:4046", nil))
}


func creds(w http.ResponseWriter, r *http.Request) {
tmpl, err := template.ParseFS(tempFs, "templates/creds.html")
if err != nil {
log.Fatal("Error loading index template: ", err)
}

model.Bapi = r.FormValue("bapi")
model.Bsecret = r.FormValue("bsecret") //picking up the value from the form

Expand All @@ -73,33 +78,39 @@ func creds(w http.ResponseWriter, r *http.Request) {
return
}

err = tpl.ExecuteTemplate(w, "creds.html", nil)
if err != nil {
fmt.Println(err)
}
tmpl.Execute(w, nil)

// err = tpl.ExecuteTemplate(w, "creds.html", nil)
// if err != nil {
// fmt.Println(err)
// }

}

func form(w http.ResponseWriter, r *http.Request) {
model.Coinid = r.FormValue("coinid")
model.Amount = r.FormValue("amount")
model.Alloted = r.FormValue("allottedpercent")
model.Buyinterval = r.FormValue("buyingintervals")
model.Fee = r.FormValue("feepercent")
model.Testvalue = r.FormValue("testing") //picking up the value from the form

f, err := os.Create("config.json")
if err != nil {
fmt.Println(err)
return
}
tmpl, err := template.ParseFS(tempFs, "templates/index.html")
if err != nil {
log.Fatal("Error loading index template: ", err)
}
model.Coinid = r.FormValue("coinid")
model.Amount = r.FormValue("amount")
model.Alloted = r.FormValue("allottedpercent")
model.Buyinterval = r.FormValue("buyingintervals")
model.Fee = r.FormValue("feepercent")
model.Testvalue = r.FormValue("testing") //picking up the value from the form

f, err := os.Create("config.json")
if err != nil {
fmt.Println(err)
return
}

l, err := f.WriteString(`{"coins":"` + model.Coinid + `","amount":"` + model.Amount + `","percent":"` + model.Alloted + `","fee":"` + model.Fee + `","testing":"` + model.Testvalue + `","buyintervals":"` + model.Buyinterval + `"}`)
if err != nil {
fmt.Println("Your Binance API and Secret are set, dont let anyone access this file on you", err)
f.Close()
return
}
l, err := f.WriteString(`{"coins":"` + model.Coinid + `","amount":"` + model.Amount + `","percent":"` + model.Alloted + `","fee":"` + model.Fee + `","testing":"` + model.Testvalue + `","buyintervals":"` + model.Buyinterval + `"}`)
if err != nil {
fmt.Println("Your Binance API and Secret are set, dont let anyone access this file on you", err)
f.Close()
return
}

fmt.Println(l, "Your DCA Configurations are set, quit the server and and run dca run")
err = f.Close()
Expand All @@ -108,10 +119,12 @@ func form(w http.ResponseWriter, r *http.Request) {
return
}

err = tpl.ExecuteTemplate(w, "index.html", nil)
if err != nil {
fmt.Println(err)
}
tmpl.Execute(w, nil)

// err = tpl.ExecuteTemplate(w, "index.html", nil)
// if err != nil {
// fmt.Println(err)
// }

}

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"api":"dvasfdsdf","secretkey":"dvdsavasdcadv"}
{"api":"","secretkey":""}

0 comments on commit 2b941f0

Please sign in to comment.