Skip to content

Commit 410672e

Browse files
committed
first submit for GoWeb
0 parents  commit 410672e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+12429
-0
lines changed

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License
2+
Copyright (c) 2016 coder4869 ( https://github.com/coder4869/GoWeb )
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in
12+
all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20+
THE SOFTWARE.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# GoWeb
2+
3+
4+
# Introduction
5+
This WEB is an go web demo, now including WEB API used and mongodb DataBase.
6+
7+
Project Directory:
8+
|--GoWeb
9+
|-- src
10+
|-- WebMain (entry of the Project)
11+
|-- conf (config operations for DataBase)
12+
|-- core (business operation)
13+
|-- controllers
14+
|-- defines
15+
|-- mgodaos (including init of mongodb config)
16+
|-- models
17+
|-- mysqldaos (including init of mysql DataBase config)
18+
|-- serves
19+
|-- github.com (libs from github)
20+
|-- libs (depends libs)
21+
|-- log (business logs)
22+
|-- tests (test cases)
23+
|-- docs
24+
25+
Function Call:
26+
WebMain --> serves --> controllers --> daos(mgodaos/mysqldaos) --> models & conf
27+
28+
Developers:
29+
coder4869@gmail.com
30+
31+
Depends:
32+
"github.com/coder4869/golibs"
33+
"gopkg.in/mgo.v2"
34+
35+
36+
# Run & Deployment
37+
Refer to "docs/Run&Deploy.md", this document provides the modify points
38+
for run and deploy the project.
39+
40+
41+
# More
42+
Other README.md or docs
43+
44+

VERSION

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Version 0.1 beta
2+
3+
4+

docs/Run&Deploy.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
## Prepare
2+
1. Install GO, Make GOROOT and GOPATH setting.
3+
2. Copy project source code (dir GoWeb) to target path, for example (/opt/)
4+
3. Adding "/opt/GoWeb" to GOPATH
5+
6+
7+
## Build & Run project
8+
1. go to src dir with cd: $ cd /opt/GoWeb/src
9+
2. directly run WebMain.go: $ go run WebMain.go
10+
or build and run GoWeb: '$ go build WebMain.go' and '$ ./WebMain'
11+
12+
13+
## Deploy Source Code AS Daemons (recommended)
14+
Here provides method using tool "supervisor" and unit file ".service"
15+
1. Install Supervisor ( http://supervisord.org/ )
16+
Example for centos:
17+
$ sudo yum install python-setuptools
18+
$ sudo easy_install supervisor
19+
2. Review and Create config file for Supervisor
20+
$ sudo echo_supervisord_conf > /etc/supervisord.conf
21+
3. Make deployment for GoWeb (using compiled binary file WebMain)
22+
Edit supervisord.conf: $vi /etc/supervisord.conf
23+
Append setting at file end:
24+
command=/opt/GoWeb/src/WebMain ; absolute path of binary file WebMain
25+
autostart=true ; autostart with the start of supervisor
26+
autorestart=true ; autorestart while process down
27+
startsecs=10
28+
stdout_logfile=/opt/GoWeb/src/log/WebMain.log
29+
stdout_logfile_maxbytes=1MB
30+
stdout_logfile_backups=10
31+
stdout_capture_maxbytes=1MB
32+
stderr_logfile=/opt/GoWeb/src/log/WebMain.log
33+
stderr_logfile_maxbytes=1MB
34+
stderr_logfile_backups=10
35+
stderr_capture_maxbytes=1MB
36+
Start supervisor
37+
manual start: $ sudo /usr/bin/supervisord -c /etc/supervisord.conf
38+
config modified and restart: $ cat /tmp/supervisord.pid | xargs sudo kill -HUP
39+
supervisor log file: /tmp/supervisord.log
40+
5. Make .service deployment for supervisor (autostart with computer's start)
41+
Create .service: $ touch /usr/lib/systemd/system/supervisord.service
42+
Edit .service: $ vi /usr/lib/systemd/system/supervisord.service
43+
Setting content as following:
44+
# supervisord service for systemd (CentOS 7.0+)
45+
# by ET-CS (https://github.com/ET-CS)
46+
47+
[Unit]
48+
Description=Process Monitoring and Control Daemon
49+
After=rc-local.service
50+
51+
[Service]
52+
Type=forking
53+
ExecStart=/usr/bin/supervisord -c /etc/supervisord.conf
54+
55+
[Install]
56+
WantedBy=multi-user.target
57+
start .service: $ systemctl start supervisord.service
58+
view .service status: $ systemctl status supervisord.service
59+
60+
61+
# Modify Points
62+
WEB API Port: in "WebMain.go" file
63+
DataBase Connection: in “src/conf/DBConf.json” file
64+
DataBase Connection file Reference: if cannot find file “conf/DBConf.json”,
65+
please modify reference path as absolute path
66+
(in "func LoadDBJsonConf()" of “src/conf/conf.go”)

src/WebMain.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/* WebMain.go
2+
entry of GoWeb
3+
4+
MIT License
5+
Copyright (c) 2016 coder4869 ( https://github.com/coder4869/GoWeb )
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
26+
package main
27+
28+
import (
29+
"core/serves"
30+
"log"
31+
"net/http"
32+
)
33+
34+
func main() {
35+
//log setting
36+
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
37+
log.Println("start WebServe8080 v2.0 beta")
38+
39+
//router setting
40+
http.HandleFunc("/", serves.Index)
41+
http.HandleFunc("/goweb/user/add", serves.UserAddServe)
42+
http.HandleFunc("/goweb/user/login", serves.UserLoginServe)
43+
44+
//listen port setting
45+
err := http.ListenAndServe(":8080", nil)
46+
if err != nil {
47+
log.Fatal("ListenAndServe: ", err)
48+
}
49+
}

src/conf/DBConf.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"Type":"mongodb",
3+
"User":"goweb_test",
4+
"Pwd":"goweb_test",
5+
"Ip":"127.0.0.1:10001",
6+
"Name":"goweb_test"
7+
}

src/conf/conf.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/* conf/conf.go
2+
setting config information
3+
4+
MIT License
5+
Copyright (c) 2016 coder4869 ( https://github.com/coder4869/GoWeb )
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
26+
package conf
27+
28+
import (
29+
"strings"
30+
31+
"github.com/coder4869/golibs/glfile"
32+
"github.com/coder4869/golibs/glio"
33+
)
34+
35+
type DBConf struct {
36+
Type string // db type, now supports "mysql" or "mongodb"
37+
User string // user name
38+
Pwd string // user password,
39+
Ip string // db ip address, format "ip:port", like "127.0.0.1:10001"
40+
Name string // db name
41+
}
42+
43+
var (
44+
DB *DBConf
45+
DBRegisterURL string
46+
)
47+
48+
/*
49+
called in "core/mysqldao/MysqlBaseDao.go" or "core/mgodao/MgoBaseDao.go"
50+
*/
51+
func LoadDBJsonConf() {
52+
// if file "./conf/DBConf.json" not found while running,
53+
// please replace relative path as absolute path and try again
54+
err := glfile.FillObjWithJsonFile(&DB, "./conf/DBConf.json")
55+
if err != nil {
56+
glio.FLPrintf("Load DB Json Config Error:%v\n", err)
57+
} else {
58+
if strings.EqualFold(DB.Type, "mysql") {
59+
DBRegisterURL = DB.User + ":" + DB.Pwd + "@tcp(" + DB.Ip + ")/" + DB.Name + "?charset=utf8"
60+
} else if strings.EqualFold(DB.Type, "mongodb") {
61+
DBRegisterURL = DB.Type + "://" + DB.User + ":" + DB.Pwd + "@" + DB.Ip + "/" + DB.Name
62+
}
63+
}
64+
glio.FLPrintf("DBRegisterURL=%v\n", DBRegisterURL)
65+
}

src/core/controllers/UserCtrl.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* core/controllers/UserCtrl.go
2+
3+
MIT License
4+
Copyright (c) 2016 coder4869 ( https://github.com/coder4869/GoWeb )
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.
23+
*/
24+
25+
package controllers
26+
27+
import (
28+
"core/mgodao"
29+
"core/models"
30+
)
31+
32+
type UserCtrl struct {
33+
}
34+
35+
/*
36+
Example for mongodb
37+
@param
38+
addParam: a map that contains register user information,
39+
keys for this map refer to defination of User
40+
@retain
41+
string : succeed - Insert User Id; failed - "false";
42+
*/
43+
func (this *UserCtrl) AddUser(addParam map[string]string) string {
44+
usr, _ := models.MgoUserWithMap(addParam)
45+
46+
return mgodao.AddUser(usr)
47+
}

src/core/defines/defines.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/* core/defines/defines.go
2+
provides variables defination
3+
4+
MIT License
5+
Copyright (c) 2016 coder4869 ( https://github.com/coder4869/GoWeb )
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
*/
25+
26+
package defines
27+
28+
import (
29+
"encoding/json"
30+
)
31+
32+
var (
33+
AesKey string = "1234567812345678"
34+
Error_Param string
35+
)
36+
37+
//Request Return Info
38+
type RespInfo struct {
39+
ErrorNo string
40+
ErrorInfo string
41+
Result string
42+
}
43+
44+
func init() {
45+
Error_Param = getErrorParam()
46+
}
47+
48+
func getErrorParam() string {
49+
str, _ := json.Marshal(RespInfo{"1", "\u53c2\u6570\u9519\u8bef", "null"})
50+
return string(str)
51+
}

0 commit comments

Comments
 (0)