-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
413 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package controller | ||
|
||
import "net/http" | ||
|
||
func ChatroomProcess(w http.ResponseWriter, r *http.Request) { | ||
//跟user/xxx 页面ajax交互 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
) | ||
|
||
func Game1(w http.ResponseWriter, r *http.Request) { | ||
t, err := template.ParseFiles("./client/templates/huarongdao.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
t.Execute(w, nil) | ||
} | ||
func Game2(w http.ResponseWriter, r *http.Request) { | ||
t, err := template.ParseFiles("./client/templates/huarongdao.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
t.Execute(w, nil) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
) | ||
|
||
func RegisterHandlers() { | ||
http.HandleFunc("/", Index) | ||
http.HandleFunc("/login", Login) | ||
http.HandleFunc("/loginProcess", LoginProcess) | ||
http.HandleFunc("/regist", Regist) | ||
http.HandleFunc("/registProcess", RegistProcess) | ||
//Todo | ||
http.HandleFunc("/user/", UserProcess) | ||
//todo | ||
http.HandleFunc("/game/game1", Game1) | ||
http.HandleFunc("/game/game2", Game2) | ||
//tODO | ||
http.HandleFunc("/chatroom/", ChatroomProcess) | ||
// http.Handle("../", http.StripPrefix("../", http.FileServer(http.Dir("./client")))) | ||
// http.Handle("../assets/", http.StripPrefix("../assets/", http.FileServer(http.Dir("./client")))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package controller | ||
|
||
import ( | ||
"fmt" | ||
"html/template" | ||
"net/http" | ||
"ssevven/model" | ||
) | ||
|
||
func Index(w http.ResponseWriter, r *http.Request) { | ||
t, err := template.ParseFiles("./client/templates/index.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
args := make(map[string]interface{}) | ||
args["LoginHref"] = model.Cfg.Domain + "/login" | ||
t.Execute(w, args) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package controller | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
"ssevven/model" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
//show /login.html | ||
func Login(w http.ResponseWriter, r *http.Request) { | ||
t, err := template.ParseFiles("./client/templates/login.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
args := make(map[string]interface{}) | ||
args["Home"] = model.Cfg.Domain | ||
args["RegistAction"] = model.Cfg.Domain + "/regist" | ||
args["LoginAction"] = model.Cfg.Domain + "/loginProcess" | ||
t.Execute(w, args) | ||
} | ||
|
||
//process login request | ||
func LoginProcess(w http.ResponseWriter, r *http.Request) { | ||
r.ParseForm() | ||
fmt.Println() | ||
log.Println("login:", r.PostForm) | ||
user := &model.User{} | ||
user.Username = r.PostForm["username"][0] | ||
user.Password = r.PostForm["password"][0] | ||
u, _ := user.GetUserByUsername() | ||
// log.Println("u=", u) | ||
res := &model.LoginRes{} | ||
if u == nil { | ||
res.Code = 310 //用户名不存在 | ||
res.Msg = "用户名不存在" | ||
} else { | ||
if u.Password == user.Password { | ||
res.Code = 300 | ||
res.Msg = "登陆成功" | ||
res.UserUrl = model.Cfg.Domain + "/user/" + strconv.Itoa(u.Uid) | ||
log.Printf("登录成功=%#v\n", user) | ||
} else { | ||
res.Code = 320 //密码错误 | ||
res.Msg = "密码错误" | ||
} | ||
} | ||
// Set response header | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(&res) | ||
log.Println("res =", res) | ||
} | ||
|
||
//show /regist.html | ||
func Regist(w http.ResponseWriter, r *http.Request) { | ||
t, err := template.ParseFiles("./client/templates/regist.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
args := make(map[string]interface{}) | ||
args["Login"] = model.Cfg.Domain + "/login" | ||
args["RegistAction"] = model.Cfg.Domain + "/registProcess" | ||
t.Execute(w, args) | ||
} | ||
|
||
//process regist request | ||
func RegistProcess(w http.ResponseWriter, r *http.Request) { | ||
//如果username在数据库中差找不到,则允许注册 | ||
//否则,返回注册失败 | ||
r.ParseForm() | ||
fmt.Println() | ||
log.Println("regist:", r.PostForm) | ||
user := &model.User{} | ||
user.Username = r.PostForm["username"][0] | ||
user.Password = r.PostForm["password"][0] | ||
u, _ := user.GetUserByUsername() | ||
// log.Println("u=", u) | ||
res := &model.RegistRes{} | ||
|
||
if u != nil { | ||
res.Code = 220 //用户已注册 | ||
res.Msg = "用户名已注册" | ||
} else { | ||
err := user.AddUser() | ||
if err != nil { | ||
log.Println("dml err=", err) | ||
return | ||
} | ||
res.Code = 200 //注册成功 | ||
res.Msg = "注册成功" | ||
log.Printf("注册成功=%#v\n", user) | ||
} | ||
// Set response header | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(&res) | ||
log.Println("res =", res) | ||
} | ||
|
||
//直接访问//Todo | ||
//show /user.html | ||
func UserProcess(w http.ResponseWriter, r *http.Request) { | ||
args := make(map[string]interface{}) | ||
user := &model.User{} | ||
user.Uid, _ = strconv.Atoi(strings.Split(r.URL.Path, "/")[2]) | ||
u, _ := user.GetUserByUid() | ||
if u == nil { | ||
t, err := template.ParseFiles("./client/templates/index.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
t.Execute(w, nil) | ||
} else { | ||
t, err := template.ParseFiles("./client/templates/user.html") | ||
if err != nil { | ||
w.WriteHeader(500) | ||
fmt.Fprint(w, err) | ||
return | ||
} | ||
args["GameOneRequest"] = model.GameOne.GameURL | ||
args["GameOneName"] = model.GameOne.GameName | ||
args["GameTwoRequest"] = model.GameTwo.GameURL | ||
args["GameTwoName"] = model.GameTwo.GameName | ||
args["UserImg"] = u.Username | ||
args["UserName"] = u.Username | ||
args["Login"] = model.Cfg.Domain + "/login" | ||
t.Execute(w, args) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package model | ||
|
||
import ( | ||
"bufio" | ||
"encoding/json" | ||
"os" | ||
) | ||
|
||
type Config struct { | ||
AppName string `json:"app_name"` | ||
AppMode string `json:"app_mode"` | ||
AppHost string `json:"app_host"` | ||
AppPort string `json:"app_port"` | ||
HttpProtocol string `json:"http_protocol"` | ||
Domain string `json:"domain"` | ||
} | ||
|
||
var Cfg *Config = nil | ||
|
||
func ParseConfig(path string) (*Config, error) { | ||
file, err := os.Open(path) | ||
if err != nil { | ||
panic(err) //初始化出错,程序直接退出 | ||
} | ||
defer file.Close() | ||
|
||
reader := bufio.NewReader(file) | ||
decoder := json.NewDecoder(reader) | ||
err = decoder.Decode(&Cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return Cfg, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package model | ||
|
||
var ( | ||
GameOne = &Game{ | ||
GameName: "数字华容道", | ||
// GameURL: Cfg.Domain + "/game/game1", | ||
GameURL: "http://ssevven.free.idcfengye.com" + "/game/game1", | ||
} | ||
GameTwo = &Game{ | ||
GameName: "数独", | ||
// GameURL: Cfg.Domain + "/game/game1", | ||
GameURL: "http://ssevven.free.idcfengye.com" + "/game/game2", | ||
} | ||
) | ||
|
||
type Game struct { | ||
GameName string | ||
GameURL string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package model | ||
|
||
type RegistRes struct { | ||
Code int32 `json:"code"` | ||
Msg string `json:"msg"` | ||
} | ||
type LoginRes struct { | ||
Code int32 `json:"code"` | ||
Msg string `json:"msg"` | ||
UserUrl string `json:"userUrl"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package model | ||
|
||
import "html/template" | ||
|
||
var Templates *template.Template | ||
|
||
func LoadTemplates() { | ||
Templates = template.New("templates") | ||
template.Must(Templates.ParseGlob("client/templates/*.html")) | ||
} |
Oops, something went wrong.