├── .gitignore ├── LICENSE ├── README.md ├── Setup.hs ├── app ├── DemoCertificate.hs └── Main.hs ├── package.yaml └── srv.cabal /.gitignore: -------------------------------------------------------------------------------- 1 | dist 2 | dist-* 3 | cabal-dev 4 | *.o 5 | *.hi 6 | *.hie 7 | *.chi 8 | *.chs.h 9 | *.dyn_o 10 | *.dyn_hi 11 | .hpc 12 | .hsenv 13 | .cabal-sandbox/ 14 | cabal.sandbox.config 15 | *.prof 16 | *.aux 17 | *.hp 18 | *.eventlog 19 | .stack-work/ 20 | cabal.project.local 21 | cabal.project.local~ 22 | .HTF/ 23 | .ghc.environment.* 24 | .idea/ 25 | *.pem 26 | *.csr 27 | .vscode/ 28 | *.iml 29 | stack.yaml.lock 30 | stack.yaml 31 | srv.yaml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # srv 2 | `srv` is a tiny web server for local deployments. 3 | I found it useful for development and testing of static websites and blogs 4 | 5 | ## Installation 6 | 7 | ```bash 8 | $ cabal install srv 9 | ``` 10 | 11 | ## build from source 12 | 13 | ```bash 14 | 15 | $ git clone https://github.com/thma/srv.git 16 | $ cd srv 17 | $ stack install 18 | 19 | -- or if you don't have stack installed 20 | 21 | $ cabal install 22 | ``` 23 | 24 | ## Usage 25 | 26 | ```bash 27 | $ cd /path/to/your/documentRoot 28 | $ srv 29 | 30 | starting up srv... 31 | srv.yaml not found, generating file with default config 32 | Starting HTTP server on port 8080 33 | Starting HTTPS server on port 8443 34 | ``` 35 | 36 | During the initial run, `srv` will create a config file `srv.yaml` 37 | in the current directory. 38 | 39 | You can edit this file to change the port, the directory to serve, 40 | and also select whether to handle HTTP or HTTPS or both. 41 | 42 | If HTTPS is selected, you can also provide a certificate and key file. 43 | If you don't provide both files, srv will use a predefined 44 | self-signed certificate which is meant for local demo use only. 45 | (using this demo certificate will result in a warning in your browser 46 | and some browsers will even refuse to connect.) 47 | 48 | By default, `srv` will serve the current directory. You can change this by 49 | editing the entry `documentRoot` in the `srv.yaml` file. 50 | 51 | By default `srv` will also open a browser window pointing to the directory configured in `documentRoot`. This behaviour can be changed by setting the entry `autoOpenBrowser` to `false`. 52 | 53 | ## Configuration 54 | 55 | Here is a sample configuration file: 56 | 57 | ```yaml 58 | autoOpenBrowser: true 59 | documentRoot: . 60 | handlers: 61 | - - HTTP 62 | - 8080 63 | - - HTTPS 64 | - 8443 65 | pathToCert: /path/to/certificate.pem 66 | pathToKey: /path/to/key.pem 67 | ``` 68 | 69 | ## How to generate your own certificate 70 | 71 | ```bash 72 | openssl genrsa -out key.pem 2048 73 | openssl req -new -key key.pem -out certificate.csr 74 | openssl x509 -req -in certificate.csr -signkey key.pem -out certificate.pem 75 | ``` 76 | 77 | 78 | 79 | 80 | -------------------------------------------------------------------------------- /Setup.hs: -------------------------------------------------------------------------------- 1 | import Distribution.Simple 2 | main = defaultMain 3 | -------------------------------------------------------------------------------- /app/DemoCertificate.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE OverloadedStrings #-} 2 | {-# LANGUAGE QuasiQuotes #-} 3 | 4 | module DemoCertificate 5 | ( demoTLSSettings, 6 | ) 7 | where 8 | 9 | {-- 10 | This module provides a self signed certificate for testing the https server. 11 | It was generated with the following commands: 12 | 13 | openssl genrsa -out key.pem 2048 14 | openssl req -new -key key.pem -out certificate.csr 15 | openssl x509 -req -in certificate.csr -signkey key.pem -out certificate.pem 16 | 17 | --} 18 | 19 | import qualified Data.ByteString as S 20 | import Network.Wai.Handler.WarpTLS (TLSSettings (..), tlsSettingsMemory) 21 | import Text.RawString.QQ (r) 22 | 23 | demoTLSSettings :: TLSSettings 24 | demoTLSSettings = tlsSettingsMemory certificate key 25 | 26 | certificate :: S.ByteString 27 | certificate = 28 | [r|-----BEGIN CERTIFICATE----- 29 | MIIDczCCAlsCFGVNzDOVmV85+d//lhqENnblNF8EMA0GCSqGSIb3DQEBCwUAMHYx 30 | CzAJBgNVBAYTAkRFMQwwCgYDVQQIDANOUlcxDjAMBgNVBAcMBUVzc2VuMQ4wDAYD 31 | VQQKDAVNaWVrZTEMMAoGA1UECwwDQ0VPMQ4wDAYDVQQDDAVNaWVrZTEbMBkGCSq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MWgi69/vLOeAcY0wBIUxPC/QYn 68 | bIcTqCQpEbz5dpZvtKQgxAOrRdkA+Pw2DifE7A91fIf4/NhmjPGMFg7tjW5E8Efa 69 | vPjfhE1jUTyLLdpXIAIEAadbjNhkQu+C0HjBbnZLAoGBAJRhW4OVntOEMSbjfTpw 70 | CR0TR0T630zlYulyBKd1w8AQxWyiWXKIPtQ2ZA4Nv2TlgMYVb/JnMxOWOSJ1dbHI 71 | Q9zgwe+Fo1pRfFPLpPpzjiVHdKMFaRTrjSbzU08Y1jW1lNdZNkNrHPQ0AcHZvzml 72 | WGFYaNQhjKgkJb9b7a6do4jXAoGBAJpyuM/f5b78hG5pYcE76kc7WmbuSjqjUcoz 73 | 5cw7VTFxXzJzOqSGAZfD7QHWWG4rz7Vjwm59tCxYp0Buo/HCmSaMPUmZrVVyiZG3 74 | YnUvPM6yQSJVJlYYblcHPLnxVhPIFvk0gV0AGoMAb4OQLCkzucUYEm0cR461tg1S 75 | PGOTYVsZAoGAVQ7k7jnWC/91FikRR3/MuG02zaEAcd9VeioDguuOqyPAIxqQjV+N 76 | d9vA3TBVpgYaUqImnc4eJST4g8oRNK0fPMwKxlUnuJv0dKb/iA3UDfdD59mHbENv 77 | VFEmMo/MDbeBYV758HP6+EMNaAYl1LjrlyiI7ypaUrcc+mPiwVA5vn8= 78 | -----END RSA PRIVATE KEY-----|] 79 | -------------------------------------------------------------------------------- /app/Main.hs: -------------------------------------------------------------------------------- 1 | {-# LANGUAGE DeriveAnyClass #-} 2 | {-# LANGUAGE DeriveGeneric #-} 3 | 4 | module Main where 5 | 6 | import Control.Concurrent.Async (mapConcurrently_) 7 | import Control.Monad (when) 8 | import qualified Data.Yaml as Yaml 9 | import DemoCertificate (demoTLSSettings) 10 | import GHC.Generics (Generic) 11 | import Network.Wai (Application) 12 | import Network.Wai.Application.Static (defaultFileServerSettings, 13 | staticApp) 14 | import Network.Wai.Handler.Warp (defaultSettings, run, setPort) 15 | import Network.Wai.Handler.WarpTLS (TLSSettings, runTLS, 16 | tlsSettings) 17 | import System.Directory (doesFileExist) 18 | import System.Info (os) 19 | import System.Process (createProcess, shell) 20 | 21 | -- | a data type representing Http or Https warp handlers 22 | data WarpHandler = HTTP | HTTPS deriving (Show, Eq, Generic, Yaml.FromJSON, Yaml.ToJSON) 23 | 24 | -- | a tuple (WarpHandler, IP port) 25 | type Handler = (WarpHandler, Int) 26 | 27 | -- | configuration settings that specify the behaviour of the server 28 | data SrvConfig = SrvConfig 29 | { handlers :: ![Handler], 30 | pathToCert :: !(Maybe FilePath), 31 | pathToKey :: !(Maybe FilePath), 32 | documentRoot :: !FilePath, 33 | autoOpenBrowser :: !Bool 34 | } 35 | deriving (Show, Eq, Generic, Yaml.FromJSON, Yaml.ToJSON) 36 | 37 | -- | create a configuration with default settings 38 | defaultConfig :: SrvConfig 39 | defaultConfig = 40 | SrvConfig 41 | { handlers = [(HTTP, 8080), (HTTPS, 8443)], 42 | pathToCert = Nothing, -- Just "certificate.pem", 43 | pathToKey = Nothing, -- Just "key.pem", 44 | documentRoot = ".", 45 | autoOpenBrowser = True 46 | } 47 | 48 | -- | create a static WAI Application from a configuration 49 | staticAppFrom :: SrvConfig -> Application 50 | staticAppFrom config = 51 | staticApp $ 52 | defaultFileServerSettings (documentRoot config) 53 | 54 | -- create an HTTP handler action based on config settings 55 | httpHandler :: Handler -> SrvConfig -> IO () 56 | httpHandler (_, port) config = do 57 | putStrLn $ "Starting HTTP server on port " <> show port 58 | when (autoOpenBrowser config) $ openBrowser HTTP port 59 | run port $ staticAppFrom config 60 | 61 | -- create an HTTPS handler action based on config settings 62 | httpsHandler :: Handler -> SrvConfig -> IO () 63 | httpsHandler (_, port) config = do 64 | putStrLn $ "Starting HTTPS server on port " <> show port 65 | when (autoOpenBrowser config) $ openBrowser HTTPS port 66 | runTLS 67 | (getTlsSettings (pathToCert config) (pathToKey config)) 68 | (setPort port defaultSettings) 69 | (staticAppFrom config) 70 | 71 | -- | create a TLS settings object from file path settings. If no file paths are 72 | -- provided, use the demo in-memory certificate 73 | getTlsSettings :: Maybe FilePath -> Maybe FilePath -> TLSSettings 74 | getTlsSettings (Just cert) (Just key) = tlsSettings cert key 75 | getTlsSettings _ _ = demoTLSSettings 76 | 77 | -- | derive all handler IO actions from the configuration 78 | getAllHandlers :: SrvConfig -> [IO ()] 79 | getAllHandlers config = 80 | map (handlerToAction config) (handlers config) 81 | where 82 | handlerToAction :: SrvConfig -> Handler -> IO () 83 | handlerToAction cfg handler = case handler of 84 | (HTTP, _) -> httpHandler handler cfg 85 | (HTTPS, _) -> httpsHandler handler cfg 86 | 87 | configFile :: FilePath 88 | configFile = "srv.yaml" 89 | 90 | -- | start all handlers configured in config.yaml 91 | main :: IO () 92 | main = do 93 | putStrLn "starting up srv..." 94 | configIsPresent <- doesFileExist configFile 95 | config <- 96 | if configIsPresent 97 | then do 98 | putStrLn $ "reading " ++ configFile ++ "..." 99 | Yaml.decodeFileThrow configFile 100 | else do 101 | putStrLn $ configFile ++ " not found, generating file with default config" 102 | Yaml.encodeFile configFile defaultConfig 103 | return defaultConfig 104 | -- run all handlers defined in config 105 | mapConcurrently_ id (getAllHandlers config) 106 | 107 | -- | open the localhost site in the default browser. 108 | openBrowser :: WarpHandler -> Int -> IO () 109 | openBrowser handler port = do 110 | case os of 111 | "mingw32" -> createProcess (shell $ "start " ++ url) 112 | "darwin" -> createProcess (shell $ "open " ++ url) 113 | _ -> createProcess (shell $ "xdg-open " ++ url) 114 | return () 115 | where 116 | url = case handler of 117 | HTTP -> "http://localhost:" ++ show port 118 | HTTPS -> "https://localhost:" ++ show port 119 | -------------------------------------------------------------------------------- /package.yaml: -------------------------------------------------------------------------------- 1 | name: srv 2 | version: 0.1.1.0 3 | github: "thma/srv" 4 | license: Apache-2.0 5 | author: "Thomas Mahler" 6 | maintainer: "thma@apache.org" 7 | copyright: "2022 Thomas Mahler" 8 | 9 | extra-source-files: 10 | - README.md 11 | 12 | # Metadata used when publishing your package 13 | synopsis: A simple web server for local usage. 14 | category: Web 15 | 16 | # To avoid duplicated efforts in documentation and dealing with the 17 | # complications of embedding Haddock markup inside cabal files, it is 18 | # common to point users to the README.md file. 19 | description: Please see the README on GitHub at 20 | 21 | dependencies: 22 | - base >= 4.7 && < 5 23 | - wai 24 | - wai-app-static 25 | - warp 26 | - warp-tls 27 | - async 28 | - yaml 29 | - directory 30 | - containers 31 | - raw-strings-qq 32 | - bytestring 33 | - process 34 | 35 | executables: 36 | srv: 37 | main: Main.hs 38 | source-dirs: app 39 | ghc-options: 40 | - -threaded 41 | - -rtsopts 42 | - -with-rtsopts=-N 43 | 44 | 45 | -------------------------------------------------------------------------------- /srv.cabal: -------------------------------------------------------------------------------- 1 | cabal-version: 1.12 2 | 3 | -- This file has been generated from package.yaml by hpack version 0.35.0. 4 | -- 5 | -- see: https://github.com/sol/hpack 6 | 7 | name: srv 8 | version: 0.1.1.0 9 | synopsis: A simple web server for local usage. 10 | description: Please see the README on GitHub at 11 | category: Web 12 | homepage: https://github.com/thma/srv#readme 13 | bug-reports: https://github.com/thma/srv/issues 14 | author: Thomas Mahler 15 | maintainer: thma@apache.org 16 | copyright: 2022 Thomas Mahler 17 | license: Apache-2.0 18 | license-file: LICENSE 19 | build-type: Simple 20 | extra-source-files: 21 | README.md 22 | 23 | source-repository head 24 | type: git 25 | location: https://github.com/thma/srv 26 | 27 | executable srv 28 | main-is: Main.hs 29 | other-modules: 30 | DemoCertificate 31 | Paths_srv 32 | hs-source-dirs: 33 | app 34 | ghc-options: -threaded -rtsopts -with-rtsopts=-N 35 | build-depends: 36 | async 37 | , base >=4.7 && <5 38 | , bytestring 39 | , containers 40 | , directory 41 | , process 42 | , raw-strings-qq 43 | , wai 44 | , wai-app-static 45 | , warp 46 | , warp-tls 47 | , yaml 48 | default-language: Haskell2010 49 | --------------------------------------------------------------------------------