Skip to content

Commit

Permalink
实现自动注册模块路由,调整项目结构
Browse files Browse the repository at this point in the history
  • Loading branch information
houmkh committed Nov 1, 2022
1 parent b119463 commit 8bf1637
Show file tree
Hide file tree
Showing 15 changed files with 804 additions and 83 deletions.
10 changes: 7 additions & 3 deletions cmn/service-context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cmn
import (
"errors"
"fmt"
"github.com/jackc/pgx/v4"
"net/http"
"strings"
"sync"
)

Expand All @@ -13,6 +16,7 @@ type ModuleAuthor struct {
Addi string `json:"addi"`
}
type ServeEndPoint struct {
Fn func(w http.ResponseWriter, r *http.Request, dbConn *pgx.Conn)
Path string
Name string
Developer *ModuleAuthor
Expand Down Expand Up @@ -55,9 +59,9 @@ func AddService(ep *ServeEndPoint) (err error) {
// break
// }
//
// if !rIsAPI.MatchString(ep.Path) {
// ep.Path = strings.ReplaceAll("/api/"+ep.Path, "//", "/")
// }
//if !rIsAPI.MatchString(ep.Path) {
ep.Path = strings.ReplaceAll("/api/"+ep.Path, "//", "/")
//}
//}

if ep.Name == "" {
Expand Down
31 changes: 31 additions & 0 deletions dao/db-connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package dao

import (
"context"
"fmt"
"github.com/jackc/pgx/v4"
)

const (
host = "localhost"
port = 5432
user = "postgres"
password = "021020"
dbname = "chatroom"
)

var dbConn *pgx.Conn

func ConnDB() *pgx.Conn {
var err error
dbConnParam := fmt.Sprintf(`%s://%s:%s@%s:%d/%s`, user, user, password, host, port, dbname)
dbConn, err = pgx.Connect(context.Background(), dbConnParam)
if err != nil {
fmt.Println("failed to connect database")
//panic(err.Error())
return nil
} else {
fmt.Println("connect database successfully")
}
return dbConn
}
19 changes: 19 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,37 @@ module chatroom
go 1.17

require (
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.5.0
github.com/jackc/pgx/v4 v4.17.2
github.com/spf13/cobra v1.6.1
github.com/spf13/viper v1.13.0
)

require (
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.13.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.1 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.12.0 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.5 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.4.1 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
432 changes: 429 additions & 3 deletions go.sum

Large diffs are not rendered by default.

39 changes: 21 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"chatroom/serve/login"
"chatroom/serve/register"
"chatroom/serve/user_management"
"context"
"chatroom/service"
"encoding/json"
"fmt"
"github.com/gorilla/websocket"
Expand Down Expand Up @@ -63,6 +63,7 @@ func schedule() {
}
}

//TODO 要测试的函数!!
//只实现一次只给一个人发消息
func handleBroadcastData(data serve.BroadcastData) {
//把消息队列里面的消息 塞到对应用户的管道中
Expand Down Expand Up @@ -235,28 +236,30 @@ func serveHome(w http.ResponseWriter, r *http.Request) {

}

const (
host = "localhost"
port = 5432
user = "postgres"
password = "021020"
dbname = "chatroom"
)
//const (
// host = "localhost"
// port = 5432
// user = "postgres"
// password = "021020"
// dbname = "chatroom"
//)

var dbConn *pgx.Conn

func main() {
var err error
dbConnParam := fmt.Sprintf(`%s://%s:%s@%s:%d/%s`, user, user, password, host, port, dbname)
dbConn, err = pgx.Connect(context.Background(), dbConnParam)
if err != nil {
fmt.Println("failed to connect database")
//panic(err.Error())
} else {
fmt.Println("connect database successfully")
}
//var err error
//dbConnParam := fmt.Sprintf(`%s://%s:%s@%s:%d/%s`, user, user, password, host, port, dbname)
//dbConn, err = pgx.Connect(context.Background(), dbConnParam)
//if err != nil {
// fmt.Println("failed to connect database")
// //panic(err.Error())
//} else {
// fmt.Println("connect database successfully")
//}
//dbConn = dao.ConnDB()
go schedule()
http.HandleFunc("/", serveHome)
service.WebServe()
//http.HandleFunc("/", serveHome)
http.HandleFunc("/chat", func(writer http.ResponseWriter, request *http.Request) {
serveWs(writer, request)
})
Expand Down
22 changes: 19 additions & 3 deletions serve/file_management/file-management.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,26 @@ func Enroll(author string) {
}

cmn.AddService(&cmn.ServeEndPoint{
//Fn: user,
Fn: UploadFile,

Path: "/file_management",
Name: "file_management",
Path: "/user/upload_file",
Name: "user/upload_file",

Developer: developer,
})
cmn.AddService(&cmn.ServeEndPoint{
Fn: ShowFiles,

Path: "/user/show_files",
Name: "user/show_files",

Developer: developer,
})
cmn.AddService(&cmn.ServeEndPoint{
Fn: DownloadFile,

Path: "/user/download_file",
Name: "user/download_file",

Developer: developer,
})
Expand Down
10 changes: 6 additions & 4 deletions serve/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (

func Enroll(author string) {
var developer *cmn.ModuleAuthor

if author != "" {
var d cmn.ModuleAuthor
err := json.Unmarshal([]byte(author), &d)
Expand All @@ -29,17 +28,20 @@ func Enroll(author string) {
}

cmn.AddService(&cmn.ServeEndPoint{
//Fn: user,
Fn: Login,

Path: "/login",
Name: "login",
Path: "/user/login",
Name: "/user/login",

Developer: developer,
})

}

func Login(w http.ResponseWriter, r *http.Request, dbConn *pgx.Conn) {

fmt.Println("func login begin")

var err error
buf, err := ioutil.ReadAll(r.Body)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions serve/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ func Enroll(author string) {
}

cmn.AddService(&cmn.ServeEndPoint{
//Fn: user,
Fn: Register,

Path: "/register",
Name: "register",
Path: "/user/register",
Name: "/user/register",

Developer: developer,
})
Expand Down
45 changes: 21 additions & 24 deletions serve/reply_msg/reply-msg.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,34 @@
package reply_msg

import (
"chatroom/cmn"
"chatroom/serve"
"encoding/json"
"fmt"
"net/http"
)

//annotation:reply_msg-service
//author:{"name":"reply_msg","tel":"15521212871","email":"[email protected]"}
//func Enroll(author string) {
// var developer *cmn.ModuleAuthor
//
// if author != "" {
// var d cmn.ModuleAuthor
// err := json.Unmarshal([]byte(author), &d)
// if err != nil {
// return
// }
// developer = &d
// }
//
// cmn.AddService(&cmn.ServeEndPoint{
// Fn: Response,
//
// Path: "/reply_msg",
// Name: "reply_msg",
//
// Developer: developer,
// })
//}

func Enroll(author string) {
var developer *cmn.ModuleAuthor

if author != "" {
var d cmn.ModuleAuthor
err := json.Unmarshal([]byte(author), &d)
if err != nil {
return
}
developer = &d
}

cmn.AddService(&cmn.ServeEndPoint{
//Fn: user,

Path: "/reply_msg",
Name: "reply_msg",

Developer: developer,
})
}
func Response(w http.ResponseWriter, msg *serve.ReplyMsg) {
buf, err := json.Marshal(&msg)
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions serve/test/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test

import (
"chatroom/cmn"
"encoding/json"
"github.com/jackc/pgx/v4"
"net/http"
)

//annotation:user_management-service
//author:{"name":"user_management","tel":"15521212871","email":"[email protected]"}
func Enroll(author string) {
var developer *cmn.ModuleAuthor

if author != "" {
var d cmn.ModuleAuthor
err := json.Unmarshal([]byte(author), &d)
if err != nil {
return
}
developer = &d
}

cmn.AddService(&cmn.ServeEndPoint{
Fn: test,

Path: "/test",
Name: "test",

Developer: developer,
})
}
func test(w http.ResponseWriter, r *http.Request, dbConn *pgx.Conn) {
println("hello")
}
40 changes: 31 additions & 9 deletions serve/user_management/user-management.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,47 @@ func Enroll(author string) {
}

cmn.AddService(&cmn.ServeEndPoint{
//Fn: user,
Fn: ShowUsersInfo,

Path: "/user_management",
Name: "user_management",
Path: "/admin/show_users",
Name: "/admin/show_users",

Developer: developer,
})
cmn.AddService(&cmn.ServeEndPoint{
Fn: DeleteUser,

Path: "/admin/delete_user",
Name: "/admin/delete_user",

Developer: developer,
})
cmn.AddService(&cmn.ServeEndPoint{
Fn: ChangeUserInfo,

Path: "/admin/change_user_info",
Name: "/admin/change_user_info",

Developer: developer,
})
}
func ShowUsersInfo(w http.ResponseWriter, r *http.Request, dbConn *pgx.Conn) {
fmt.Println("func show users begin")
var err error
if err != nil {
fmt.Println(err.Error())
msg := serve.ReplyMsg{ServeStatus: -200, ResponseMessage: "read msg failed"}
reply_msg.Response(w, &msg)
//var err error
//if err != nil {
// fmt.Println(err.Error())
// msg := serve.ReplyMsg{ServeStatus: -200, ResponseMessage: "read msg failed"}
// reply_msg.Response(w, &msg)
//}
if dbConn == nil {
fmt.Println("nil")
}
userArray := make([]serve.UserInfo, 0)
sqlstr := `select uid, username from userinfo where privilege = 1`
result, _ := dbConn.Query(context.Background(), sqlstr)
result, err := dbConn.Query(context.Background(), sqlstr)
if err != nil {
fmt.Println(err.Error())
}
defer result.Close()
for result.Next() {
var user serve.UserInfo
Expand Down
Loading

0 comments on commit 8bf1637

Please sign in to comment.