Create BeeGo web application
Environment
- Windows 10
Steps
- Create work folder
mkdir D:\Go_workspace
- The env variable GOROOT is Go installed directory. The env variable GOPATH is your workspace directory.
- Get BeeGo
cd D:\Go_workspace D:\Go_workspace> go get github.com/astaxie/beego
- Edit hello.go
package main import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (this *MainController) Get() { this.Ctx.WriteString("hello world") } func main() { beego.Router("/", &MainController{}) beego.Run() }
- Build it and run it.
D:\Go_workspace> go build hello.go D:\Go_workspace> hello.exe
- Go to http://127.0.0.1:8080 in the browser.
- Get bee tool. Update it use the command go get -u github.com/beego/bee
D:\Go_workspace> go get github.com/beego/bee
- Use bee tool to create my first project
D:\Go_workspace> bin\bee new myproject D:\Go_workspace>cd src\myproject
- Run my project
D:\Go_workspace\src\myproject>..\..\bin\bee run
- Go to http://127.0.0.1:8080 in the browser.
- Compress all project into a simgle file.
D:\Go_workspace\src\myproject>..\..\bin\bee pack
Support SSLl
- Download XCA tool
- Open xca-portable-2.1.2\xce.exe
-
Select menu item **File New Database**. Specify where you want to save the database and enter a password. - Select tab Certificates. Clicking New Certificate button.
- Download openssl for win
- Install openssl-0.9.8h-1-setup.exe
- Edit file C:\Program Files (x86)\GnuWin32\share\openssl.cnf
uncomment line: # req_extensions = v3_req add this line to section v3_req: subjectAltName=@alt_names add below section before the section v3_req: [ alt_names ] DNS.1 = localhost IP.1 = 127.0.0.1
- Change directory
>cd C:\Program Files (x86)\GnuWin32\bin >openssl genrsa -des3 -out googleapis.key 2048
- Create csr
openssl req -new -sha256 -key googleapis.key -out googleapis.csr ---- Country Name:TW State or Province Name:Taiwan Locality Name:Taipei
- Then it will show the following message
14032:error:0D0BA041:asn1 encoding routines:ASN1_STRING_set:malloc failure:./crypto/asn1/asn1_lib.c:381: 14032:error:0B08A041:x509 certificate routines:X509_ATTRIBUTE_set1_data:malloc failure:./crypto/x509/x509_att.c:317: problems making Certificate Request
- Download and install Win64 OpenSSL v1.1.1
- Open a console with administrator
- Run command
cd C:\Program Files\OpenSSL-Win64\bin openssl genrsa -des3 -out googleapis.key 2048
- Run command
openssl req -new -sha256 -key googleapis.key -out googleapis.csr
- Run command
copy googleapis.key googleapis.key.old
- Remove password from key file
openssl rsa -in googleapis.key.old -out googleapis.key
- Export
openssl x509 -req -days 3650 -in googleapis.csr -signkey googleapis.key -out googleapis.cer -extensions v3_req -extfile "C:\Program Files\OpenSSL-Win64\bin\cnf\openssl.cnf"
- Install googleapis.cer to system using default settings.
- Copy googleapis.key and googleapis.cer to the directory D:\Go_workspace\src\myproject
- Edit D:\Go_workspace\src\myproject\conf\app.conf
appname = myproject runmode = prod [dev] httpaddr = "127.0.0.1" HTTPPort = 9100 [prod] EnableHTTP = false HTTPSPort = 9099 httpsaddr = "127.0.0.1" EnableHTTPS = true EnableHttpTLS = true HTTPSCertFile = "googleapis.cer" HTTPSKeyFile = "googleapis.key" [test] HTTPSPort = 9099
- Restart
bee run
. If you receive the following messages, it means that you have to set EnableHTTP as false. Only one of EnableHTTP and EnableHTTPS can be set to true. Reference this link2018/11/09 10:07:56.251 [I] [asm_amd64.s:2361] http server Running on http://127.0.0.1:8080 2018/11/09 10:07:56.253 [I] [asm_amd64.s:2361] https server Running on https://127.0.0.1:9099 2018/11/09 10:07:56.293 [C] [asm_amd64.s:2361] ListenAndServeTLS: listen tcp 127.0.0.1:9099: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
Error
- When I execute the command go get github.com/astaxie/beego, I get following error message.
package github.com/astaxie/beego: cannot download, $GOPATH must not be set to $GOROOT. For more details see: 'go help gopath'
You have to set the correct variable GOROOT and GOPATH.
Change GOPATH
If you want to move the project to another folder, you need to set the GOPATH variable.
- I want to move myproject to Google Drive.
- Make folder
mkdir D:\Google Drive\Work\GoWorkspace
- Go to Advanced system settings. Set the GOPATH variable as D:\Google Drive\Work\GoWorkspace. You can set the GOPATH variable as multiple path. Like path1;path2.
- Make the source folder
mkdir D:\Google Drive\Work\GoWorkspace\src
- Move myproject to src folder
move C:\myproject D:\Google Drive\Work\GoWorkspace\src
- Install package to the new GOPATH. It will create a pkg folder in D:\Google Drive\Work\GoWorkspace.
go get github.com/astaxie/beego
- Let’s run beego
D:\Google Drive\Work\GoWorkspace\src\myproject>bee run