Separating client and server package #3

The client and server package is now separated, so I can build
additional server package with `-tags server`.
This commit is contained in:
ditatompel 2024-05-18 17:59:54 +07:00
parent e0cd343be7
commit 7dea8380b8
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
8 changed files with 39 additions and 36 deletions

View file

@ -1,4 +1,4 @@
package cmd package client
import ( import (
"bytes" "bytes"
@ -30,19 +30,15 @@ func newProber(cfg *config.App) *proberClient {
return &proberClient{config: cfg} return &proberClient{config: cfg}
} }
var probeCmd = &cobra.Command{ var ProbeCmd = &cobra.Command{
Use: "probe", Use: "probe",
Short: "Run Monero node prober", Short: "Probe remote nodes",
Run: func(_ *cobra.Command, _ []string) { Run: func(cmd *cobra.Command, args []string) {
runProbe() RunProbe()
}, },
} }
func init() { func RunProbe() {
rootCmd.AddCommand(probeCmd)
}
func runProbe() {
cfg := config.AppCfg() cfg := config.AppCfg()
if cfg.ServerEndpoint == "" { if cfg.ServerEndpoint == "" {
fmt.Println("Please set SERVER_ENDPOINT in .env") fmt.Println("Please set SERVER_ENDPOINT in .env")

View file

@ -2,26 +2,26 @@ package cmd
import ( import (
"os" "os"
"xmr-remote-nodes/internal/config" "xmr-remote-nodes/cmd/client"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
const AppVer = "0.0.1" const AppVer = "0.0.1"
var rootCmd = &cobra.Command{ var Root = &cobra.Command{
Use: "xmr-nodes", Use: "xmr-nodes",
Short: "XMR Nodes", Short: "XMR Nodes",
Version: AppVer, Version: AppVer,
} }
func Execute() { func Execute() {
err := rootCmd.Execute() err := Root.Execute()
if err != nil { if err != nil {
os.Exit(1) os.Exit(1)
} }
} }
func init() { func init() {
config.LoadAll(".env") Root.AddCommand(client.ProbeCmd)
} }

View file

@ -1,4 +1,4 @@
package cmd package server
import ( import (
"bufio" "bufio"
@ -13,7 +13,7 @@ import (
"golang.org/x/term" "golang.org/x/term"
) )
var adminCmd = &cobra.Command{ var AdminCmd = &cobra.Command{
Use: "admin", Use: "admin",
Short: "Create Admin", Short: "Create Admin",
Long: `Create an admin account for WebUI access.`, Long: `Create an admin account for WebUI access.`,
@ -37,10 +37,6 @@ var adminCmd = &cobra.Command{
}, },
} }
func init() {
rootCmd.AddCommand(adminCmd)
}
func createAdmin() error { func createAdmin() error {
admin := repo.NewAdminRepo(database.GetDB()) admin := repo.NewAdminRepo(database.GetDB())
a := repo.Admin{ a := repo.Admin{

View file

@ -1,4 +1,4 @@
package cmd package server
import ( import (
"encoding/json" "encoding/json"
@ -25,7 +25,7 @@ type importData struct {
DateEntered int `json:"date_entered"` DateEntered int `json:"date_entered"`
} }
var importCmd = &cobra.Command{ var ImportCmd = &cobra.Command{
Use: "import", Use: "import",
Short: "Import Monero nodes from old API", Short: "Import Monero nodes from old API",
Long: `Import Monero nodes from old API. Long: `Import Monero nodes from old API.
@ -72,10 +72,6 @@ This command only available during migration process and will be removed in futu
}, },
} }
func init() {
rootCmd.AddCommand(importCmd)
}
func (i *importClient) processData(node importData) error { func (i *importClient) processData(node importData) error {
query := `SELECT id FROM tbl_node WHERE hostname = ? AND port = ? AND protocol = ?` query := `SELECT id FROM tbl_node WHERE hostname = ? AND port = ? AND protocol = ?`
row := i.db.QueryRow(query, node.Hostname, node.Port, node.Protocol) row := i.db.QueryRow(query, node.Hostname, node.Port, node.Protocol)

View file

@ -1,4 +1,4 @@
package cmd package server
import ( import (
"fmt" "fmt"
@ -6,6 +6,7 @@ import (
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"time" "time"
"xmr-remote-nodes/cmd"
"xmr-remote-nodes/internal/database" "xmr-remote-nodes/internal/database"
"xmr-remote-nodes/internal/repo" "xmr-remote-nodes/internal/repo"
@ -80,7 +81,7 @@ var addProbersCmd = &cobra.Command{
} }
func init() { func init() {
rootCmd.AddCommand(probersCmd) cmd.Root.AddCommand(probersCmd)
probersCmd.AddCommand(listProbersCmd) probersCmd.AddCommand(listProbersCmd)
probersCmd.AddCommand(addProbersCmd) probersCmd.AddCommand(addProbersCmd)
listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts") listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts")

View file

@ -1,4 +1,4 @@
package cmd package server
import ( import (
"fmt" "fmt"
@ -21,7 +21,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var serveCmd = &cobra.Command{ var ServeCmd = &cobra.Command{
Use: "serve", Use: "serve",
Short: "Serve the WebUI", Short: "Serve the WebUI",
Long: `This command will run HTTP server for APIs and WebUI.`, Long: `This command will run HTTP server for APIs and WebUI.`,
@ -30,10 +30,6 @@ var serveCmd = &cobra.Command{
}, },
} }
func init() {
rootCmd.AddCommand(serveCmd)
}
func serve() { func serve() {
appCfg := config.AppCfg() appCfg := config.AppCfg()
// connect to DB // connect to DB
@ -105,6 +101,6 @@ func fiberConfig() fiber.Config {
return fiber.Config{ return fiber.Config{
Prefork: config.AppCfg().Prefork, Prefork: config.AppCfg().Prefork,
ProxyHeader: config.AppCfg().ProxyHeader, ProxyHeader: config.AppCfg().ProxyHeader,
AppName: "ditatompel's XMR Nodes HTTP server " + AppVer, AppName: "ditatompel's XMR Nodes HTTP server",
} }
} }

View file

@ -1,7 +1,11 @@
package main package main
import "xmr-remote-nodes/cmd" import (
"xmr-remote-nodes/cmd"
"xmr-remote-nodes/internal/config"
)
func main() { func main() {
config.LoadAll(".env")
cmd.Execute() cmd.Execute()
} }

14
server.go Normal file
View file

@ -0,0 +1,14 @@
//go:build server
package main
import (
"xmr-remote-nodes/cmd"
"xmr-remote-nodes/cmd/server"
)
func init() {
cmd.Root.AddCommand(server.AdminCmd)
cmd.Root.AddCommand(server.ServeCmd)
cmd.Root.AddCommand(server.ImportCmd)
}