code.gitea.io/gitea@v1.22.3/modules/web/middleware/data.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package middleware
     5  
     6  import (
     7  	"context"
     8  	"time"
     9  
    10  	"code.gitea.io/gitea/modules/setting"
    11  )
    12  
    13  // ContextDataStore represents a data store
    14  type ContextDataStore interface {
    15  	GetData() ContextData
    16  }
    17  
    18  type ContextData map[string]any
    19  
    20  func (ds ContextData) GetData() ContextData {
    21  	return ds
    22  }
    23  
    24  func (ds ContextData) MergeFrom(other ContextData) ContextData {
    25  	for k, v := range other {
    26  		ds[k] = v
    27  	}
    28  	return ds
    29  }
    30  
    31  const ContextDataKeySignedUser = "SignedUser"
    32  
    33  type contextDataKeyType struct{}
    34  
    35  var contextDataKey contextDataKeyType
    36  
    37  func WithContextData(c context.Context) context.Context {
    38  	return context.WithValue(c, contextDataKey, make(ContextData, 10))
    39  }
    40  
    41  func GetContextData(c context.Context) ContextData {
    42  	if ds, ok := c.Value(contextDataKey).(ContextData); ok {
    43  		return ds
    44  	}
    45  	return nil
    46  }
    47  
    48  func CommonTemplateContextData() ContextData {
    49  	return ContextData{
    50  		"IsLandingPageOrganizations": setting.LandingPageURL == setting.LandingPageOrganizations,
    51  
    52  		"ShowRegistrationButton":        setting.Service.ShowRegistrationButton,
    53  		"ShowMilestonesDashboardPage":   setting.Service.ShowMilestonesDashboardPage,
    54  		"ShowFooterVersion":             setting.Other.ShowFooterVersion,
    55  		"DisableDownloadSourceArchives": setting.Repository.DisableDownloadSourceArchives,
    56  
    57  		"EnableSwagger":      setting.API.EnableSwagger,
    58  		"EnableOpenIDSignIn": setting.Service.EnableOpenIDSignIn,
    59  		"PageStartTime":      time.Now(),
    60  
    61  		"RunModeIsProd": setting.IsProd,
    62  	}
    63  }