refactor(routor): split routor from main.go
This commit is contained in:
@@ -19,7 +19,7 @@ type InstanceInfo struct {
|
|||||||
ServerAddr string `json:"serverAddr"`
|
ServerAddr string `json:"serverAddr"`
|
||||||
ServerPort string `json:"serverPort"`
|
ServerPort string `json:"serverPort"`
|
||||||
AuthMethod string `json:"auth_method"`
|
AuthMethod string `json:"auth_method"`
|
||||||
BootAtStart bool `json:"bootAtStart"`
|
// BootAtStart bool `json:"bootAtStart"`
|
||||||
RunUser string `json:"runUser"`
|
RunUser string `json:"runUser"`
|
||||||
Additional map[string]interface{} `json:"additionalProperties"`
|
Additional map[string]interface{} `json:"additionalProperties"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -86,56 +85,3 @@ func main() {
|
|||||||
log.Println("server stopped")
|
log.Println("server stopped")
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupRoutes() {
|
|
||||||
http.HandleFunc("/register", RegisterHandler)
|
|
||||||
http.HandleFunc("/login", LoginHandler)
|
|
||||||
|
|
||||||
http.HandleFunc("/frpcAct/instanceMgr/create", CreateInstanceHandler)
|
|
||||||
http.HandleFunc("/frpcAct/instanceMgr/list", ListInstancesHandler)
|
|
||||||
|
|
||||||
http.HandleFunc("/frpcAct/instanceMgr/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
path := r.URL.Path
|
|
||||||
if len(path) < len("/frpcAct/instanceMgr/") {
|
|
||||||
SendErrorResponse(w, http.StatusNotFound, "invalid path")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
remainingPath := path[len("/frpcAct/instanceMgr/"):]
|
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
|
||||||
if remainingPath == "create" {
|
|
||||||
CreateInstanceHandler(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if remainingPath == "list" {
|
|
||||||
ListInstancesHandler(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.HasSuffix(remainingPath, "/delete") {
|
|
||||||
instanceName := strings.TrimSuffix(remainingPath, "/delete")
|
|
||||||
instanceName = strings.Trim(instanceName, "/")
|
|
||||||
DeleteInstanceHandler(w, r, instanceName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(remainingPath, "/modify/") {
|
|
||||||
parts := strings.SplitN(remainingPath, "/modify/", 2)
|
|
||||||
if len(parts) == 2 {
|
|
||||||
instanceName := strings.Trim(parts[0], "/")
|
|
||||||
ModifyInstanceHandler(w, r, instanceName)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
instanceName := strings.Trim(remainingPath, "/")
|
|
||||||
if instanceName != "" {
|
|
||||||
ListInstancesHandler(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
SendErrorResponse(w, http.StatusNotFound, "endpoint not found")
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,60 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupRoutes() {
|
||||||
|
http.HandleFunc("/register", RegisterHandler)
|
||||||
|
http.HandleFunc("/login", LoginHandler)
|
||||||
|
|
||||||
|
http.HandleFunc("/frpcAct/instanceMgr/create", CreateInstanceHandler)
|
||||||
|
http.HandleFunc("/frpcAct/instanceMgr/list", ListInstancesHandler)
|
||||||
|
|
||||||
|
http.HandleFunc("/frpcAct/instanceMgr/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
path := r.URL.Path
|
||||||
|
if len(path) < len("/frpcAct/instanceMgr/") {
|
||||||
|
SendErrorResponse(w, http.StatusNotFound, "invalid path")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
remainingPath := path[len("/frpcAct/instanceMgr/"):]
|
||||||
|
|
||||||
|
if r.Method == http.MethodPost {
|
||||||
|
if remainingPath == "create" {
|
||||||
|
CreateInstanceHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if remainingPath == "list" {
|
||||||
|
ListInstancesHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.HasSuffix(remainingPath, "/delete") {
|
||||||
|
instanceName := strings.TrimSuffix(remainingPath, "/delete")
|
||||||
|
instanceName = strings.Trim(instanceName, "/")
|
||||||
|
DeleteInstanceHandler(w, r, instanceName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.Contains(remainingPath, "/modify/") {
|
||||||
|
parts := strings.SplitN(remainingPath, "/modify/", 2)
|
||||||
|
if len(parts) == 2 {
|
||||||
|
instanceName := strings.Trim(parts[0], "/")
|
||||||
|
ModifyInstanceHandler(w, r, instanceName)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
instanceName := strings.Trim(remainingPath, "/")
|
||||||
|
if instanceName != "" {
|
||||||
|
ListInstancesHandler(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
SendErrorResponse(w, http.StatusNotFound, "endpoint not found")
|
||||||
|
})
|
||||||
|
}
|
||||||
Binary file not shown.
Reference in New Issue
Block a user