47 lines
1.0 KiB
Go
47 lines
1.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"net/http"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
func http_HandleFunc(url_path string, filepath string, is_mainpage bool, content_type string) {
|
||
|
if is_mainpage {
|
||
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||
|
w.Header().Add("Content-Type", content_type)
|
||
|
io.WriteString(w, html_replacer(file_read(filepath)))
|
||
|
})
|
||
|
} else {
|
||
|
s := new(submit)
|
||
|
s.data = "null"
|
||
|
http.HandleFunc("/"+url_path, func(w http.ResponseWriter, r *http.Request) {
|
||
|
if r.Method == "POST" {
|
||
|
if err := r.ParseForm(); err != nil {
|
||
|
fmt.Fprintf(w, "ParseForm() err: %v", err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
|
w.Header().Add("Content-Type", content_type)
|
||
|
|
||
|
io.WriteString(w, html_replacer(file_read(filepath)))
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func http_start(address string) { // address could be = ":8080"
|
||
|
handler()
|
||
|
|
||
|
err := http.ListenAndServe(address, nil)
|
||
|
|
||
|
if errors.Is(err, http.ErrServerClosed) {
|
||
|
fmt.Printf("server closed\n")
|
||
|
} else if err != nil {
|
||
|
fmt.Printf("error starting server: %s\n", err)
|
||
|
os.Exit(1)
|
||
|
}
|
||
|
}
|