58 lines
1.2 KiB
Go
58 lines
1.2 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
type UserStore interface {
|
||
|
GetUserName(name string) string
|
||
|
ShowIP(name string) string
|
||
|
}
|
||
|
|
||
|
type ServerHandler struct {
|
||
|
store UserStore
|
||
|
http.Handler
|
||
|
userIpMap map[string]string
|
||
|
}
|
||
|
|
||
|
func NewLoginServer(store UserStore) *ServerHandler {
|
||
|
s := &ServerHandler{
|
||
|
store,
|
||
|
http.NewServeMux(),
|
||
|
nil,
|
||
|
}
|
||
|
router := http.NewServeMux()
|
||
|
router.Handle("/", http.HandlerFunc(s.homeHandler))
|
||
|
s.Handler = router
|
||
|
return s
|
||
|
}
|
||
|
|
||
|
func (s *ServerHandler) homeHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
switch r.Method {
|
||
|
case http.MethodPost:
|
||
|
s.updateIpHandler(w, r)
|
||
|
case http.MethodGet:
|
||
|
s.sendHomeHandler(w, r)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *ServerHandler) sendHomeHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
w.WriteHeader(http.StatusOK)
|
||
|
w.Write([]byte(r.RemoteAddr))
|
||
|
}
|
||
|
|
||
|
func (s *ServerHandler) updateIpHandler(w http.ResponseWriter, r *http.Request) {
|
||
|
//userName := strings.TrimPrefix(r.URL.Path, "/")
|
||
|
//if ! s.store.GetUserName(userName)
|
||
|
w.WriteHeader(http.StatusAccepted)
|
||
|
w.Write([]byte(r.RemoteAddr))
|
||
|
}
|
||
|
|
||
|
func (s *ServerHandler) GetUserName(name string) string {
|
||
|
return name
|
||
|
}
|
||
|
|
||
|
func (s *ServerHandler) ShowIP(name string) string {
|
||
|
return s.userIpMap[name]
|
||
|
}
|