golang 웹 초기 분석

2112 단어 Webgolang
golang 웹 부분 초기 분석
package main
import "fmt"
import "log"
import "net/http"
import "time"


type HttpHander struct {}

var urlMapping map[string] func(http.ResponseWriter,*http.Request)


func main() {
	    
		server := &http.Server{}
		server.Addr = ":8080"
		server.Handler = HttpHander{}
		server.ReadTimeout = 5 *time.Second
		server.WriteTimeout = 5 *time.Second
		urlMapping = make(map[string] func(http.ResponseWriter,*http.Request))
		urlMapping["/get"] = Get 
		urlMapping["/post"] = Post 
		urlMapping["/delete"] = Delete 
		urlMapping["/put"] = Put
		urlMapping["/update"] = Update 
		err := server.ListenAndServe()
		if err != nil {
			fmt.Println("========")
			log.Println(err)
		}
	
}


func(HttpHander) ServeHTTP(response http.ResponseWriter, request *http.Request) {
	
	v,ok:= urlMapping[request.URL.String()]
	if ok {
		v(response,request)
	} else {
		log.Println("no url mapping found !")
	}
	
}

func Get(response http.ResponseWriter, request *http.Request) {
	response.Write([]byte("get ...."))
}
func Post(response http.ResponseWriter, request *http.Request) {
	response.Write([]byte("Post ...."))
}
func Delete(response http.ResponseWriter, request *http.Request) {
	response.Write([]byte("Delete ...."))
}
func Put(response http.ResponseWriter, request *http.Request) {
	response.Write([]byte("Put ...."))
}
func Update(response http.ResponseWriter, request *http.Request) {
	response.Write([]byte("Update ...."))
}


 브 라 우 저 입력:
http://localhost:8080/get 
http://localhost:8080/post 
http://localhost:8080/delete
http://localhost:8080/update
http://localhost:8080/put
브 라 우 저 보기 정보 표시

좋은 웹페이지 즐겨찾기