Esempio n. 1
0
func mainHandler(c *gin.Context) {
	filename := c.Param("file")
	fmt.Println("opening file: ", filename, filename[1:])
	file, err := os.Open("web" + filename)
	if err != nil {
		fmt.Println(err)
		c.String(404, "cound not find page")
	}
	defer file.Close()
	reader := bufio.NewReader(file)
	data, err := ioutil.ReadAll(reader)
	if err != nil {
		fmt.Println(err)
	}
	pathnames := strings.Split(filename, ".")
	ext := pathnames[len(pathnames)-1]
	fmt.Println("extention is:", ext)
	if ext == "js" {
		c.Header("Content-Type", "text/javascript")
		c.String(200, string(data))
	} else if ext == "png" {
		fmt.Println("sending image")
		c.Data(200, "image/png", data)
	} else {
		c.Header("Content-type", "text/"+ext)
		c.String(200, string(data))
	}
}