github.com/mssola/todo@v0.0.0-20181029153210-d25348dc3f48/app/root.go (about)

     1  // Copyright (C) 2014-2017 Miquel Sabaté Solà <mikisabate@gmail.com>
     2  //
     3  // This Source Code Form is subject to the terms of the Mozilla Public
     4  // License, v. 2.0. If a copy of the MPL was not distributed with this
     5  // file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7  package app
     8  
     9  import (
    10  	"net/http"
    11  
    12  	"github.com/mssola/todo/lib"
    13  )
    14  
    15  // RootIndex renders the root page. It has three different options:
    16  //
    17  //  1. If there's no user, it renders the "Create user" page.
    18  //  2. If the current user is not logged in, it render the "Login" page.
    19  //  3. If the current user is logged in, then it redirects the user to the
    20  //     /topics page.
    21  func RootIndex(res http.ResponseWriter, req *http.Request) {
    22  	id := lib.GetCookie(req, "userId")
    23  
    24  	if id == nil {
    25  		count := Count("users")
    26  		if count == 0 {
    27  			lib.Render(res, "users/new", lib.DefaultViewData())
    28  		} else {
    29  			lib.Render(res, "application/login", lib.DefaultViewData())
    30  		}
    31  	} else {
    32  		http.Redirect(res, req, "/topics", http.StatusFound)
    33  	}
    34  }