{"id":22214189,"url":"https://github.com/grimdork/sweb","last_synced_at":"2025-03-25T06:24:32.291Z","repository":{"id":57559901,"uuid":"325389388","full_name":"grimdork/sweb","owner":"grimdork","description":"Simple web server quickstart.","archived":false,"fork":false,"pushed_at":"2021-12-05T03:42:43.000Z","size":31,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-30T05:43:38.240Z","etag":null,"topics":["package","quickstart","server","web","webapp"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/grimdork.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-12-29T21:20:51.000Z","updated_at":"2021-12-05T03:42:30.000Z","dependencies_parsed_at":"2022-08-26T07:00:58.238Z","dependency_job_id":null,"html_url":"https://github.com/grimdork/sweb","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimdork%2Fsweb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimdork%2Fsweb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimdork%2Fsweb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/grimdork%2Fsweb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/grimdork","download_url":"https://codeload.github.com/grimdork/sweb/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245409704,"owners_count":20610590,"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":["package","quickstart","server","web","webapp"],"created_at":"2024-12-02T21:14:18.013Z","updated_at":"2025-03-25T06:24:32.271Z","avatar_url":"https://github.com/grimdork.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# sweb\nServe webstuff.\n\n## What?\nThis is a quickstart package to get a web server app running, waiting on a port and IP address specified in the environment, and serving static files from a similarly specified directory.\n\nThis is geared at things running in typical Docker, AWS etc. setups behind a reverse proxy which handles the domain and certificates. If you need S3, GCP or other special backend storage support, write a route for it. See chi documentation for more on how routing works.\n\n## How do I try it?\nThe minimal example:\n\n```go\nsrv := sweb.New()\nsrv.Start()\n// do stuff, wait for CTRL-C, whatever\nsrv.Stop()\n```\n\nThis will launch a web server bound to the IP address 127.0.0.1, port 15000, loading static files from the folder `static` in the working path of the program.\n\nTo configure these settings, use the following environment variables:\n```\nWEBHOST\nWEBPORT\nWEBSTATIC\n```\n\n# How do I customise the root path?\nEmbed the Server struct into a struct of your own, run srv.Init(), optionally srv.InitMiddleware() and add routes manually from there with WebGet(), WebGets() and Route().\n\nExample:\n```go\ntype MyServer struct {\n\tsweb.Server\n}\n…\nsrv:=\u0026MyServer{}\n// Add router and logger\nsrv.Init()\n// Add four pieces of middleware suitable for HTTP\nsrv.InitMiddleware()\nsrv.WebGet(\"/\", srv.Static)\nsrv.WebGets(\"/{page}\", func(r chi.Router) {\n\tr.Get(\"/*\", srv.Static)\n\tr.Options(\"/\", sweb.Preflight)\n})\n…\nsrv.Stop()\n```\n\nThis works like the simpler example, setting things up like the defaults.\n\nExample setup for a `/api` route alongside the default handler:\n```go\nsrv.Route(\"/api\", func(r chi.Router) {\n\t// API middleware\n\tr.Use(\n\t\tmiddleware.NoCache, // chi\n\t\tmiddleware.RealIP, // chi\n\t\tsweb.AddCORS,\n\t\tmiddleware.Timeout(time.Second*10), // chi\n\t)\n\t// TODO: Insert r.NotFound(w) call here if desirable\n\tr.Options(\"/\", sweb.Preflight)\n\t// Longer endpoints first, otherwise the \"/\" route will be used for everything\n\tr.Get(\"/hello\", func(w http.ResponseWriter, r *http.Request) {\n\t\tw.Write([]byte(\"Hello \" + r.RemoteAddr))\n\t})\n\tr.Get(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\t\tw.Write([]byte(\"v1\"))\n\t})\n})\n```\n\n## Middleware\nThese middlware functions are available for use, in addition to everything found in chi:\n\n- AddJSONHeaders: For typical REST endpoints\n- AddHTMLHeaders: For regular browser-friendly pages\n- Preflight: Sets up options for REST calls\n- AddCORS: Allows REST calls from other domains\n- AddSecureHeaders: Forces secure pages when behind a HTTPS reverse proxy\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrimdork%2Fsweb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgrimdork%2Fsweb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgrimdork%2Fsweb/lists"}