|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "io" |
| 4 | + "fmt" |
5 | 5 | "log" |
6 | 6 | "net/http" |
| 7 | + |
| 8 | + "github.com/julienschmidt/httprouter" |
7 | 9 | ) |
8 | 10 |
|
9 | | -func MainPage(w http.ResponseWriter, req *http.Request) { |
10 | | - io.WriteString(w, "Welcome to GoBlog!\n") |
| 11 | +func mainpage(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { |
| 12 | + fmt.Fprintf(w, "Welcome to GoBlog!") |
11 | 13 | } |
12 | 14 |
|
13 | | -func main() { |
14 | | - http.HandleFunc("/", MainPage) |
15 | | - err := http.ListenAndServe(":1337", nil) |
16 | | - if err != nil { |
17 | | - log.Fatal("ListenAndServe: ", err) |
| 15 | +func signup(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 16 | + username := ps.ByName("username") |
| 17 | + password := ps.ByName("password") |
| 18 | + // create new user |
| 19 | + if len(username)+len(password) < 11 { |
| 20 | + fmt.Fprintf(w, "New user"+username+" with pass:"+password+"has signed up") |
| 21 | + } else { |
| 22 | + fmt.Fprintf(w, "Invalid arguments!") |
18 | 23 | } |
19 | 24 | } |
| 25 | + |
| 26 | +func newblog(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { |
| 27 | + // authenticate username and password |
| 28 | + blogname := ps.ByName("blogname") |
| 29 | + |
| 30 | + if len(blogname) < 20 { |
| 31 | + // create new blog |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func main() { |
| 36 | + router := httprouter.New() |
| 37 | + router.GET("/", mainpage) |
| 38 | + router.GET("/new/:username/:password", signup) |
| 39 | + router.GET("/new/:username/:password/:blogname", newblog) |
| 40 | + log.Fatal(http.ListenAndServe(":1337", router)) |
| 41 | +} |
0 commit comments