web

package
v1.0.13 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EventStreamHandler added in v1.0.13

func EventStreamHandler(onEvent func(*http.Request, chan<- string)) func(http.ResponseWriter, *http.Request)

func OpenBrowser added in v1.0.10

func OpenBrowser(url string) bool

OpenBrowser tries to open the URL in a browser, and returns whether it succeed.

Example
package main

import (
	"fmt"
	"log"
	"net/http"
	"time"

	"github.com/jhunters/goassist/web"
)

func main() {
	httpAddr := "localhost:8080"
	url := "http://" + httpAddr
	if web.OpenBrowser(url) && waitServer(url) {
		log.Printf("A browser window should open. If not, please visit %s", url)
	} else {
		log.Printf("Please open your web browser and visit %s", url)
	}
	fmt.Println("ok")

	// Ouput:
	// ok
}

// waitServer waits some time for the http Server to start
// serving url. The return value reports whether it starts.
func waitServer(url string) bool {
	tries := 20
	for tries > 0 {
		resp, err := http.Get(url)
		if err == nil {
			resp.Body.Close()
			return true
		}
		time.Sleep(100 * time.Millisecond)
		tries--
	}
	return false
}
Output:

Types

type TemplateFS

type TemplateFS struct {
	Content     embed.FS
	Embbed      bool
	DelimsLeft  string
	DelimsRigth string
	FuncMap     template.FuncMap
}

template FS mock

Example
// web could use TemplateFS to develop by embed mode or direct mode(files modify aware on running)
webTemplate := web.TemplateFS{Content: htmlDir, Embbed: false, DelimsLeft: "${{", DelimsRigth: "}}"}
templ, _, err := webTemplate.Parse("./", "web_test/*.html")
if err != nil {
	return
}

data := struct {
	Title string
	Body  string
}{
	Title: "My page",
	Body:  "here is html content",
}

templ.ExecuteTemplate(os.Stdout, "web2.html", data)

// to integrate with gin
// import "github.com/gin-gonic/gin"
// router := gin.Default()
// router.SetHTMLTemplate(templ)

// router.Run(":8080")
Output:

func (TemplateFS) Parse

func (t TemplateFS) Parse(nonEmbedPath string, patterns ...string) (*template.Template, []string, error)

Parse 此函数是模板文件系统(TemplateFS)的一部分。 可以处理embed与普通文件方式。 它接受一个路径参数,还有一个可变参数。 第一个if块检查Embbed字段的内容,如果它是true,它将使用文件内容(t.Content)和所提供的模式(patterns)创建一个模板。 否则,它将使用fs.Glob构建一个文件列表,其中包含给定模式(pattern)和nonEmbedPath参数指定的路径。 然后,他们使用文件名数组创建一个模板,并返回它

type WebDir

type WebDir struct {
	Prefix      string // relative path prefix read by direct from http filesytem
	EmbedPrefix string // relative path prefix read by embed mode
	Content     embed.FS
	Embbed      bool // flag to determin how to read
}
Example
// web could use WebDir to develop by embed mode or direct mode(files modify aware on running)
webdir := web.WebDir{Prefix: "./web_test", EmbedPrefix: "./web_test", Content: content, Embbed: true} // embed mode files modify not aware on runing

mutex := http.NewServeMux()
mutex.Handle("/", http.FileServer(webdir)) // visit http://localhost:8080/web.html
server := &http.Server{Addr: ":8080", Handler: mutex}
go func() {
	time.Sleep(5 * time.Second)
	server.Close()
}()

err := server.ListenAndServe()
if err != nil {
	log.Fatal(err)
}

// to integrate with gin
// import "github.com/gin-gonic/gin"
// router := gin.Default()
// router.StaticFS("/", webdir)

// router.Run(":8080")
Output:

func (WebDir) Open

func (d WebDir) Open(name string) (http.File, error)

Open implments Open method

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL