nogo

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2021 License: MIT Imports: 9 Imported by: 0

README

nogo

Nogo helps to compile non-go files into go binaries. It might be helpful if you want to get a kickstart while writing your own, small library. If you are looking for a well-written, feature-complete solution, please have a look at: https://github.com/markbates/pkger.

The nogo-method will only work if you are using go modules.

1) generate nogo.go

# install nogogen
go get github.com/rbicker/nogo/cmd/nogogen

# run nogogen to generate a a nogo file within your golang project
nogogen

# by default, nogogen will include a folder called "assets" and all of it's subfolders and -files
# if you want to include other (maybe multiple) directories, use the NOGO_DIRS env variable
NOGO_DIRS="/templates /public" nogogen
# please make sure to use absolute paths, using your project directory as root
# the command generates a file called "nogo.go" under "internal/nogo"

2) use nogo

package main

import (
	"fmt"
	"html/template"
	"io"
	"log"
	"net/http"
	"strings"

	"github.com/rbicker/nogo-playground/internal/nogo"
)

func main() {
	// serve template
	http.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "GET" {
			w.WriteHeader(http.StatusNotImplemented)
		}
		f, err := nogo.Get("/assets/templates/test.html")
		if err != nil {
			log.Printf("error while opening test html file: %v", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		buf := new(strings.Builder)
		_, err = io.Copy(buf, f)
		if err != nil {
			log.Printf("error while reading test html file: %v", err)
			w.WriteHeader(http.StatusInternalServerError)
			return
		}
		t, err := template.New("").Parse(buf.String())
		t.Execute(w, struct {
			Foo string
		}{
			Foo: "Bar",
		})
	})

	// serve static files
	http.Handle("/public/", http.StripPrefix("/public/", http.FileServer(nogo.Dir("/assets/public"))))
	http.ListenAndServe(":3000", nil)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add(name string, b []byte)

Add adds the given bytes to nogo under the given FileName.

Types

type Dir

type Dir string

Dir corresponds to http.FileSystem.

func (Dir) Open

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

Open returns a http.File based on the given FileName. The function corresponds to http.FileSystem.

type File

type File struct {
	//File os.File
	FileInfo FileInfo
	Content  []byte
	DirInfos []FileInfo
	// contains filtered or unexported fields
}

File represents a file's Content and it's FileInfo. It implements the http.File interface.

func Get

func Get(name string) (*File, error)

Get decodes the file with the given FileName.

func LoadFile

func LoadFile(name string) (File, error)

LoadFile creates a new nogo file.

func (*File) Close

func (f *File) Close() error

Close implements the io.Closer interface. It does not do anything at the moment.

func (*File) Read

func (f *File) Read(b []byte) (int, error)

Read implements the io.Reader interface.

func (*File) Readdir

func (f *File) Readdir(n int) ([]os.FileInfo, error)

Readdir corresponds to the http.file interface.

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

Seek implements the io.Seeker interface.

func (*File) Stat

func (f *File) Stat() (os.FileInfo, error)

Stat corresponds to the http.file interface.

type FileInfo

type FileInfo struct {
	FileName    string
	FileSize    int64
	FileMode    os.FileMode
	FileModTime time.Time
	FileIsDir   bool
}

FileInfo corresponds to os.FileInfo.

func NewFileInfo added in v0.1.1

func NewFileInfo(info os.FileInfo) FileInfo

func (*FileInfo) IsDir

func (info *FileInfo) IsDir() bool

IsDir returns true if the file is a directory.

func (*FileInfo) ModTime

func (info *FileInfo) ModTime() time.Time

ModTime returns the file's modification time.

func (*FileInfo) Mode

func (info *FileInfo) Mode() os.FileMode

Mode returns the file's FileMode.

func (*FileInfo) Name

func (info *FileInfo) Name() string

Name returns the file's FileName.

func (*FileInfo) Size

func (info *FileInfo) Size() int64

Size returns the file's FileSize.

func (*FileInfo) Sys

func (info *FileInfo) Sys() interface{}

Sys returns nil.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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