设为首页 收藏本站
查看: 466|回复: 0

[经验分享] golang angular5 nginx web程序部署说明

[复制链接]

尚未签到

发表于 2018-11-10 06:30:26 | 显示全部楼层 |阅读模式
  1.nginx
  
  nginx-1.13.8.zip
  http://nginx.org/en/download.html
  unzip to E:\web-server-for-win
  E:\web-server-for-win\nginx-1.13.8\conf
  nginx配置修改
  nginx.conf
  红色字体为新添加配置
  ######## start ########
  #user  nobody;
  worker_processes  1;
  #error_log  logs/error.log;
  #error_log  logs/error.log  notice;
  #error_log  logs/error.log  info;
  #pid        logs/nginx.pid;
  events {
  worker_connections  1024;
  }
  http {
  include       mime.types;
  default_type  application/octet-stream;
  #access_log  logs/access.log  main;
  sendfile        on;
  #tcp_nopush     on;
  #keepalive_timeout  0;
  keepalive_timeout  65;
  #gzip  on;
  # upstream for golang service,0.0.0.0:8000
  upstream api_server_01.localtion01 {
  server localhost:8000;
  }
  server {
  listen       80;
  server_name  localhost;
  # all dir or files that angular build(ng build --prod) in dist path
  root  E:\pgmon-web\dist;
  # SPA index setting
  index  index.html index.htm;
  #charset koi8-r;
  #access_log  logs/host.access.log  main;
  # local service(angular SPA app, 0.0.0.0:80)
  location / {
  try_files $uri $uri/ =404;
  }
  # golang api service , api_server_01.localtion01
  # CORS(跨域访问) and proxy(代理) access
  # return same in browser: "localhost/api/status"
  # or "localhost:8000/api/status"
  location /api/{
  rewrite ^(api/?.*)$ /$1 break;
  proxy_pass http://api_server_01.localtion01;
  proxy_set_header   Host             $host;
  proxy_set_header   X-Real-IP        $remote_addr;
  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
  proxy_set_header   Cookie $http_cookie;
  add_header Access-Control-Allow-Origin *;
  add_header Access-Control-Allow-Headers Content-Type;
  add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
  add_header Access-Control-Allow-Credentials true;
  }
  #error_page  404              /404.html;
  # redirect server error pages to the static page /50x.html
  #
  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
  root   html;
  }
  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #    proxy_pass   http://127.0.0.1;
  #}
  # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  #
  #location ~ \.php$ {
  #    root           html;
  #    fastcgi_pass   127.0.0.1:9000;
  #    fastcgi_index  index.php;
  #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
  #    include        fastcgi_params;
  #}
  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #    deny  all;
  #}
  }
  }
  ######## end ########
  检查配置命令行
  e:\web-server-for-win\nginx-1.13.8>nginx -t
  nginx: the configuration file e:\web-server-for-win\nginx-1.13.8/conf/nginx.conf syntax is ok
  nginx: configuration file e:\web-server-for-win\nginx-1.13.8/conf/nginx.conf test is successful
  启动nginx程序
  e:\web-server-for-win\nginx-1.13.8>nginx.exe
  2.angular 5
  1). install nodejs
  node-v8.9.3-x64.msi
  2).setting prog
  由于 npm 官网镜像国内访问太慢,这里我使用了淘宝的npm镜像,安装方法如下:
  $ npm install -g cnpm --registry=https://registry.npm.taobao.org
  执行后我们就可以使用 cnpm 命令来安装模块:
  $ npm install -g @angular/cli@latest
  项目初始化
  新建项目
  E:\>ng new pgmon-web --routing
  选项:
  --routing,附加路由功能
  E:\>cd pgmon-web
  新建组件(默认每个组件单独生成文件夹)
  E:\pgmon-web>ng g c login
  E:\pgmon-web>ng g c dashboard
  新建服务(每个服务单独生成生成文件夹)
  E:\pgmon-web> ng g s --flat false AuthGuard
  create src/app/auth-guard/auth-guard.service.spec.ts (393 bytes)
  create src/app/auth-guard/auth-guard.service.ts (115 bytes)
  E:\pgmon-web> ng g s --flat false AuthenticationService
  create src/app/authentication-service/authentication-service.service.spec.ts (465 bytes)
  create src/app/authentication-service/authentication-service.service.ts (127 bytes)
  E:\pgmon-web> ng g s --flat false UserService
  create src/app/user-service/user-service.service.spec.ts (405 bytes)
  create src/app/user-service/user-service.service.ts (117 bytes)
  E:\pgmon-web>
  安装node模块
  E:\pgmon-web> npm install bootstrap@next
  选项:
  @next,使用最新版本4.0.0-beta.2,否则使用版本3
  npm WARN bootstrap@4.0.0-beta.2 requires a peer of jquery@1.9.1 - 3 but none is installed. You must install peer dependencies yourself.
  npm WARN bootstrap@4.0.0-beta.2 requires a peer of popper.js@^1.12.3 but none is installed. You must install peer dependencies yourself.
  npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\fsevents):
  npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
  + bootstrap@4.0.0-beta.2
  added 115 packages, removed 5 packages and updated 2 packages in 23.201s
  E:\pgmon-web>
  加载bootstrap到angular组件
  E:\pgmon-web>目录,编辑.angular-cli.json文件
  "styles": [
  "styles.css",
  "../node_modules/bootstrap/dist/css/bootstrap.css"
  ],
  生成生产环境部署文件,E:\pgmon-web\dist
  E:\pgmon-web>ng build --prod
  3.golang
  软件列表:
  go1.9.windows-amd64.msi
  Git-2.14.1-32-bit.exe
  Sublime Text Build 3143 x64 Setup.exe
  进入命令行
  Microsoft Windows [版本 10.0.14393]
  (c) 2016 Microsoft Corporation。保留所有权利。
  C:\Users\Administrator>e:
  E:\>
  create project
  E:\>mkdir go-web
  E:\>cd go-web
  init project
  E:\go-web>mkdir pkg
  E:\go-web>mkdir src
  E:\go-web>mkdir bin
  create app
  E:\go-web>cd src
  E:\go-web\src>mkdir app
  E:\go-web\src>cd app
  install dep(golang packages admin tools)
  E:\go-web\src\app>set GOPATH=E:\go-web
  E:\go-web\src\app>go get -u github.com/golang/dep/cmd/dep
  init dep
  E:\go-web\src\app>e:\go-web\bin\dep init
  E:\go-web\src\app>e:\go-web\bin\dep help
  Dep is a tool for managing dependencies for Go projects
  Usage: "dep [command]"
  Commands:
  init     Initialize a new project with manifest and lock files
  status   Report the status of the project's dependencies
  ensure   Ensure a dependency is safely vendored in the project
  prune    Prune the vendor tree of unused packages
  version  Show the dep version information
  Examples:
  dep init                               set up a new project
  dep ensure                             install the project's dependencies
  dep ensure -update                     update the locked versions of all dependencies
  dep ensure -add github.com/pkg/errors  add a dependency to the project
  Use "dep help [command]" for more information about a command.
  E:\go-web\src\app>
  E:\go-web\src\app>e:\go-web\bin\dep status
  PROJECT  CONSTRAINT  VERSION  REVISION  LATEST  PKGS USED
  E:\go-web\src\app>
  编辑项目
  相关main.go,放入目录E:\go-web\src\app>
  ######### code start #############
  package main
  // import golang packages
  import (
  "encoding/json"
  "fmt"
  "log"
  "net/http"
  "strings"
  "time"
  "github.com/codegangsta/negroni"
  "github.com/dgrijalva/jwt-go"
  "github.com/dgrijalva/jwt-go/request"
  )
  // def: SecretKey
  const (
  SecretKey = "welcome to wangshubo's blog"
  )
  // func: fatal error
  func fatal(err error) {
  if err != nil {
  log.Fatal(err)
  }
  }
  // def: UserCredentials, User, Response, Token
  type UserCredentials struct {
  Username string `json:"username"`
  Password string `json:"password"`
  }
  type User struct {
  ID       int    `json:"id"`
  Name     string `json:"name"`
  Username string `json:"username"`
  Password string `json:"password"`
  }
  type Response struct {
  Data string `json:"data"`
  }
  type Token struct {
  Token string `json:"token"`
  }
  // func: start server
  func StartServer() {
  http.HandleFunc("/api/status", ServerStatusHandler)
  http.HandleFunc("/api/authenticate", AuthHandler)
  http.Handle("/resource", negroni.New(
  negroni.HandlerFunc(ValidateTokenMiddleware),
  negroni.Wrap(http.HandlerFunc(ProtectedHandler)),
  ))
  log.Println("Now listening...")
  http.ListenAndServe(":8000", nil)
  }
  // def: main
  func main() {
  // start server
  StartServer()
  }
  // func: test url
  func ServerStatusHandler(w http.ResponseWriter, r *http.Request) {
  response := Response{"Golang API Server run successfull."}
  JsonResponse(response, w)
  }
  // func: print message of gained access
  func ProtectedHandler(w http.ResponseWriter, r *http.Request) {
  response := Response{"Gained access to protected resource"}
  JsonResponse(response, w)
  }
  // func: auth for username and password
  func AuthHandler(w http.ResponseWriter, r *http.Request) {
  var user UserCredentials
  err := json.NewDecoder(r.Body).Decode(&user)
  if err != nil {
  w.WriteHeader(http.StatusForbidden)
  fmt.Fprint(w, "Error in request")
  return
  }
  // def: access database, get user
  if strings.ToLower(user.Username) != "admin" {
  if user.Password != "123456" {
  w.WriteHeader(http.StatusForbidden)
  fmt.Println("Error logging in")
  fmt.Fprint(w, "Invalid credentials")
  return
  }
  }
  // generate token with claims map
  token := jwt.New(jwt.SigningMethodHS256)
  claims := make(jwt.MapClaims)
  claims["exp"] = time.Now().Add(time.Hour * time.Duration(1)).Unix()
  claims["iat"] = time.Now().Unix()
  token.Claims = claims
  if err != nil {
  w.WriteHeader(http.StatusInternalServerError)
  fmt.Fprintln(w, "Error extracting the key")
  fatal(err)
  }
  //  token singed
  tokenString, err := token.SignedString([]byte(SecretKey))
  if err != nil {
  w.WriteHeader(http.StatusInternalServerError)
  fmt.Fprintln(w, "Error while signing the token")
  fatal(err)
  }
  // return response
  response := Token{tokenString}
  JsonResponse(response, w)
  }
  // func: validate token
  func ValidateTokenMiddleware(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
  token, err := request.ParseFromRequest(r, request.AuthorizationHeaderExtractor,
  func(token *jwt.Token) (interface{}, error) {
  return []byte(SecretKey), nil
  })
  if err == nil {
  if token.Valid {
  next(w, r)
  } else {
  w.WriteHeader(http.StatusUnauthorized)
  fmt.Fprint(w, "Token is not valid")
  }
  } else {
  w.WriteHeader(http.StatusUnauthorized)
  fmt.Fprint(w, "Unauthorized access to this resource")
  }
  }
  // func: return json format's response
  func JsonResponse(response interface{}, w http.ResponseWriter) {
  json, err := json.Marshal(response)
  if err != nil {
  http.Error(w, err.Error(), http.StatusInternalServerError)
  return
  }
  w.WriteHeader(http.StatusOK)
  w.Header().Set("Content-Type", "application/json")
  w.Write(json)
  }
  ######### code end #############
  安装项目依赖包(安装前必须先进行dep初始化)
  E:\go-web\src\app> e:\go-web\bin\dep ensure
  运行项目
  E:\go-web\src\app> go run main.go
  打开浏览器,
  输入"localhost/api/status"
  或者"localhost:8000/api/status"
  browser output:
  {"data":"Golang API Server run successfull."}


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-632976-1-1.html 上篇帖子: nginx.conf文件详解及调优 下篇帖子: Flask+Gunicorn+Gevent+Supervisor+Nginx生产环境部署
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表