Skip to content

Instantly share code, notes, and snippets.

package main
import (
"bytes"
"fmt"
"io"
"log"
"os"
"sync"
)
func (crud *CRUD) registerD() gomHTTP.ServerRoute {
// build create sql
crud.Config.sqlCRUDDelete = fmt.Sprintf(sqlCRUDDelete, crud.Config.TableName,
crud.Config.pk.name)
return gomHTTP.ServerRoute{
Name: "crud_delete_" + crud.Config.TableName,
Method: http.MethodDelete,
Path: fmt.Sprintf("/%s", crud.Config.TableName),
Validators: []gomHTTP.ParamValidator{validatePrimaryKey(crud.Config.pk.index)},
Handler: crud.handleDelete,
func (crud *CRUD) Delete(data interface{}) (int64, error) {
rv := reflect.ValueOf(data)
rv = reflect.Indirect(rv)
pk := rv.Field(crud.Config.pk.index)
if !pk.CanInterface() {
return 0, errors.Errorf("table %s with primary key has wrong interface type",
crud.Config.TableName, crud.Config.pk.index)
}
result, err := crud.Config.DB.Exec(crud.Config.sqlCRUDDelete, pk.Interface())
if err != nil {
func (crud *CRUD) registerL() gomHTTP.ServerRoute {
// build list sql
fields := crud.Config.listFields
if len(fields) == 0 {
// allow update all fields
fields = crud.Config.fields
}
fieldNames := make([]string, len(fields))
for i, field := range fields {
fieldNames[i] = field.name
// List lists data and paging the result
func (crud *CRUD) List(pageID, perPage int64) ([]interface{}, error) {
offset := (pageID - 1) * perPage
rows, err := crud.Config.DB.Queryx(crud.Config.sqlCRUDList, offset, perPage)
if err != nil {
return nil, errors.Wrap(err, "error crud list")
}
defer rows.Close()
result := []interface{}{}
package main
import (
"flag"
"log"
"net/http"
"strings"
)
func main() {
// FileSystem custom file system handler
type FileSystem struct {
fs http.FileSystem
}
// Open opens file
func (fs FileSystem) Open(path string) (http.File, error) {
f, err := fs.fs.Open(path)
if err != nil {
return nil, err
package main
import (
"flag"
"log"
"net/http"
"strings"
)
// FileSystem custom file system handler
package main
import (
"log"
"sync"
)
func main() {
cond := sync.NewCond(&sync.Mutex{})
var wg1 sync.WaitGroup
// OR function combines all signal from channels into a single channel with Or condition
func OR(channels ...<-chan interface{}) <-chan interface{} {
switch len(channels) {
case 0:
return nil
case 1:
return channels[0]
}
orDone := make(chan interface{})
go func() {