github.com/google/go-safeweb@v0.0.0-20231219055052-64d8cfc90fbb/examples/sample-application/cmd/main.go (about)

     1  // Copyright 2020 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //	https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build go1.16
    16  // +build go1.16
    17  
    18  package main
    19  
    20  import (
    21  	"flag"
    22  	"log"
    23  	"net"
    24  	"net/http"
    25  	"strconv"
    26  
    27  	"github.com/google/go-safeweb/safehttp"
    28  
    29  	"github.com/google/go-safeweb/examples/sample-application/secure"
    30  	"github.com/google/go-safeweb/examples/sample-application/server"
    31  	"github.com/google/go-safeweb/examples/sample-application/storage"
    32  )
    33  
    34  var (
    35  	port = flag.Int("port", 8080, "Port for the HTTP server")
    36  	dev  = flag.Bool("dev", false, "Run in dev mode")
    37  )
    38  
    39  func main() {
    40  	log.SetFlags(log.Flags() | log.Lshortfile)
    41  	flag.Parse()
    42  	safehttp.UseLocalDev() // TODO(clap): remove this
    43  	if *dev {
    44  		safehttp.UseLocalDev()
    45  	}
    46  	db := storage.NewDB()
    47  
    48  	addr := net.JoinHostPort("localhost", strconv.Itoa(*port))
    49  	mux := secure.NewMuxConfig(db, addr).Mux()
    50  	server.Load(db, mux)
    51  
    52  	log.Printf("Listening on %q", addr)
    53  	log.Fatal(http.ListenAndServe(addr, mux))
    54  }