{"id":16398852,"url":"https://github.com/sauldoescode/echo-memfile","last_synced_at":"2025-02-23T09:42:06.082Z","repository":{"id":71027848,"uuid":"109374040","full_name":"SaulDoesCode/echo-memfile","owner":"SaulDoesCode","description":"Super basic echo memory serving sollution","archived":false,"fork":false,"pushed_at":"2018-10-13T15:47:45.000Z","size":17,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-04T20:35:10.823Z","etag":null,"topics":["cache","cache-control","echo","echo-framework","go","golang","gzip","memory-cache","static-server"],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SaulDoesCode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-11-03T08:57:56.000Z","updated_at":"2023-06-01T14:21:40.000Z","dependencies_parsed_at":"2023-03-22T07:48:19.695Z","dependency_job_id":null,"html_url":"https://github.com/SaulDoesCode/echo-memfile","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaulDoesCode%2Fecho-memfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaulDoesCode%2Fecho-memfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaulDoesCode%2Fecho-memfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SaulDoesCode%2Fecho-memfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SaulDoesCode","download_url":"https://codeload.github.com/SaulDoesCode/echo-memfile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240298398,"owners_count":19779280,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","cache-control","echo","echo-framework","go","golang","gzip","memory-cache","static-server"],"created_at":"2024-10-11T05:14:04.358Z","updated_at":"2025-02-23T09:42:06.041Z","avatar_url":"https://github.com/SaulDoesCode.png","language":"Go","readme":"# echo-memfile - read a directory, cache it and serve files straight from memory\n\n### example\n\n```go\npackage main\n\nimport (\n\t\"time\"\n\n\t\"github.com/SaulDoesCode/echo-memfile\"\n\t\"github.com/labstack/echo\"\n)\n\nfunc main() {\n\tserver := echo.New() // your echo instance\n\n\tassetsDir := \"./assets\" // directory containing your static assets\n\tdevmode := true         // devmode will mostly log what's happening\n\n\t// MemFileInstance: read files and apply the middleware\n\tmfi := memfile.New(server, assetsDir, devmode)\n\n\t// Keep your files updated when you're developing\n\tif devmode {\n\t\tmfi.UpdateOnInterval(time.Second * 1)\n\t}\n\n\tserver.Start(\":1323\")\n}\n\n```\n\n### echo-memfile will serve index.html files in directories\n\nhttp://localhost:1323/ -\u003e ./assets/index.html   \nhttp://localhost:1323/blog -\u003e ./assets/blog/index.html   \n\n________\n##### note\n``mfi = MemFileInstance``\nfrom    \n``mfi := memfile.New(server *echo.Echo, assetsDir string, devmode bool)``\n_______\n\n### Updating files\n\n```go\n  // if you want to keep your files updated you can\n  // 1 - Update them manually as needed\n\n  // this reads the directory and updates files and etags as needed\n  mfi.Update()\n\n  // 2 - Update them regularly on an interval\n\n  // this runs memfile.Update every 5 seconds\n  mfi.UpdateOnInterval(time.Second * 5)\n\n  // to stop the interval updater\n  ticker := mfi.UpdateOnInterval(time.Second * 5)\n  if NeedsToStop {\n    ticker.Stop()\n  }\n```\n\n### Caching files from any path\n\n```go\n  location := \"../secret_files/pwd.txt\"\n  route := \"/secrets/pwd\"\n  err := mfi.CacheFile(location, route)\n  if err != nil {\n    // ...\n  }\n\n  // GET http://localhost:1323/secrets/pwd -\u003e pwd.txt\n```\n\n### Serving Files Manually\n\n```go\n  server.GET(\"/resource/\", func(c echo.Context) error {\n\n    if result, ok := mfi.Cached.Load(\"/resource.json\"); ok {\n      return mfi.ServeMF(c, result.(*memfile.MemFile))\n    }\n\n    return c.JSON(404, map[string]string{\n      \"err\": \"out of luck no resource here\",\n    })\n  })\n\n  // or\n  server.GET(\"/resource/\", func(c echo.Context) error {\n    return mfi.ServeFile(c, \"/resource.json\")\n  })\n\n  // or shorthand\n  mfi.ServeMemFile(\"/resource\", \"/resource.json\")\n\n  // NOTE: the resource's string corresponds to files under the assets dir\n  // \"./assets/resource.json\" -\u003e \"/resource.json\"\n```\n\n### HTTP2 Push with (myfile.html).push\n\n```\n\t./assets\n\t\u003e\t\tindex.html\n\t\u003e \tstyles.css\n\t\u003e \tpicture.jpg\n\t\u003e \tindex.html.push\n```\n\n``index.html.push`` is essentially just a json array containing serve routes to push with your html file\n```json\n[\"/styles.css\",  \"/picture.jpg\"]\n```\n\n### HTTP2 Push By MemFileInstance.SetPushAssets\nUse this method for best results because manually modifying MemFiles can lead to corruption (in memory, your files are safe)\nSetPushAssets uses a mutex lock and unlock internally to avoid concurrent read-write related issues.\n\n```go\n\tmfi.SetPushAssets(\"/route.html\", []string{\"/route.css\", \"/route.js\"})\n```\n\n### HTTP2 Push By Modifying MemFile.PushAssets ([]string)\n\n```go\nresult, exists := mfi.Cached.Load(\"/subdir/index.html\")\nif exists {\n\tresult.(*memfile.MemFile).PushAssets = []string{\"/js/rilti.min.js\", \"/css/bulma.min.css\"}\n}\n// note if you're doing it this way do it before the server starts\n// because of potential MemFile corruption from concurrent read-write issues\n// MFI.SetPushAssets uses mutex locks internally to avoid this\n```\n\n### API\n\n##### memfile\n* ``.New(server *echo.Echo, dir string, devmode bool) MemFileInstance``\n* ``.CompressBytes(data []byte) ([]byte, error)`` gzip a byte slice\n* ``.ServeMemFile(res http.ResponseWriter, req *http.Request, memFile MemFile, CacheControl string) error``\n* ``.ServablePath(dir string, loc string) string`` cleans a filepath (under dir) for use as a url\n* ``.RandBytes(size int) []byte``\n* ``.RandStr(size int) string``\n* ``.MemFileInstance{}``\n* ``.MemFile{}``\n\n\n##### MemFileInstance\n* ``.CacheFile(location string, servePath string) error`` read a file and serve it at a particular route\n* ``.Update()`` check files for changes and update accordingly\n* ``.UpdateOnInterval(interval time.Duration) *time.Ticker`` Keep updating regularly on a duration\n* ``.ServeFile(c echo.Context, filename string) error``\n* ``.ServeMF(c echo.Context, memFile *MemFile) error``\n* ``.ServeMemFile(route string, filename string)`` shorthand handler\n* ``.Serve(res http.ResponseWriter, req *http.Request, filename string) error`` for other middleware etc.\n* ``.CacheControl`` the Cache-Control header is ``\"private, must-revalidate\"`` by default, but you can change it\n* ``.DevMode``\n* ``.Cached``:``*sync.Map``, this contains all the MemFile's in an Instance [assumed types (key string, value MemFile)]\n\n\n#### public domain, do whatever man\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsauldoescode%2Fecho-memfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsauldoescode%2Fecho-memfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsauldoescode%2Fecho-memfile/lists"}