Skip to content

Commit 64d4eb8

Browse files
committed
initialize
1 parent b13be39 commit 64d4eb8

File tree

6 files changed

+120
-0
lines changed

6 files changed

+120
-0
lines changed

example/base1/http.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
"net/http"
7+
)
8+
9+
type MyHandler struct {
10+
routes []string
11+
}
12+
13+
func (slf *MyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
14+
switch r.URL.Path {
15+
case "/":
16+
slf.routes = append(slf.routes, r.URL.Path)
17+
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
18+
case "/hello":
19+
slf.routes = append(slf.routes, r.URL.Path)
20+
for k, v := range r.Header {
21+
fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
22+
}
23+
case "/route_show":
24+
for _, v := range slf.routes {
25+
fmt.Fprintf(w, "route= %q\n", v)
26+
}
27+
default:
28+
fmt.Fprintf(w, "404 NOT FOUND: %s\n", r.URL)
29+
}
30+
}
31+
32+
func main() {
33+
h := new(MyHandler)
34+
http.Handle("/", h)
35+
// http.HandleFunc("/hello", helloHandler)
36+
log.Fatal(http.ListenAndServe(":9999", nil))
37+
}
38+
func indexHandler(w http.ResponseWriter, req *http.Request) {
39+
fmt.Fprintf(w, "URL.Path = %q\n", req.URL.Path)
40+
}
41+
42+
// handler echoes r.URL.Header
43+
func helloHandler(w http.ResponseWriter, req *http.Request) {
44+
for k, v := range req.Header {
45+
fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
46+
}
47+
}

example/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module example
2+
3+
go 1.21.6

README.md renamed to gee/README.md

File renamed without changes.

gee/gee.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package gee
2+
3+
import (
4+
"fmt"
5+
. "net/http"
6+
)
7+
8+
type Engine struct {
9+
router map[string]HandlerFunc
10+
}
11+
12+
func New() *Engine {
13+
14+
return &Engine{router: make(map[string]HandlerFunc, 0)}
15+
}
16+
17+
func (engine *Engine) addRoute(method string, pattern string, handler HandlerFunc) {
18+
key := method + "-" + pattern
19+
engine.router[key] = handler
20+
}
21+
22+
func (engine *Engine) GET(pattern string, handler HandlerFunc) {
23+
engine.addRoute("GET", pattern, handler)
24+
}
25+
26+
func (engine *Engine) POST(pattern string, handler HandlerFunc) {
27+
engine.addRoute("POST", pattern, handler)
28+
}
29+
30+
func (slf *Engine) ServeHTTP(w ResponseWriter, r *Request) {
31+
key := r.Method + "-" + r.URL.Path
32+
if handlerFunc, ok := slf.router[key]; ok {
33+
handlerFunc(w, r)
34+
} else {
35+
fmt.Fprintf(w, "404 NOT FOUND: %s\n", r.URL)
36+
}
37+
}
38+
39+
func (g *Engine) Run(prot string) error {
40+
return ListenAndServe(prot, g)
41+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module gee-rpc
2+
3+
go 1.21.6

main.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"fmt"
6+
"gee-rpc/gee"
7+
"net/http"
8+
)
9+
10+
func main() {
11+
port := flag.Int("port", 9999, "service port")
12+
13+
r := gee.New()
14+
r.GET("/", func(w http.ResponseWriter, r *http.Request) {
15+
fmt.Fprintf(w, "URL.Path = %q\n", r.URL.Path)
16+
})
17+
18+
r.GET("/hello", func(w http.ResponseWriter, r *http.Request) {
19+
for k, v := range r.Header {
20+
fmt.Fprintf(w, "Header[%q] = %q\n", k, v)
21+
}
22+
})
23+
24+
fmt.Printf(fmt.Sprintf("Listening on: http://localhost:%d", *port))
25+
r.Run(fmt.Sprintf(":%d", *port))
26+
}

0 commit comments

Comments
 (0)