backend need to be rewrited to go

legacy code in archive folder
This commit is contained in:
SnippetsX 2024-12-04 00:37:52 +03:00
parent 51a73350ae
commit 39c1b7d2d0
11 changed files with 65 additions and 0 deletions

38
cmd/admin_backend/main.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"admin_dashboard_backend/pkg/handlers"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
func main() {
configFile, err := os.ReadFile("config.json")
if err != nil {
log.Fatal("Error loading config.json file:", err)
}
var config struct {
Port json.Number `json:"serverPort"`
}
if err := json.Unmarshal(configFile, &config); err != nil {
log.Fatal("Error parsing config.json:", err)
}
port := config.Port.String()
if port == "" {
port = "84" // default port
}
// Define routes
http.HandleFunc("/welcome", handlers.HandlersWelcome)
// Start server
fmt.Printf("Server is running on port %s...\n", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module admin_dashboard_backend
go 1.23.4

24
pkg/handlers/welcome.go Normal file
View File

@ -0,0 +1,24 @@
package handlers
import (
"encoding/json"
"net/http"
)
type Response struct {
Message string `json:"message"`
}
func HandlersWelcome(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
return
}
response := Response{
Message: "Welcome to the API!",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}