github.com/IRelaxxx/servefiles/v3@v3.4.6/webserver/example.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2016 Rick Beton 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 // 12 // The above copyright notice and this permission notice shall be included in all 13 // copies or substantial portions of the Software. 14 // 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 // SOFTWARE. 22 23 // This package provides an example webserver. The purpose is mostly to show by example how to serve assets. 24 // It supports both HTTP and HTTPS. 25 package main 26 27 import ( 28 "crypto/tls" 29 "flag" 30 "fmt" 31 "log" 32 "net/http" 33 "time" 34 35 "github.com/IRelaxxx/servefiles/v3" 36 ) 37 38 var path = flag.String("path", "..", "directory for the files tp be served") 39 var cert = flag.String("cert", "", "file containing the certificate (optional)") 40 var key = flag.String("key", "", "file containing the private key (optional)") 41 var port = flag.Int("port", 8080, "TCP port to listen on") 42 var maxAge = flag.String("maxage", "", "Maximum age of assets sent in response headers - causes client caching") 43 var verbose = flag.Bool("v", false, "Enable verbose messages") 44 45 func main() { 46 flag.Parse() 47 48 if *verbose { 49 servefiles.Debugf = log.Printf 50 } 51 52 if (*cert != "" && *key == "") || 53 (*cert == "" && *key != "") { 54 log.Fatal("Both certificate file (-cert) and private key file (-key) are required.") 55 } 56 57 h := servefiles.NewAssetHandler(*path) 58 59 if *maxAge != "" { 60 d, err := time.ParseDuration(*maxAge) 61 log.Printf("MaxAge: %s %v\n", d, err) 62 h = h.WithMaxAge(d) 63 } 64 65 srv := &http.Server{ 66 Addr: fmt.Sprintf(":%d", *port), 67 Handler: h, 68 } 69 70 if *cert != "" { 71 srv.TLSConfig = &tls.Config{ 72 MinVersion: tls.VersionTLS12, 73 CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256}, 74 PreferServerCipherSuites: true, 75 CipherSuites: []uint16{ 76 tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 77 tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 78 tls.TLS_RSA_WITH_AES_256_GCM_SHA384, 79 tls.TLS_RSA_WITH_AES_256_CBC_SHA, 80 tls.TLS_AES_256_GCM_SHA384, 81 }, 82 } 83 log.Printf("Access the server via: https://localhost:%d/", *port) 84 log.Fatal(srv.ListenAndServeTLS(*cert, *key)) 85 86 } else { 87 log.Printf("Access the server via: http://localhost:%d/", *port) 88 log.Fatal(srv.ListenAndServe()) 89 } 90 }