github.com/kotovmak/go-admin@v1.1.1/examples/chi/main.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"os"
     7  	"os/signal"
     8  	"path/filepath"
     9  	"strings"
    10  	"time"
    11  
    12  	_ "github.com/kotovmak/go-admin/adapter/chi"
    13  	"github.com/kotovmak/go-admin/examples/datamodel"
    14  	_ "github.com/kotovmak/go-admin/modules/db/drivers/mysql"
    15  
    16  	"github.com/GoAdminGroup/themes/adminlte"
    17  	"github.com/go-chi/chi/v5"
    18  	"github.com/kotovmak/go-admin/engine"
    19  	"github.com/kotovmak/go-admin/modules/config"
    20  	"github.com/kotovmak/go-admin/modules/language"
    21  	"github.com/kotovmak/go-admin/plugins/example"
    22  	"github.com/kotovmak/go-admin/template"
    23  	"github.com/kotovmak/go-admin/template/chartjs"
    24  )
    25  
    26  func main() {
    27  	r := chi.NewRouter()
    28  
    29  	eng := engine.Default()
    30  
    31  	cfg := config.Config{
    32  		Env: config.EnvLocal,
    33  		Databases: config.DatabaseList{
    34  			"default": {
    35  				Host:            "127.0.0.1",
    36  				Port:            "3306",
    37  				User:            "root",
    38  				Pwd:             "root",
    39  				Name:            "godmin",
    40  				MaxIdleConns:    50,
    41  				MaxOpenConns:    150,
    42  				ConnMaxLifetime: time.Hour,
    43  				Driver:          config.DriverMysql,
    44  			},
    45  		},
    46  		UrlPrefix: "admin",
    47  		Store: config.Store{
    48  			Path:   "./uploads",
    49  			Prefix: "uploads",
    50  		},
    51  		Language:    language.EN,
    52  		IndexUrl:    "/",
    53  		Debug:       true,
    54  		ColorScheme: adminlte.ColorschemeSkinBlack,
    55  	}
    56  
    57  	template.AddComp(chartjs.NewChart())
    58  
    59  	// customize a plugin
    60  
    61  	examplePlugin := example.NewExample()
    62  
    63  	// load from golang.Plugin
    64  	//
    65  	// examplePlugin := plugins.LoadFromPlugin("../datamodel/example.so")
    66  
    67  	// customize the login page
    68  	// example: https://github.com/GoAdminGroup/demo.go-admin.cn/blob/master/main.go#L39
    69  	//
    70  	// template.AddComp("login", datamodel.LoginPage)
    71  
    72  	// load config from json file
    73  	//
    74  	// eng.AddConfigFromJSON("../datamodel/config.json")
    75  
    76  	if err := eng.AddConfig(&cfg).
    77  		AddGenerators(datamodel.Generators).
    78  		AddDisplayFilterXssJsFilter().
    79  		// add generator, first parameter is the url prefix of table when visit.
    80  		// example:
    81  		//
    82  		// "user" => http://localhost:9033/admin/info/user
    83  		//
    84  		AddGenerator("user", datamodel.GetUserTable).
    85  		AddPlugins(examplePlugin).
    86  		Use(r); err != nil {
    87  		panic(err)
    88  	}
    89  
    90  	workDir, _ := os.Getwd()
    91  	filesDir := filepath.Join(workDir, "uploads")
    92  	FileServer(r, "/uploads", http.Dir(filesDir))
    93  
    94  	// you can custom your pages like:
    95  
    96  	eng.HTML("GET", "/admin", datamodel.GetContent)
    97  
    98  	go func() {
    99  		_ = http.ListenAndServe(":3333", r)
   100  	}()
   101  
   102  	quit := make(chan os.Signal, 1)
   103  	signal.Notify(quit, os.Interrupt)
   104  	<-quit
   105  	log.Print("closing database connection")
   106  	eng.MysqlConnection().Close()
   107  }
   108  
   109  // FileServer conveniently sets up a http.FileServer handler to serve
   110  // static files from a http.FileSystem.
   111  func FileServer(r chi.Router, path string, root http.FileSystem) {
   112  	if strings.ContainsAny(path, "{}*") {
   113  		panic("FileServer does not permit URL parameters.")
   114  	}
   115  
   116  	fs := http.StripPrefix(path, http.FileServer(root))
   117  
   118  	if path != "/" && path[len(path)-1] != '/' {
   119  		r.Get(path, http.RedirectHandler(path+"/", http.StatusMovedPermanently).ServeHTTP)
   120  		path += "/"
   121  	}
   122  	path += "*"
   123  
   124  	r.Get(path, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   125  		fs.ServeHTTP(w, r)
   126  	}))
   127  }