码迷,mamicode.com
首页 > Web开发 > 详细

Golan http静态服务器

时间:2015-01-11 12:09:58      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

main.go

package main

import (
	"fmt"
	"io"
	"log"
	"net/http"
	"os"
	"strconv"
	"strings"
	"time"
)

func main() {
	config := readConfig()
	log.Println("start")
	http.Handle("/", http.FileServer(http.Dir(config.Wwwroot)))
	http.Handle("/download/", http.StripPrefix("/download/", http.FileServer(http.Dir(config.Downloadpath))))
	http.HandleFunc("/upload", upload)
	http.HandleFunc("/upload2", upload2)

	http.ListenAndServe(":"+config.Port, nil)
}

type Sizer interface {
	Size() int64
}

func upload(writer http.ResponseWriter, r *http.Request) {
	file, fileheader, _ := r.FormFile("file_1")
	defer file.Close()
	config := readConfig()
	newfile, _ := os.Create(config.Uploadpath + fileheader.Filename)
	defer newfile.Close()
	io.Copy(newfile, file)
	fmt.Fprintf(writer, "上传文件的大小为: %d", file.(Sizer).Size())
}

func upload2(writer http.ResponseWriter, r *http.Request) {
	log.Println("upload2 started.")
	file, fileheader, _ := r.FormFile("file")
	defer file.Close()
	config := readConfig()

	str_time := strings.Replace(fileheader.Filename, "IMG_", "", -1)
	str_time = strings.Replace(str_time, "VID_", "", -1)
	var folder string
	t, error := time.Parse("20060102", str_time[0:8])

	if error == nil {
		folder = config.PhotoPath + strconv.Itoa(t.Year()) + "/" +
			strconv.Itoa(int(t.Month())) + "月/" + strconv.Itoa(t.Day()) + "日/"
		//folder := config.PhotoPath + str_time[0:4] + "/" + str_time[4:6] + "月/" + str_time[6:8] + "日/"
	} else {
		folder = config.Uploadpath
	}
	os.MkdirAll(folder, os.ModeDir)
	log.Println(folder + fileheader.Filename)

	newfile, _ := os.Create(folder + fileheader.Filename)
	defer newfile.Close()
	_, error2 := io.Copy(newfile, file)
	if error2 == nil {
		fmt.Fprintf(writer, "%s", fileheader.Filename)
		return
	}

	log.Println("error: " + error2.Error())
	fmt.Fprintf(writer, "error: %s", error2.Error())
}

 

myConfig.go

package main

import (
	"encoding/json"
	"io/ioutil"
	"log"
)

//定义结构体
//首字母大写 , json:"msg_id" 是 tag
type MyConfig struct {
	Wwwroot      string `json:"wwwroot"`
	Uploadpath   string `json:"uploadpath"`
	Downloadpath string `json:"downloadpath"`
	PhotoPath    string `json:"photopath"`
	Port         string `json:"port"`
}

func readConfig() MyConfig {
	var config MyConfig
	filename := `./aa.json`
	bytes, _ := ioutil.ReadFile(filename)
	err := json.Unmarshal(bytes, &config)
	if err != nil {
		log.Fatal(err)
	}
	//log.Println(config.Wwwroot)
	return config
}

  

serialize.go

package main

import (
	"encoding/json"
	"fmt"
)

//定义结构体
//首字母大写 , json:"msg_id" 是 tag
type Message struct {
	MsgId   string `json:"msg_id"`
	Content string `json:"content"`
}

//json 序列号反序列化
func T3_1() {
	msg := Message{"msgid_001", "contente2222222222"}
	str, err := json.Marshal(msg)
	//输出 {"msg_id":"msgid_001","content":"contente2222222222"}
	fmt.Println(string(str), err)

	var msg1 Message
	//  str := `{"changes": [{"armid":3,"Index":5}, {"armid":3,"Index":6}]}`
	//反序列化为 stuct
	err = json.Unmarshal(str, &msg1)
	//输出 {msgid_001 contente2222222222}
	fmt.Println(msg1)
	//反序列化为map
	var msg2 map[string]string
	err = json.Unmarshal(str, &msg2)
	//输出 map[msg_id:msgid_001 content:contente2222222222]
	fmt.Println(msg2)
}

  

aa.json

{
"wwwroot":"./wwwroot/",
"uploadpath":"./up/",
"downloadpath":"./down/",
"photopath":"./Record/",
"port":"80"
}

  

Golan http静态服务器

标签:

原文地址:http://www.cnblogs.com/eturn/p/4216263.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!