Skip to content

Commit

Permalink
add UpdateEndpoints
Browse files Browse the repository at this point in the history
Signed-off-by: YifanYang6 <[email protected]>
  • Loading branch information
YifanYang6 committed Sep 25, 2024
1 parent 7a780fc commit 7877e63
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 7 deletions.
34 changes: 30 additions & 4 deletions server/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ package main

import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
"log"
"os"
"strings"
"text/tabwriter"

"github.com/AlecAivazis/survey/v2"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/cobra"
)

func Setup() *cobra.Command {
Expand Down Expand Up @@ -224,6 +225,31 @@ func GetAllUsers() *cobra.Command {
return getAllUsersCmd
}

func UpdateEndpoints() *cobra.Command {
var updateEndpointsCmd = &cobra.Command{
Use: "updateendpoints",
Short: "Update user endpoints",
Run: func(cmd *cobra.Command, args []string) {
userManager, err := NewUserManager("./users.db")
if err != nil {
log.Fatal(err)
}
defer userManager.db.Close()
serverConfig, err := LoadServerConfig("server.yaml")
if err != nil {
log.Fatal(err)
}
err = userManager.UpdateUserEndpoints(*serverConfig)
if err != nil {
log.Fatal(err)
}

fmt.Println("User endpoints updated successfully")
},
}
return updateEndpointsCmd
}

func Server() *cobra.Command {
serverCmd := &cobra.Command{
Use: "server",
Expand Down
23 changes: 22 additions & 1 deletion server/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"os"

"github.com/gin-gonic/gin"
)

type AddUserRequest struct {
Expand Down Expand Up @@ -220,3 +221,23 @@ func getAllRoutesHandler(c *gin.Context) {
}
c.JSON(http.StatusOK, Response{Message: "Routes retrieved successfully", Data: routes})
}

func updateUserEndpointsHandler(c *gin.Context) {
userManager, err := NewUserManager("./users.db")
if err != nil {
c.JSON(http.StatusInternalServerError, Response{Message: "Internal Server Error"})
return
}
defer userManager.db.Close()
serverConfig, err := LoadServerConfig("./server.yaml")
if err != nil {
c.JSON(http.StatusInternalServerError, Response{Message: "Internal Server Error"})
return
}
err = userManager.UpdateUserEndpoints(*serverConfig)
if err != nil {
c.JSON(http.StatusInternalServerError, Response{Message: "Internal Server Error"})
return
}
c.JSON(http.StatusOK, Response{Message: "User endpoints updated successfully"})
}
5 changes: 3 additions & 2 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package main

import (
"fmt"
_ "modernc.org/sqlite"
"os"

_ "modernc.org/sqlite"

"github.com/spf13/cobra"
)

func main() {
var rootCmd = &cobra.Command{Use: "vpn-tool"}

rootCmd.AddCommand(Setup(), Add(), Delete(), Get(), GetAllUsers(), Server())
rootCmd.AddCommand(Setup(), Add(), Delete(), Get(), GetAllUsers(), Server(), UpdateEndpoints())

if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
23 changes: 23 additions & 0 deletions server/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,29 @@ func (um *UserManager) UpdateUser(user UserConfig) error {
return err
}

func (um *UserManager) UpdateUserEndpoints(serverConfig ServerConfig) error {
serverIP := serverConfig.ServerIP
stmt, err := um.db.Prepare("UPDATE users SET endpoint = ? WHERE user_id = ?")
if err != nil {
return err
}
defer stmt.Close()

users, err := um.GetAllUsers()
if err != nil {
return err
}

for _, user := range users {
_, err := stmt.Exec(serverIP, user.UserID)
if err != nil {
return err
}
}

return nil
}

func (um *UserManager) DeleteUser(id string) error {
stmt, err := um.db.Prepare("DELETE FROM users WHERE user_id = ?")
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions web/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,19 @@
const config = data.data.user_config;
await navigator.clipboard.writeText(config);
}
async function updateUserEndpoints() {
const response = await fetch('http://localhost:8080/api/updateendpoints', {
method: 'POST',
});
const data = await response.json();
if (!response.ok) {
alert(data.message);
} else {
alert("User endpoints updated successfully");
}
await fetchUsers();
}
</script>

<main class="max-w-2xl mx-auto p-4 bg-gray-900 text-white rounded-lg shadow-lg">
Expand Down

0 comments on commit 7877e63

Please sign in to comment.