Сервер
import (
"io"
"log"
"net/http"
"fmt"
)
func main() {
// Функции
http.HandleFunc("/ok/", sayHello)
http.HandleFunc("/bye", sayBye)
http.HandleFunc("/", StaticPage) // Link to static page
fmt.Println("Server is started!")
err := http.ListenAndServe(":5555", nil)
if err != nil {
log.Fatal(err)
}
}
func sayHello(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Hello world, this is version 1.")
}
func sayBye(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, "Bye bye, this is version 1.")
}
/********************************************************************************************************************************
*
* Статические странички
* c установкой разрешений и доступов на операции
*
* http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {http.ServeFile(w, r, r.URL.Path[1:])})
* http.HandleFunc("/static/", func(w http.ResponseWriter, r *http.Request) {http.FileServer(http.Dir("/static/"))})
*
* /static/....
*
*********************************************************************************************************************************/
func StaticPage(w http.ResponseWriter, r *http.Request) {
// Allows
w.Header().Set("Access-Control-Allow-Origin", "*")
/* Allows
if origin := r.Header().Get("Origin"); origin != "" {
w.Header().Set("Access-Control-Allow-Origin", origin)
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
w.Header().Set("Access-Control-Max-Age", "86400") // 24 hours
}
*/
// File static page
http.ServeFile(w, r, r.URL.Path[1:])
}