4

go客户之搭建环境_zzxiaoma的技术博客_51CTO博客

 1 year ago
source link: https://blog.51cto.com/u_3764469/5658141
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

go客户之搭建环境

精选 原创

zzxiaoma 2022-09-07 11:44:25 博主文章分类:go ©著作权

文章标签 html sed ide 文章分类 Go语言 编程语言 私藏项目实操分享 阅读数219

通过前一段的学习,使用GO做了一个小例子,功能主要是登录,客户表的增删改查和导出excel,功能很小,但是基本上能够进行实战操作。数据库采用mysql,首先说搭建开发环境。

1、先安装GO,我使用的是1.19的windows安装版。

2、下载开发工具LiteIDE。

这个工具用着还挺方便,就是里面的功能好多不会用

3、搭建框架,创建代码目录cus,所有的项目文件就放到cus目录中。先建立go.mod文件,需要执行go mod init cus,这样就创建好了。

4、使用LiteIDE打开cus目录,这样以后就可以在LiteIDE中进行操作了。当然应该使用LiteIDE也可以建立go.mod文件,目前不会用,还是用命令比较方便。

5、建立templates文件夹,这里面主要是存放使用的页面,这次使用的是单体项目,前端不会写,只能从网上下载现成的页面使用。建立lib文件夹存放页面使用的js库。

6、建立logger文件夹,这里面主要是日志功能,model存放的是和数据库对应的结构体,主要受java的影响,写程序都是按照java思路写的,所以里面的程序存放都感觉不太规范,不过不影响,主要就是为了练习。建立dbdata文件夹,里面存放和数据库相关的操作。建立utils文件夹,主要是放token的操作,建立web文件夹,主要存放页面提交的操作。

7、最后建立server.go文件,用于启动项目

package main

import (
"cus/logger"
"cus/utils"
"cus/web"
"fmt"
"html/template"
"net/http"
"strings"
"github.com/gorilla/mux"
"github.com/urfave/negroni"
"golang.org/x/net/context"
)

func MyMiddleware(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
//fmt.Println("url", r.URL.Path)
if r.URL.Path == "/login" || r.URL.Path == "/" || strings.Contains(r.URL.Path, "/lib/") || strings.Contains(r.URL.Path, "/static/") || strings.Contains(r.URL.Path, "/temp/") {
next(rw, r)
} else {
token, err := r.Cookie("token")
if err != nil {
t, error := template.ParseFiles("templates/login.html")
if error != nil {
fmt.Println(error.Error())
}
t.Execute(rw, nil)
}

if token != nil {
usermap, err := utils.GetToken(token.Value)
if err != nil {
t, error := template.ParseFiles("templates/login.html")
if error != nil {
fmt.Println(error.Error())
}
t.Execute(rw, nil)
} else {
tcontext := context.WithValue(context.Background(), "token", usermap)
rcontext := r.WithContext(tcontext)
next(rw, rcontext)
}
} else {
t, error := template.ParseFiles("templates/login.html")
if error != nil {
fmt.Println(error.Error())
}
t.Execute(rw, nil)
}
}
}

func main() {
r := mux.NewRouter()
r.HandleFunc("/", web.Index)
r.HandleFunc("/login", web.Login).Methods("POST")
r.HandleFunc("/upwinit", web.Upwinit)
r.HandleFunc("/upw", web.Upw)
r.HandleFunc("/cuslist", web.Cuslist)
r.HandleFunc("/cusaddinit", web.Cusaddinit)
r.HandleFunc("/cusadd", web.Cusadd)
r.HandleFunc("/cuseditinit", web.Cuseditinit)
r.HandleFunc("/cusedit", web.Cusedit)
r.HandleFunc("/cusdel", web.Cusdel)
r.HandleFunc("/cusinfo", web.Cusinfo)
r.HandleFunc("/cusexcel", web.Cusexcel)

n := negroni.New()
n.Use(negroni.HandlerFunc(MyMiddleware))
r.PathPrefix("/static/").Handler(http.StripPrefix("/static", http.FileServer(http.Dir("./static"))))
r.PathPrefix("/lib/").Handler(http.StripPrefix("/lib", http.FileServer(http.Dir("./lib"))))
r.PathPrefix("/temp/").Handler(http.StripPrefix("/temp", http.FileServer(http.Dir("./temp"))))
r.PathPrefix("/templates/").Handler(http.StripPrefix("/templates", http.FileServer(http.Dir("./templates"))))
n.UseHandler(r)
logger.Log.Fatal(http.ListenAndServe(":9091", n))
}
  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK