go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/projects/chirp/pkg/controller/api.go (about)

     1  /*
     2  
     3  Copyright (c) 2023 - Present. Will Charczuk. All rights reserved.
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository.
     5  
     6  */
     7  
     8  package controller
     9  
    10  import (
    11  	"net/http"
    12  
    13  	"go.charczuk.com/projects/chirp/pkg/config"
    14  	"go.charczuk.com/projects/chirp/pkg/model"
    15  	"go.charczuk.com/sdk/apputil"
    16  	"go.charczuk.com/sdk/web"
    17  )
    18  
    19  // API is the basic api controller.
    20  //
    21  // It handles things like getting the session and basically that's it.
    22  type API struct {
    23  	apputil.BaseController
    24  	Config config.Config
    25  	Model  model.Manager
    26  }
    27  
    28  // Register adds the controller routes to the app.
    29  func (a API) Register(app *web.App) {
    30  	app.Get("/api/v1/session", web.SessionAware(a.getSession))
    31  }
    32  
    33  // getSession gets the session.
    34  func (a API) getSession(r web.Context) web.Result {
    35  	session := r.Session()
    36  	if session == nil {
    37  		return web.JSON().NotAuthorized()
    38  	}
    39  	return web.JSON().Result(http.StatusOK, session)
    40  }