mysql
This commit is contained in:
		
							
								
								
									
										48
									
								
								bin/main.go
									
									
									
									
									
								
							
							
						
						
									
										48
									
								
								bin/main.go
									
									
									
									
									
								
							@@ -1,6 +1,7 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"database/sql"
 | 
			
		||||
	"image"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
@@ -12,13 +13,60 @@ import (
 | 
			
		||||
 | 
			
		||||
	"github.com/chai2010/webp"
 | 
			
		||||
	"github.com/disintegration/imaging"
 | 
			
		||||
	_ "github.com/go-sql-driver/mysql"
 | 
			
		||||
	giftowebp "github.com/sizeofint/gif-to-webp"
 | 
			
		||||
	"github.com/spf13/viper"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func init() {
 | 
			
		||||
	// 设置日志格式
 | 
			
		||||
	log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 实现一个 web api 服务(获取指定尺寸的图片)
 | 
			
		||||
func main() {
 | 
			
		||||
	runtime.GOMAXPROCS(runtime.NumCPU())
 | 
			
		||||
 | 
			
		||||
	// 读取配置文件yaml
 | 
			
		||||
	viper.SetConfigName("config") // 设置配置文件的名字
 | 
			
		||||
	viper.SetConfigType("yaml")   // 设置配置文件的格式
 | 
			
		||||
	viper.AddConfigPath("./data") // 设置配置文件所在的路径
 | 
			
		||||
	if err := viper.ReadInConfig(); err != nil {
 | 
			
		||||
		log.Println("读取配置文件失败", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	user := viper.Get("mysql.user").(string)
 | 
			
		||||
	password := viper.Get("mysql.password").(string)
 | 
			
		||||
	host := viper.Get("mysql.host").(string)
 | 
			
		||||
	port := viper.Get("mysql.port").(int)
 | 
			
		||||
	database := viper.Get("mysql.database").(string)
 | 
			
		||||
	sqlconf := user + ":" + password + "@tcp(" + host + ":" + strconv.Itoa(port) + ")/" + database + "?charset=utf8mb4&parseTime=True&loc=Local"
 | 
			
		||||
 | 
			
		||||
	// 连接数据库
 | 
			
		||||
	db, err := sql.Open("mysql", sqlconf)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println("连接数据库失败", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// 从 web_images 表读取图片 id, version, width, height, format
 | 
			
		||||
	rows, err := db.Query("SELECT id, width, height, content FROM web_images WHERE id = 8888")
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println("查询数据库失败", err)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	defer rows.Close()
 | 
			
		||||
 | 
			
		||||
	// 遍历查询结果
 | 
			
		||||
	for rows.Next() {
 | 
			
		||||
		var id, width, height, content string
 | 
			
		||||
		if err = rows.Scan(&id, &width, &height, &content); err != nil {
 | 
			
		||||
			log.Println("读取数据库失败", err)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		log.Println(id, width, height, content)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	http.HandleFunc("/img/", func(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
 | 
			
		||||
		// URL 格式: /img/{id}-{version}@{倍图}{宽度}.{格式}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user