1 Star 1 Fork 25

2733284198 / gfstudy

forked from goflyfox / gfstudy 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
03.goframe的WEB服务介绍.md 2.44 KB
一键复制 编辑 原始数据 按行查看 历史
zcool321@sina.com 提交于 2020-03-19 23:52 . update doc

GoFrame的Web服务介绍

GF框架提供了非常强大的WebServer,由ghttp模块实现。实现了丰富完善的相关组件,例如:Router、Cookie、Session、路由注册、配置管理、模板引擎、缓存控制等等,支持热重启、热更新、多域名、多端口、多实例、HTTPS、Rewrite等等特性。

一、web基本介绍

我们的电脑浏览器(Browser)就是客户端(Client),大型的服务器就是服务端(Server);浏览器发送HTTP请求,即客户端通过网络将需求发给服务端,然后服务端也是通过网络将数据发给客户端;

image-20200319225300542

二、GF搭建web项目

这里主要介绍基本项目启动和配置参数

目录结构

web:.
│  go.mod   -- go module
│  go.sum
│  main.go  -- 启动文件

├─config
│      config.toml --配置文件

├─gflogs
│      2020-03-19.log -- gf系统日志
│      access-20200319.log -- 访问日志
│      error-20200319.log  -- 异常日志

├─logs
│      2020-03-19.log -- 业务日志

└─public
        hello.html -- 静态文件
        index.html -- 静态入口文件

main.go

package main

import (
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gf/os/glog"
)

func main() {
	s := g.Server()
	// 测试日志
	s.BindHandler("/welcome", func(r *ghttp.Request) {
		glog.Info("你来了!")
		glog.Error("你异常啦!")
		r.Response.Write("哈喽世界!")
	})
	// 异常处理
	s.BindHandler("/panic", func(r *ghttp.Request) {
		glog.Panic("123")
	})
	// post请求
	s.BindHandler("POST:/hello", func(r *ghttp.Request) {
		r.Response.Writeln("Hello World!")
	})
	s.Run()
}

config.toml

GF框架的核心组件均实现了便捷的文件配置管理方式,包括Server、日志组件、数据库ORM、模板引擎等等,非常强大便捷。

[server]
    # 端口号
    Address          = ":8199"
    # 静态目录
    ServerRoot       = "public"
    # 入口文件
    IndexFiles       = ["index.html", "main.html"]
    # 系统访问日志
    AccessLogEnabled = true
    # 系统异常日志panic
    ErrorLogEnabled  = true
    # 系统日志目录,启动,访问,异常
    LogPath          = "gflogs"

[logger]
    # 标准日志目录
    path   = "logs"
    # 日志级别
    level  = "all"
1
https://gitee.com/linlin2018/gfstudy.git
git@gitee.com:linlin2018/gfstudy.git
linlin2018
gfstudy
gfstudy
master

搜索帮助