FTmp开发记录
Xhofe Lv3

使用golang/gin+vue实现的一个临时文本/文件分享程序。

刚开始将项目命名为FTemp,后改为FTmp(为了简便)。

编写文档

项目介绍文档
API文档 (未完成)

开发

初始化

首先使用cobra新建这个项目结构

1
cobra init FTmp --pkg-name FTmp

添加server命令

1
cobra add server

引入gin提供web接口,gorm作为orm框架。

使用gopkg.in/ini.v1进行配置文件读取。之后可能会使用cobra带的Viper 进行重构。

models

建立本项目中主要的几个models:

  • text 文本
  • file 文件
  • setting 配置

同时数据也使用这些models自动生成(gorm的migration)。

Router

在路由中添加了一些跨域支持。

Utils

建立一些工具类,方便使用。

Service

服务层,主要处理逻辑

Controller

控制层,提供web接口

Middleware

添加一些中间件,做一些权限验证。

修改

刚开始没准备用service层,但是不方便。引入service层后,对models重新处理,不再使用model接收数据,而是创建单独的service structural。

文件流程改变,本来是想一次性上传多个文件,然后生成一个code返回的,但是发现不好操作,所以现在准备采用先申请code,然后携带code一个个的去上传。

问题解决记录

跨域问题

因为采用前后端分离的架构,所以避免不了碰到跨域问题。刚开始碰到了session失效的问题,解决方法总结如下:

  • 前端使用axios请求是添加withCredentials: true
  • 后端跨域配置AllowOrigins时,不要直接使用*通配符,而是使用具体的域名。
  • same-site设置位none

Thanks

Package Description
github.com/gin-gonic/gin Gin is a web framework written in Go (Golang).
github.com/go-playground/validator/v10 Package validator implements value validations for structs and individual fields based on tags.
github.com/jinzhu/gorm The fantastic ORM library for Golang, aims to be developer friendly.
github.com/speps/go-hashids 提供一些哈希算法
github.com/spf13/cobra Cobra is both a library for creating powerful modern CLI applications as well as a program to generate applications and command files.
github.com/gin-contrib/cors Gin middleware/handler to enable CORS support.
github.com/fatih/color Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang).
gopkg.in/ini.v1 提供ini配置文件解析
  • 本文标题:FTmp开发记录
  • 本文作者:Xhofe
  • 创建时间:2020-01-13 17:20:18
  • 本文链接:https://nn.ci/posts/ftmp-dev.html
  • 版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
 评论