都2021移動端適配你還用flexible.js嗎?vw+rem一行代碼搞定

了解一下rem

rem(font size of the root element)是相對長度單位。相對於根元素(即html 元素)font-size 計算值的倍數。

適配原理:將px 替換成rem,動態修改html 的font-size 適配。它可以很好的根據根元素的字體大小來進行變化,從而達到各種屏幕基本一致的效果體驗

u 同學給的設計稿

常見的設計圖寬度,當然也可以是其他的寬度,比如720 像素的

  1. 375 iPhone7
  2. 750 二倍圖
  3. 320 iPhone5
  4. 640 二倍圖

為什麼給的是375?因為這個是iPhone7 的寬度,

也就是說最低兼容到375 像素的屏幕。(低於375 佈局可能會亂)

其他的同理

1. vw + rem 方案

如果效果圖是375px 的,

html 的style 屬性的font-size 設置為26.666666vw

css 中20px 改寫為0.2rem 即可

<!DOCTYPE html>
<html lang="en" style="font-size: 26.666666vw">
    <head>
        <meta charset="UTF-8" />
        <!--
        下面一行代码的解析:
        width=device-width 内容宽度 等于 设备宽度,换句话说 网页宽度为设备宽度
        initial-scale=1.0 初始缩放比等于1.0倍,换句话说 网页初始化缩放比为1.0 就是默认不缩放
    -->
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>Document</title>
        <style>
            /* 设置 div 为宽度100px高度18px */
            .app-main {
                /* 移动端写法 */
                width: 1rem;
                height: 0.18rem;
                /* 
                PC端写法
                width: 100px;
                height: 18px; */
            }
        </style>
    </head>
    <body>
        <div class="app-main"></div>
    </body>
</html>
复制代码

為什麼是26.666666vw?得了解下面幾個問題

-   1. 什么是 viewport?
-   2. 为什么要用它?
-   3. 怎么用?
-   4. `vw`、`vh`是什么?

答:

1. 什么是 viewport?
   [MDN viewport](https://developer.mozilla.org/zh-CN/docs/Web/CSS/Viewport_concepts)的解析是
   视口(viewport)代表当前可见的计算机图形区域。在 Web 浏览器术语中,通常与浏览器窗口相同,但不包括浏览器的 UI, 菜单栏等——即指你正在浏览的文档的那一部分。

2. 用它来移动端适配,兼容不同的设备,当然不局限于移动端,这里只讨论移动端

3. 只需要在`head`中定义 `<meta name="viewport" content="width=device-width, initial-scale=1.0" />`就行,具体如下:

`
<head>
    <meta charset="UTF-8" />
    <!--
        下面一行代码的解析:
        width=device-width 内容宽度 等于 设备宽度,换句话说 网页宽度为设备宽度
        initial-scale=1.0 初始缩放比等于1.0倍,换句话说 网页初始化缩放比为1.0 就是默认不缩放
    -->
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
</head>
`

4.  `vw` 是视口宽度的一个单位,viewport width 的简称,根据 viewport 相关定义,
    已经定义好了的,PC 端 100vw 就等于浏览器的宽度,移动端 100vw 就是设备的宽度(按照上面的 width=device-width)
    `vh` 同理,是视口高度,viewport height 的简称,100vh 就是可视窗口的高度
复制代码

了解完上面的知識點,我們可以回答

為什麼font-size 設置成26.666666vw?

设计图的宽度 = 设备宽度
假如 设计图的宽度为 375px,当然可以是其他的,这里是一个假如,如果是640,750就把375换成对应的数值按照以下方法换算就行就行
因为
    375px = 100vw
那么
    1px = 100 / 375 vw = 0.26666666666666666vw(约等于)

为了方便计算,放大一百倍,精确到6位,只能下取舍,因为上取舍,计算宽度的时候会大于页面宽度,从而出现滚动条
故:
    100px = 26.666666vw(约等于)

又因为给 html 标签设置 font-size 为 26.666666vw (约等于)

1rem为font-size的大小

所以:
    1rem = 100px
    0.2rem = 20px
也就是说:
    设计图上的 12px 换算成rem就是0.12rem,20px就写成0.2rem即可
复制代码

優點:不需要引入新的js,一行代碼搞定適配問題缺點:瀏覽器兼容性差,IE9 以下不支持,但現代瀏覽器,特別是移動端,基本都支持


可以參考:

設計圖大小(單位px) html 的font-size(單位vw) 備註
375 26.666666 效果圖20px,代碼應該寫0.2rem
750 13.333333 效果圖20px,代碼應該寫0.2rem
320 31.25 效果圖20px,代碼應該寫0.2rem
640 15.625 效果圖20px,代碼應該寫0.2rem

2. flexible 方案,(阿里)

lib-flexible的github 上有著這樣的一句話。

由於 viewport 單位得到眾多瀏覽器的兼容,lib-flexible這個過渡方案已經可以放棄使用,不管是現在的版本還是以前的版本,都存有一定的問題。建議大家開始使用viewport 來替代此方案。vw的兼容方案可以參閱《如何在Vue 項目中使用vw 實現移動端適配》一文。

我們可以得到一個很明確的信息,lib-flexible 這個方案已經被放棄使用了,我們可以去擁抱 vw 的那套實現方案。

3. 基於flexible 的hotcss方案

圖片上傳在ios中click事件無效

addImage方法中的this.input.click()在ios中無法生效。
網上提供的幾種解決方法,供大家參考:

1、​將click 事件直接綁定到目標​元素(​​即.target)上;
2、將目標​元素換成a 或者button 等可點擊的​元素;
​3、將click 事件委託到​​​​​非document 或body 的​​父級元素上;
​4、給​目標元素加一條樣式規則cursor: pointer。

我最後採用了直接調用dom的原生方法觸發input的點擊事件

addImage = () => {
const event = document.createEvent(‘MouseEvents’);
event.initMouseEvent(‘click’,false,false);
this.input.dispatchEvent(event)
};

Create Login Signup UI Screens in Vue with Bootstrap 4

In this tutorial, we are going to create responsive Login & Signup UI screens using the Bootstrap 4 CSS framework in the Vue.js application.

We will create a Login screen, Sign up screen, and Forgot Password screen from scratch.

Bootstrap is a free and open-source CSS based UI framework, and It is used for rapid front-end development. It offers plenty of UI components that are 100% responsive and can work on any device size smoothly. It contains CSS- and JavaScript-based design templates for typography, forms, buttons, navigation, and other interface components.

Vue.js is a robust progressive open-source JavaScript framework, and It is used use by web developers for creating excellent user interfaces and single-page applications. It makes app development quite simple and straightforward. The primary factor of Vue is that it is lightweight, flexible, modular, and highly performant.

Let’s start creating Login and Registration user-interface templates for Vue.js.

 

Vue.js Login & Signup UI Example

You need to have following tools and frameworks ready to get started with this tutorial:

  • Vue CLI
  • Vue
  • Bootstrap 4
  • Code Editor

Generate Vue App with Vue CLI

The Vue CLI offers the standard tooling option for swift development in Vue, run the command to install Vue CLI.

npm install -g @vue/cli

# or 

yarn global add @vue/cli

Make sure which vue-cli version has been installed in your development system:

vue --version

Generate a new Vue.js project by running the command from Vue CLI.

vue create vue-animated-ui

Answer Vue CLI questions with following choices.

# ? Please pick a preset: Manually select features
# ? Check the features needed for your project: Babel, Router, CSS Pre-processors, Linter
# ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes
# ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass)
# ? Pick a linter / formatter config: Basic
# ? Pick additional lint features: Lint on save
# ? Where do you prefer placing config for Babel, ESLint, etc.? In package.json
# ? Save this as a preset for future projects? (y/N) No

Head over to project folder.

cd vue-animated-ui

Start to see the latest created Vue app on the browser window.

npm run serve

Adding Bootstrap 4 in Vue.js

To use the Bootstrap UI components, we need to install the Bootstrap module in our Vue app.

npm install bootstrap

# or

yarn add bootstrap

Import Bootstrap path in the main.js file. It makes Bootstrap module available throughout our app.

import Vue from 'vue'
import App from './App.vue'
import router from './router'

import 'bootstrap/dist/css/bootstrap.min.css'

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

We also need to define the Font Awesome icon CDN path in the public/index.html. It allow us to add some useful icons in our Vue app.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">

Adding Global CSS in Vue.js Project

Let’s look at the most asked question regarding Vue that how to add global styles via CSS in a Vue.js project.

Create a css folder inside the src/assets folder then create the main.css file in it.

Define the global CSS path inside the main.js file just below the Bootstrap path.

import Vue from 'vue'
import App from './App.vue'
import router from './router'

import 'bootstrap/dist/css/bootstrap.min.css'
import '@/assets/css/main.css'

Vue.config.productionTip = false

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')

Add the common style of our primary authentication components in assets/css/main.css file.

* {
  box-sizing: border-box;
}

body {
  background: #2554FF !important;
  min-height: 100vh;
  display: flex;
  font-weight: 400;
}

body,
html,
.App,
.vue-tempalte,
.vertical-center {
  width: 100%;
  height: 100%;
}

.navbar-light {
  background-color: #ffffff;
  box-shadow: 0px 14px 80px rgba(34, 35, 58, 0.2);
}

.vertical-center {
  display: flex;
  text-align: left;
  justify-content: center;
  flex-direction: column;    
}

.inner-block {
  width: 450px;
  margin: auto;
  background: #ffffff;
  box-shadow: 0px 14px 80px rgba(34, 35, 58, 0.2);
  padding: 40px 55px 45px 55px;
  border-radius: 15px;
  transition: all .3s;
}

.vertical-center .form-control:focus {
  border-color: #2554FF;
  box-shadow: none;
}

.vertical-center h3 {
  text-align: center;
  margin: 0;
  line-height: 1;
  padding-bottom: 20px;
}

label {
  font-weight: 500;
}

.forgot-password,
.forgot-password a {
  text-align: right;
  font-size: 13px;
  padding-top: 10px;
  color: #7a7a7a;
  margin: 0;
}

.forgot-password a {
  color: #2554FF;
}

.social-icons {
  text-align: center;
  font-family: "Open Sans";
  font-weight: 300;
  font-size: 1.5em;
  color: #222222;
}

.social-icons ul {
  list-style: none;
  margin: 0;
  padding: 0;
}
.social-icons ul li {
  display: inline-block;
  zoom: 1;
  width: 65px;
  vertical-align: middle;
  border: 1px solid #e3e8f9;
  font-size: 15px;
  height: 40px;
  line-height: 40px;
  margin-right: 5px;
  background: #f4f6ff;
}

.social-icons ul li a {
  display: block;
  font-size: 1.4em;
  margin: 0 5px;
  text-decoration: none;
}
.social-icons ul li a i {
  -webkit-transition: all 0.2s ease-in;
  -moz-transition: all 0.2s ease-in;
  -o-transition: all 0.2s ease-in;
  -ms-transition: all 0.2s ease-in;
  transition: all 0.2s ease-in;
}

.social-icons ul li a:focus i,
.social-icons ul li a:active i {
  transition: none;
  color: #222222;
}

Create User Login UI in Vue

Designing and developing a login screen a bit time consuming task and requires some additional skills for a software developer. As we know, the login component allows you to access an application. It contains some strong web development fundamentals such as creating HTML forms, routing to navigate to some other screen, and two-way data-binding to extract the data from the login form.

We’re going to learn how to build a beautiful login form in the Vue.js application.

Create a Login.vue file in components folder and add the following code inside the components/Login.vue file.

<template>
    <div class="vue-tempalte">
        <form>
            <h3>Sign In</h3>

            <div class="form-group">
                <label>Email address</label>
                <input type="email" class="form-control form-control-lg" />
            </div>

            <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control form-control-lg" />
            </div>

            <button type="submit" class="btn btn-dark btn-lg btn-block">Sign In</button>

            <p class="forgot-password text-right mt-2 mb-4">
                <router-link to="/forgot-password">Forgot password ?</router-link>
            </p>

            <div class="social-icons">
                <ul>
                    <li><a href="#"><i class="fa fa-google"></i></a></li>
                    <li><a href="#"><i class="fa fa-facebook"></i></a></li>
                    <li><a href="#"><i class="fa fa-twitter"></i></a></li>
                </ul>
            </div>

        </form>
    </div>
</template>

<script>
    export default {
        data() {
            return {}
        }
    }
</script>

Create User Login UI in Vue

Build Sign up Template

Well, sign up in simple terms means to create a new account or register in an application. It could be signing up for any web portal or even for a newsletter. When you visit any new website, you need to create an account using the name, email, and password.

In this step, we are going to create an eye-catching registration UI screen in Vue using the Bootstrap 4 Form component.

Create components/Signup.vue and paste the given below code inside the file.

<template>
    <div class="vue-tempalte">
        <form>
            <h3>Sign Up</h3>

            <div class="form-group">
                <label>Full Name</label>
                <input type="text" class="form-control form-control-lg"/>
            </div>

            <div class="form-group">
                <label>Email address</label>
                <input type="email" class="form-control form-control-lg" />
            </div>

            <div class="form-group">
                <label>Password</label>
                <input type="password" class="form-control form-control-lg" />
            </div>

            <button type="submit" class="btn btn-dark btn-lg btn-block">Sign Up</button>

            <p class="forgot-password text-right">
                Already registered 
                <router-link :to="{name: 'login'}">sign in?</router-link>
            </p>
        </form>
    </div>
</template>

<script>
    export default {
        data() {
            return {}
        }
    }
</script>

Build Vue Sign up Template

Forgot Password Screen in Vue

“Forgot password” screen is used to recover any password that you forgot for any specific account for the application.

Create components/ForgotPassword.vue and paste the given below code inside the file.

<template>
    <div class="vue-tempalte">
        <form>
            <h3>Forgot Password</h3>

            <div class="form-group">
                <label>Email address</label>
                <input type="email" class="form-control form-control-lg" />
            </div>

            <button type="submit" class="btn btn-dark btn-lg btn-block">Reset password</button>

        </form>
    </div>
</template>

<script>
    export default {
        data() {
            return {}
        }
    }
</script>

Forgot password in Vue

Enable Vue Router

Next, we enable the router in the vue app, these routes will allow us to navigate from one page to another page. So, first install the vue router package in the app.

npm install vue-router

Add the following code inside the router/index.js file.

import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

  const routes = [
  {
    path: '/',
    name: 'signup',
    component: () => import('../components/Signup.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../components/Login.vue')
  },
  {
    path: '/forgot-password',
    name: 'forgot-password',
    component: () => import('../components/ForgotPassword.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router

Implement Vue Navigation with Bootstrap 4

Finally, we will create the simple and beautiful navigation bar that will allow us jump from one template to another template in Vue app.

Include the given below code inside the App.vue file.

<template>
  <div class="vue-tempalte">
    <!-- Navigation -->
    <nav class="navbar shadow bg-white rounded justify-content-between flex-nowrap flex-row fixed-top">
      <div class="container">
        <a class="navbar-brand float-left" href="https://www.positronx.io" target="_blank">
           positronX.io
        </a>
        <ul class="nav navbar-nav flex-row float-right">
          <li class="nav-item">
            <router-link class="nav-link pr-3" to="/login">Sign in</router-link>
          </li>
          <li class="nav-item">
            <router-link class="btn btn-outline-primary" to="/">Sign up</router-link>
          </li>
        </ul>
      </div>
    </nav>

    <!-- Main -->
    <div class="App">
      <div class="vertical-center">
        <div class="inner-block">
          <router-view />
        </div>
      </div>
    </div>
  </div>
</template>

Summary

We just created beautiful UI screes for Vue authentication process, we explored how to create some beautiful Login, Signup and Forgot password templates with Bootstrap 4.

You can download the full code from this GitHub repository.

WangEditor添加首行缩进功能

官方也不增加这个,但对于经常写文章的用户来说,首行缩进是很实用的功能,改变一下样式就可以了text-indent: 2em;

修改一下初始化wangEditor的地方,注册一个菜单

//初始化编辑器
var E = window.wangEditor;
// 菜单 key ,各个菜单不能重复
const menuKey = ‘MyTextIndentMenuKey’

// 注册菜单
E.registerMenu(menuKey, MyTextIndentMenu)

var editor = new E(‘#’+id);

 

新增一个MyTextIndentMenu.js,代码如下:

 

const E = window.wangEditor
const { $, BtnMenu , DropListMenu, PanelMenu, DropList, Panel, Tooltip } = E

const lengthRegex = /^(\d+)(\w+)$/
const percentRegex = /^(\d+)%$/
const reg = /^(SECTION|P|H[0-9]*)$/

// 第一,菜单 class ,Button 菜单继承 BtnMenu class https://www.wangeditor.com/doc/
class MyTextIndentMenu extends BtnMenu{
constructor(editor) {
// data-title属性表示当鼠标悬停在该按钮上时提示该按钮的功能简述text-indent: 2em;
const $elem = E.$(
`<div class=”w-e-menu w-e-icon-indent-increase” data-title=”首行缩进”>
</div>`
)
super($elem, editor)
}
// 菜单点击事件
clickHandler() {
// 做任何你想做的事情text-indent: 2em;
// 可参考【常用 API】文档,来操作编辑器
this.command();
}
// 菜单是否被激活(如果不需要,这个函数可以空着)
// 1. 激活是什么?光标放在一段加粗、下划线的文本时,菜单栏里的 B 和 U 被激活,如下图
// 2. 什么时候执行这个函数?每次编辑器区域的选区变化(如鼠标操作、键盘操作等),都会触发各个菜单的 tryChangeActive 函数,重新计算菜单的激活状态
tryChangeActive() {
// 激活菜单
// 1. 菜单 DOM 节点会增加一个 .w-e-active 的 css class
// 2. this.this.isActive === true
const editor = this.editor
const $selectionElem = editor.selection.getSelectionStartElem()
const $selectionStartElem = $($selectionElem).getNodeTop(editor)

if ($selectionStartElem.length <= 0) return

if ($selectionStartElem.elems[0].style[‘textIndent’] !== ”) {
this.active()
} else {
this.unActive()
}
}
/**
* 执行命令
* @param value value
*/
command() {
const editor = this.editor
const $selectionElem = editor.selection.getSelectionContainerElem()

// 判断 当前选区为 textElem 时
if ($selectionElem && editor.$textElem.equal($selectionElem)) {
// 当 当前选区 等于 textElem 时
// 代表 当前选区 可能是一个选择了一个完整的段落或者多个段落
const $elems = editor.selection.getSelectionRangeTopNodes()
if ($elems.length > 0) {
$elems.forEach((item) => {
this.operateElement($(item), editor)
})
}
} else {
// 当 当前选区 不等于 textElem 时
// 代表 当前选区要么是一个段落,要么是段落中的一部分
if ($selectionElem && $selectionElem.length > 0) {
$selectionElem.forEach((item) => {
this.operateElement($(item), editor)
})
}
}

// 恢复选区
editor.selection.restoreSelection()
this.tryChangeActive()
}

operateElement($node, editor) {
const $elem = $node.getNodeTop(editor)
let type = ‘increase’;
if($node.elems[0].style[‘textIndent’] !== ”)
type = ‘decrease’;

if (reg.test($elem.getNodeName())) {
if (type === ‘increase’) this.increaseIndentStyle($elem, this.parseIndentation(editor))
else if (type === ‘decrease’) this.decreaseIndentStyle($elem, this.parseIndentation(editor))
}
}
increaseIndentStyle($node, options) {
const $elem = $node.elems[0]
if ($elem.style[‘textIndent’] === ”) {
$node.css(‘text-indent’, options.value + options.unit)
} else {
const oldPL = $elem.style[‘textIndent’]
const oldVal = oldPL.slice(0, oldPL.length – options.unit.length)
const newVal = Number(oldVal) + options.value
$node.css(‘text-indent’, `${newVal}${options.unit}`)
}
}
decreaseIndentStyle($node, options) {
const $elem = $node.elems[0]
if ($elem.style[‘textIndent’] !== ”) {
const oldPL = $elem.style[‘textIndent’]
const oldVal = oldPL.slice(0, oldPL.length – options.unit.length)
const newVal = Number(oldVal) – options.value
if (newVal > 0) {
$node.css(‘text-indent’, `${newVal}${options.unit}`)
} else {
$node.css(‘text-indent’, ”)
}
}
}

parseIndentation(editor) {
const { indentation } = editor.config

if (typeof indentation === ‘string’) {
if (lengthRegex.test(indentation)) {
const [value, unit] = indentation.trim().match(lengthRegex).slice(1, 3)
return {
value: Number(value),
unit,
}
} else if (percentRegex.test(indentation)) {
return {
value: Number(indentation.trim().match(percentRegex)?indentation.trim().match(percentRegex):[1]),
unit: ‘%’,
}
}
} else if (indentation.value !== void 0 && indentation.unit) {
return indentation
}

return {
value: 2,
unit: ’em’,
}
}

}

ubuntu 下使用openconnect 连接vpn

使用openconnect在ubuntu 中安装openconnect,可以在软件中心找到.

 

在/etc/vpc/目录下新建vpnc-script 文件

文件内容可以到此处拷贝

http://git.infradead.org/users/dwmw2/vpnc-scripts.git/blob_plain/HEAD:/vpnc-script

 

sudo openconnect -u 用户名 –script=/etc/vpnc/vpnc-script –no-dtls vpn.test.com

 

输入密码后提示

POST https://vpn.test.com/+webvpn+/index.html

Got CONNECT response: HTTP/1.1 200 OK

CSTP connected. DPD 30, Keepalive 20

Connected tun0 as 10.22.22.22, using SSL

 

连接成功!!!

Golang指南:顶级Golang框架、IDE和工具列表

译文链接:http://www.codeceo.com/article/golang-framework-ide-tools.html 英文原文:Golang Guide: A List of Top Golang Frameworks, IDEs, and Tools

自推出以来,Google的Go编程语言(Golang)越来越受主流用户的欢迎。在2016年12月的一份调研中,3,595名受访者中有89%表明他们在工作中或工作以外用Go语言编程。

此外,在编程语言中,Go语言在专业知识和偏好方面排名最高。2017年7月,在Tiobe的年度编程语言排名中,Go语言从去年的第55名一跃跳到了第10名。

显然,Go语言吸引了来自不同学科的许多程序员和软件开发外包专业人士。可以这么说,这全都是因为Go语言的易用性。

作为一种编译型的开源编程语言,Go语言能使开发人员轻松构建简单可靠又高效的软件。它是更保守的语言,如C和C ++的创新和演变的产物。

使用Go语言,可以减少代码输入量,并且编写稳健的API而不牺牲性能变得更加容易。 Go语言旨在实现可扩展性和并发性,从而实现优化。编译器可以在运行时前执行所有代码检查工作。

我们收罗了Golang的顶级框架、IDE和工具列表,以供大家快速参考。建议添加到浏览器书签中,以便随时查看!

Golang框架

Web框架可以帮助开发人员尽可能方便快捷地构建应用程序。Go语言还比较新,所以使用的框架带有充足的文档很重要。

这里有9个框架可帮助你使用Go语言构建项目。

1.Revel

作为Go语言的高效生产力框架,Revel包含的Hot Code Reload工具可以让你在每次更改文件时重建项目。它还包括各种全面和高性能的功能,因此你不需要找外部库集成到框架中。

2.Beego

Beego是一个完整的MVC框架,有自己的日志库、ORM和Web框架。你不需要再去安装第三方库。它有一个称为Bee Tool的内置工具,用于监视代码更改,并在检测到更改时运行任务。

Beego可以为你节省很多时间,特别是在项目一开始,你要弄清楚日志框架或应用程序结构的时候。

3.Martini

受Sinatra启发,Martini是一个极其轻巧但功能强大的框架。它被开发用于用Golang编写模块化Web应用程序和服务。

它的特点是非侵入式设计,快速易用,且包括各种处理程序和中间件。它能够为HTML5模式的AngularJS应用程序执行基本路由,异常处理和默认文档服务。

Martini的最佳功能是可以使用反射,它允许开发人员动态地将数据插入到处理函数中并添加新的服务。Martini也完全兼容http.HandlerFunc界面。不过,缺点在于Martini框架不再维护了。

4.Gin Gonic

Gin Gonic是一个Web框架,有类似Martini的API,但性能更好。如果你以前使用过Martini,那么你也一定熟悉Gin Gonic。没用过Martini也没关系,只需要学习10分钟就能掌握Gin。就是这么容易!

Gin Gonic是一个极简化的框架,仅包含最重要的库和功能。这使得它非常适合开发高性能REST API。此外,它比Martini快四十倍。

你可以添加中间件、嵌套组、JSON验证以及渲染,并依然保持其最佳性能。Gin Gonic使用httprouter,Go语言最快的HTTP路由器。

5.Buffalo

要构建Go语言新的Web应用程序,使用Buffalo是一个快速又简单的方法。当你开始一个新项目时,Buffalo可以为你提供一切——从前端到后端开发。

它具有热重载功能,这意味着dev命令将自动查看.go和.html文件。然后,它将为你重建并重启二进制文件。运行dev命令,你就能看到变化在你的眼前发生!

Buffalo不仅仅是一个框架——它也是一个整体的Web开发生态系统,可以让你直接构建应用程序。

6.Goji

Goji是一个轻量级的快速Web框架,将可组合性和简单性作为其主要优先级。很像net / http.ServeMux,Goji是一个极简的HTTP请求复用器。它包括Einhorn支持,允许在Goji中提供Websocket支持。

其他功能包括URL模式,可重新配置的中间件堆栈,正常关机等。Goji可以用于生产,并在若干组织中提供了数以亿计个请求。

7.Tiger Tonic

受Dropwizard启发,Tiger Tonic是开发JSON Web服务和构建高性能REST API的Go框架。为了忠于Golang的原则,Tiger Tonic努力保持正交特性。

Tiger Tonic的缺点在于构建大型后端应用程序尚有不足之处。

8. Gocraft

这是又一个强大而简约的框架,Gocraft提供快速和可扩展的路由性能。它将路由添加来自标准库的net / http包中。

Gocraft是一个Go mux和中间件软件包,具有强大的投射和反射能力,可以静态输入代码。你还可以使用内置中间件添加可选功能或者自己编写。

由于性能始终是开发人员最关心的问题之一,所以Gocraft是开发人员的绝佳选择。而且使用Gocraft框架编写后端Web应用程序很容易。

9.Mango

虽然Mango没有得到创作者Paul Bellamy的积极维护,但Go语言的许多用户仍然在使用它。Mango的优势在于它的模块化。你可以从各种库中选择,以包含在你的项目中。

Mango让你可以尽可能快速又轻松地构建可重复使用的HTTP功能模块。它将一系列中间件和应用程序编译成单个HTTP服务器对象,以保持代码独立。

Golang的集成开发环境(IDE)

Golang的IDE随着Go语言的普及越来越受大家的欢迎。虽然还是有许多开发人员仍然喜欢使用文本编辑器,但也有很多开发人员更倾向于使用IDE。

如果你正工作于具有广泛代码库的大型项目,那么IDE可以帮助你轻松组织代码和导航。此外,IDE可以帮助你测试代码并相应地编辑。

以下是用Golang工作良好的顶尖IDE。

1.Gogland

软件开发公司JetBrains发布了另一个可靠的IDE,这次是针对Golang发布的。Gogland是一个商业IDE,为Go开发人员提供了一个强大的人机工程学环境。它还具有编码协助、调试器和集成终端的功能。

由于Gogland是由一家已成立的公司创建的,所以它拥有广泛的IntelliJ插件生态系统,让你可以在需要更多工具的时候获得更多。

2. Visual Studio Code

由Microsoft创建的Visual Studio Code是一个功能齐全的开源IDE和代码编辑器,支持各种各样的编程语言。它的特点是智能完成;使用断点调用、调用堆栈和交互式控制台调试;内置Git集成;以及分层文件夹和文件浏览器。

作为另一个流行的IDE,Visual Studio Code有一个Go开发人员定期贡献的支持社区。使用Visual Studio Code,你可以使用可用插件数组来扩展功能。

3. LiteIDE

LiteIDE是五年多前创建的首个以Golang为中心的开源IDE。作为具有独特外观的C ++ Qt应用程序,LiteIDE提供代码管理、可配置构建命令、gdb和Delve调试器,使用WordApi——基于MIME类型的系统——自动完成和创建等等。它还提供JSON和Golang支持。

4.Wide

Wide是Golang程序员使用的基于Web的IDE。它专为协作开发而设计,适用于团队和Web开发机构。Wide功能包括代码高亮、调试、Git集成等。

因为Wide是由一名中国开发者创建和维护的,所以其大部分文档和支持是中文的。

5.带有Go-Plus插件的Atom

如果你已经在使用Atom,那么你可以通过一个名为go-plus的开源软件包来改善Golang语言的代码编辑体验。使用go-plus,你可以立即获得关于语法和构建错误的实时反馈。

Go-plus软件包提供了几乎所有Atom中对Golang的支持。它还可以用于工具,构建流程,linters,vet和coverage工具。

Go-plus还包括各种代码片段和功能,如gocode的自动完成,gofmt、goreturns或goimports等的代码格式化。

6.带有GoClipse的Eclipse

由于Eclipse是广受欢迎的IDE,因此我们为其创建了许多插件。GoClipse是针对Golang的Eclipse插件,提供Go源代码编辑,具有可配置的语法高亮和自动缩进以及大括号完成功能。

GoClipse还可以作为项目向导和构建器来立即报告语法和构建错误。GoClipse的其他功能包括调试功能和代码辅助。

7.带有GoSublime的Sublime Text

Sublime Text也是一个复杂的文本编辑器,具有大量的贡献者和开发者社区。因此,开发者为此IDE创建了各种各样的插件。

GoSublime是Sublime Text 3针对Golang的插件,在你编写代码时,提供来自Gocode的代码完成,lint /语法检查,自动添加和删除程序包导入,等等。

8.带有Vim-Go插件的Vim

Vim是一个免费的开源IDE,可以定制和配置各种插件。如果你是Golang程序员,那么你可以使用Vim中由Fatih Arslan创建的vim-go插件。Vim-go自动安装所有必需的二进制文件,为Golang提供平滑的Vim集成。

Vim-go是一款功能强大的插件套件,用于撰写和开发Go。其功能包括高级源代码分析,添加和删除导入路径,多次第三方支持,goto定义,快速文件执行等等。

Vim-go是高度可定制的,可以根据你的需要启用或禁用各种功能。

9.Komodo

Komodo是一个全功能的Go语言IDE,并且支持如Node.js,Python,Ruby,Perl等其他编程语言。使用这个Go IDE,你可以轻松地编写干净的代码。其功能包括高级代码编辑器,智能代码完成,语法检查,版本控制和单元测试,以及允许代码浏览和代码提示的Go Code Intelligence。

Komodo的优点是,它可以很好地协助团队合作,因为允许多个开发人员同时编辑文档。只要一个许可证,Komodo就可以安装在Mac,Windows或Linux上。

10. 带有Go语言(golang.org)支持插件的IntelliJ IDEA

IntelliJ IDEA(由JetBrains公司开发)是可以通过Go语言支持插件从而使用Golang的IDE。如果你想要在IntelliJ IDEA中使用Golang,那么你需要安装此插件,虽然不同于Gogland,它的功能有限。

Golang工具

Golang工具可用于各种项目和Web应用程序。使用这些有用的工具可以帮助开发人员尽可能快速而轻松地编写代码并构建应用程序。

这里有一系列顶级的Golang工具以供参考。

1.Apicompat

Apicompat是一种新的Go语言工具,可帮助开发人员检测向后不兼容的更改和导出的声明。

你可以通过Apicompat避免误报。但是,Apicompat并不能检测到每个向后不兼容的变化。并且,库作者没有考虑到交换参数和其他更改的需要。

2.Checkstyle

受Java Checkstyle启发,针对Golang的Checkstyle输出编码风格的建议。它还允许开发人员检查文件行/函数和行/参数号,然后由用户进行配置。

3.Depth

又一个有用的Golang工具,Depth可帮助Web开发人员检索和可视化Go源代码依赖关系树。它可以用作独立的命令行应用程序或作为项目中的特定包。你可以通过在解析之前在Tree上设置相应的标志来添加自定义。

4.Go-Swagger

该工具包包括各种功能和功能。Go-Swagger是Swagger 2.0的一个实现,可以序列化和反序列化swagger规范。它是RESTful API简约但强大的代表。

通过Go-Swagger,你可以swagger规范文档,验证JSON模式以及其他额外的规则。其他功能包括代码生成,基于swagger规范的API生成,基于代码的规范文档生成,扩展了的字符串格式,等等。

5.Go Meta Linter

如果你需要运行Go lint工具并同时使其输出正常化,那么Go Meta Linter可以为你办到。Go Meta Linter旨在与文本编辑器或IDE集成,如如Sublime Linter插件,Atom go-plus包,Emacs Flycheck检查器,Vim / Neovim,以及Go for Visual Studio Code一起使用。它还支持各种各样的linter和配置文件,如JSON。

6.Go-callvis

Go-callvis是一个Web开发工具,允许你使用Graphviz的点格式可视化Go程序的调用图。此工具在构建具有复杂代码库的大型项目时特别有用。它在你想要了解另一个开发人员的代码结构或重建别人的项目时,也很有用。

通过go-callvis,开发人员可以在程序中关注特定包;根据软件包的分组函数和根据类型的方法;以及将软件包限制到自定义路径前缀,并忽略那些包含它们的自定义前缀。

7.Gonative

Gonative是一个简单的Golang工具,让你能够使用本机库构建Go工具链,而这可以在使用stdlib软件包的Cgo-enabled版本时进行交叉编译。

Gonative为每个平台下载二进制发行版,并将它们的库复制到正确的位置。同时,Gonative设置正确的mod时间,以避免不必要的重建。

不幸的是,Gonative在Windows上仍然未经测试。此外,也没有提供Linux / arm支持。

8.Grapes

Grapes是一种轻量级的Golang工具,旨在轻松地通过SSH分发命令。它由Yaron Sumel编写和积极维护。

Grapes不久将支持完整的主机密钥验证,这是开发人员应该注意到的。

9.Gosimple

Golang linter的伟大之处在于它专注于简化Go源代码。Gosimple始终将最新的Go版本作为目标,因此它需要Go 1.6或更高版本。

如果有新的Go版本,gosimple会建议最轻松和最简单的方法来避免复杂的构造。

10.Go Vendor

Go Vendor是与标准Vendor文件夹兼容的Golang工具。它允许开发人员通过govendor add / update从$GOPATH中复制现有的依赖关系。你还可以通过govendor fetch直接提取新的依赖关系或更新现有的依赖关系,以及使用govendor迁移来移动旧的系统。

总结

如果你有JS / Node背景,那么你还需要学习一些新的编程概念,如协同程序,通道,严格的类型与编译,接口,结构,指针和其他一些差异。但是,一旦你进入状态,你会发现Golang用起来更容易,也更快。


版权申明:内容来源网络,版权归原创者所有。除非无法确认,我们都会标明作者及出处,如有侵权烦请告知,我们会立即删除并表示歉意。谢谢。

nmcli网络配置命令

nmcli使用方法非常类似linux ip命令、cisco交换机命令,并且支持tab补全,也可在命令最后通过-h、–help、help查看帮助。在nmcli中有2个命令最为常用:

nmcli语法:
nmcli [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT和COMMAND可以用全称也可以用简称,最少可以只用一个字母,建议用头三个字母。OBJECT里面我们平时用的最多的就是connection和device,还有其他的选项在里暂时不介绍,这里需要简单区分一下connection和device

详细的介绍请看这篇文章:RHEL/CentOS系列发行版nmcli命令概述

这里主要介绍命令的使用

1、查看网络接口信息
————————————————————–
nmcli          ##查看ip(类似于ifconfig、ip addr)

nmcli device status      ##所有接口的简略信息

nmcli device show       ##所有接口的详细信息

nmcli device show interface-name     ##特定接口的详细信息
————————————————————–

2、查看连接信息
————————————————————–
nmcli connection show         ##所有连接的简略信息

nmcli connection show –active      ##显示激活的连接

nmcli connection show inteface-name   ##某个接口的详细连接信息
————————————————————–

3、激活连接与取消激活链接
————————————————————–
#激活连接
nmcli connection up connection-name
nmcli device connect interface-name

#取消激活链接
nmcli connection down connection-name    ##这个操作当取消一个激活后,如果有其它连接会自动激活其它连接
nmcli device disconnect interface-name     ##这个操作会取消接口上的激活,如果有其它连接也不会自动激活其它连接
————————————————————–
建议使用 nmcli device disconnect(connect) interface-name,因为连接断开可将该接口放到“手动”模式,这样做用户让 NetworkManager 启动某个连接前,或发生外部事件(比如载波变化、休眠或睡眠)前,不会启动任何自动连接。

4、创建动态获取ip地址的连接
————————————————————–
nmcli connection add type ethernet con-name connection-name ifname interface-name

add表示添加连接,type后面是指定创建连接时候必须指定类型,类型有很多,可以通过nmcli c add type -h看到,这里指定为ethernet。con-name后面是指定创建连接的名字,ifname后面是指定物理设备,网络接口

例子:nmcli connection add type ethernet con-name dhcp-ens33 ifname ens33
————————————————————–

5、创建静态ip地址连接
————————————————————–
nmcli connection add type ethernet con-name connection-name ifname interface-name ipv4.method manual ipv4.addresses address ipv4.gateway address

ipv4.addresses后面指定网卡ipv4的地址,ipv4.gateway后面指定网卡的ipv4网关

例子:nmcli connection add type ethernet con-name static-enp0s3 ifname enp0s3 ipv4.method manual ipv4.addresses 192.168.1.115/24 ipv4.gateway 192.168.1.1
————————————————————–
注意:创建连接后,NetworkManager 自动将 connection.autoconnect 设定为 yes。还会将设置保存到 /etc/sysconfig/network-scripts/connection-name 文件中,且自动将 ONBOOT 参数设定为 yes。

6、常用参数和网卡配置文件参数的对应关系这个只使用RHEL系列的发行版,不适合Debian系列发行版
————————————————————–

7、修改连接配置

————————————————————–
#添加一个ip地址
nmcli connection modify connection-name ipv4.addresses 192.168.0.58     ##如果已经存在ip会更改现有ip

#给eth0添加一个子网掩码(NETMASK)
nmcli connection modify connection-name ipv4.addresses 192.168.0.58/24

#获取方式设置成手动(BOOTPROTO=static/none)

nmcli connection modify connection-name ipv4.method manual

#获取方式设置成自动(BOOTPROTO=dhcp)

nmcli connection modify connection-name ipv4.method auto

#添加DNS

nmcli connection modify connection-name ipv4.dns 114.114.114.114

#删除DNS

nmcli connection modify connection-name -ipv4.dns 114.114.114.114 (注意这里的减号)

#添加一个网关(GATEWAY)

nmcli connection modify connection-name ipv4.gateway 192.168.0.2

#可一块写入:

nmcli connection modify connection-name ipv4.dns 114.114.114.114 ipv4.gateway 192.168.0.2

#修改连接是否随开机激活
nmcli connection modify connection-name connection.autoconnect no/on

#配置静态路由,重启系统依然生效

nmcli connection modify connection-name +ipv4.routes “192.168.12.0/24 10.10.10.1”

这样会将 192.168.122.0/24 子网的流量指向位于 10.10.10.1 的网关,同时在 /etc/sysconfig/network-scripts/目录下生产一个route-connection-name的文件,这里记录了这个连接的路由信息

————————————————————–

8、重载connection
————————————————————–
#重载所有ifcfg到connection(不会立即生效,在通过配置文件更改后需要做这个操作让NM知道你做了更改,重新激活连接或重启NM服务后生效)
nmcli connection reload
————————————————————–
#重载指定ifcfg到connection(不会立即生效,重新激活连接或重启NM服务后生效)
nmcli connection load /etc/sysconfig/network-scripts/ifcfg-connection-name
nmcli connection load /etc/sysconfig/network-scripts/route-connection-name
————————————————————–

9、删除connection
————————————————————–
nmcli connection delete connection-name
————————————————————–

10、设置主机名
————————————————————–
#查询当前主机名
nmcli general hostname

#修改主机名
nmcli general hostname new-hostname

#重启hostname(主机名)服务
systemctl restart systemd-hostnamed
————————————————————–
注意:CentOS7 / Redhat7 下的主机名管理是基于系统服务systemd-hostnamed,服务自身提供了hostnamectl命令用于修改主机名,推荐这种方式进行修改;
使用nmcli命令更改主机名时,systemd-hostnamed服务并不知晓 /etc/hostname 文件被修改,因此需要重启服务去读取配置;

automatically connect OpenConnect VPN use a service file

i use a service file

/etc/systemd/system/myVpn.service

[Unit]
Description=My Vpn Connection
After=network.target

[Service]
Type=simple
Environment=password=correcthorsebatterystaple
 ExecStart=/bin/sh -c 'echo YourPasswordHere | sudo openconnect --protocol=nc YourServerHere --user=YourUserHere --passwd-on-stdin'

Restart=always

systemctl enable myVpn

systemctl start myVpn

快速分析 Apache 的 access log,抓出前十大網站流量兇手

說到 Log 分析大家都會先想到用 AWStats 來分析,沒錯這絕對是一個最好的解決方式,但如果你只是要簡單的分析一些資訊,就可以利用一些簡單的 shell 組合來撈出你要的資料

 

這篇主要是針對 Apache 的 access log 來進行分析,並提供以下範例給大家參考

 

取得前十名access 最多的IP 位址

cat access_log | awk'{print $ 1}'| sort | uniq -c | sort -nr | head -10

 

取得前十名 access 最多的網頁

cat access_log | awk'{print $ 11}'| sort | uniq -c | sort -nr | head -10

 

取得前十名下載流量最大的 zip 檔案

cat access.log | awk'($ 7〜/ \。zip /){print $ 10“” $ 1“” $ 4“” $ 7}'| sort -nr | head -10

 

取得前十名 Loading 最大的頁面 (大於60秒的 php 頁面)

cat access_log | awk'($ NF> 60 && $ 7〜/ \。php /){print $ 7}'| sort -n | uniq -c | sort -nr | head -10

 

取得前十名 User access 最久的頁面

cat access_log | awk'($ 7〜/ \。php /){print $ NF“” $ 1“” $ 4“” $ 7}'| sort -nr | head -10

 

取得access log 平均流量(GB)

cat access_log | awk'{sum + = $ 10} END {print sum / 1024/1024/1024}'

 

取得所有404 Link

awk'($ 9〜/ 404 /)'access_log | awk'{print $ 9,$ 7}'| 分類

 

取得所有 access code 的 stats 數量

cat access_log | awk -F'''$ 9 ==“ 400” || $ 9 ==“ 404” || $ 9 ==“ 408” || $ 9 ==“ 499” || $ 9 ==“ 500” || $ 9 ==“ 502” || $ 9 ==“ 504” {print $ 9}'| | 排序| uniq -c | 更多

 

以上只是簡單分析出常用的需求,也可以自行斟酌調整,然後再從中找到自己想要的分析模式

相信在日常的維護使用中可以幫上很大的忙。