Files
GoLangProxy/golangproxy/server/server.go
2025-03-02 17:14:05 +00:00

40 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package server
import (
"fmt"
"net/http"
"os"
"path/filepath"
)
// StartServer launches a web server on 127.0.0.1:61147
func StartServer() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
indexPath := filepath.Join("www", "index.html")
if _, err := os.Stat(indexPath); os.IsNotExist(err) {
// Create index.html if it doesnt exist
if err := os.MkdirAll("www", 0755); err != nil {
http.Error(w, "Error creating www directory", http.StatusInternalServerError)
return
}
file, err := os.Create(indexPath)
if err != nil {
http.Error(w, "Error creating index.html", http.StatusInternalServerError)
return
}
defer file.Close()
_, err = file.WriteString("<h1>GoLangProxy is up</h1>")
if err != nil {
http.Error(w, "Error writing index.html", http.StatusInternalServerError)
return
}
}
http.ServeFile(w, r, indexPath)
})
fmt.Println("Starting simple web server on 127.0.0.1:61147")
if err := http.ListenAndServe("127.0.0.1:61147", nil); err != nil {
fmt.Println("Web server error:", err)
}
}