Sfoglia il codice sorgente

add root file handler before StartPay

tags/v0.5
patrick 5 anni fa
parent
commit
412b491bfd
2 ha cambiato i file con 37 aggiunte e 1 eliminazioni
  1. +34
    -0
      fs_wrapper_handler.go
  2. +3
    -1
      main.go

+ 34
- 0
fs_wrapper_handler.go Vedi File

package main

import (
"net/http"
)

type NotFoundRedirectRespWr struct {
http.ResponseWriter // We embed http.ResponseWriter
status int
}

func (w *NotFoundRedirectRespWr) WriteHeader(status int) {
w.status = status // Store the status for our own use
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}

func (w *NotFoundRedirectRespWr) Write(p []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(p)
}
return len(p), nil // Lie that we successfully written it
}

func wrapHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/" {
StartPay(w, r)
} else {
h.ServeHTTP(w, r)
}
}
}

+ 3
- 1
main.go Vedi File

func main() { func main() {
//readConfig() //readConfig()
readConfigForTest() readConfigForTest()
http.HandleFunc("/", StartPay)
http.HandleFunc("/choosePayment", choosePayment) http.HandleFunc("/choosePayment", choosePayment)
http.HandleFunc("/rpnNameAndCard", rpnNameAndCard) http.HandleFunc("/rpnNameAndCard", rpnNameAndCard)
// http.HandleFunc("/", Index) // http.HandleFunc("/", Index)
// http.HandleFunc("/insert", Insert) // http.HandleFunc("/insert", Insert)
// http.HandleFunc("/update", Update) // http.HandleFunc("/update", Update)
// http.HandleFunc("/delete", Delete) // http.HandleFunc("/delete", Delete)
fs := wrapHandler(http.FileServer(http.Dir("./PG")))
http.HandleFunc("/", fs)
http.ListenAndServe(":8080", nil) http.ListenAndServe(":8080", nil)
log.Println("Server started on: http://localhost:8080") log.Println("Server started on: http://localhost:8080")
} }

Loading…
Annulla
Salva