SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 13/07/2018, 08:56 AM   #1
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,943
Reputation: 1468
Default Environment Variables

pawn-env



Provides access to environment variables in Pawn.

Rule III of the Twelve Factor App states:

Store config in the environment

An app’s config is everything that is likely to vary between deploys (staging, production, developer environments, etc). This includes:
  • Resource handles to the database, Memcached, and other backing services
  • Credentials to external services such as Amazon S3 or *******
  • Per-deploy values such as the canonical hostname for the deploy

Apps sometimes store config as constants in the code. This is a violation of twelve-factor, which requires strict separation of config from code. Config varies substantially across deploys, code does not.

(read the rest here)

Installation

Simply install to your project:

Code:
sampctl package install Southclaws/pawn-env
Include in your code and begin using the library:

Code:
#include <env>
Not a sampctl user? See the GitHub for source and binaries.

Usage

This package provides one function GetEnv which stores into dest the environment variable identified by name and returns the length of the result.

For example, this code loads a MySQL password from an environment variable:

Code:
main() {
    new mysqlPassword[128];
    ret = GetEnv("MYSQL_PASSWORD", mysqlPassword);
    if(ret) {
        // use mysqlPassword to connect to DB
    } else {
        print("Environment variable `MYSQL_PASSWORD` not set.");
    }
}
Building

On Windows, to build the standard Windows .dll file: open CMakeLists.txt with Visual Studio 2017+ and develop/build using MSVC tools. To build the Linux build on Windows, run make build-linux and Docker will be used to compile it. On Linux, use CMake: mkdir build && cd build && cmake .. && make.

Testing

To test on Windows or Linux with the need for containers:

Code:
make test-windows
To the Linux build on Windows with a Docker container:

Code:
make test-linux
[HLF]Southclaw is offline   Reply With Quote
Old 13/07/2018, 09:13 AM   #2
Verc
Big Clucker
 
Verc's Avatar
 
Join Date: Apr 2018
Location: 🇯🇵
Posts: 180
Reputation: 29
Default Re: Environment Variables

What is this used for? Can you explain more?
Verc is offline   Reply With Quote
Old 13/07/2018, 09:15 AM   #3
coool
Gangsta
 
Join Date: Feb 2013
Location: Republic of Pakistan
Posts: 555
Reputation: 33
Default Re: Environment Variables

good.
__________________
coool is offline   Reply With Quote
Old 13/07/2018, 10:14 AM   #4
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,943
Reputation: 1468
Default Re: Environment Variables

Quote:
Originally Posted by Verc View Post
What is this used for? Can you explain more?
If you don't know what it is, you probably don't need it (seems like a running theme with my recent releases...)

Anyway, environment variables are basically standard in all languages. An environment variable is a configuration parameter specific to the machine running the code - not a file, not hard coded and not in a database. Those qualities are important for the reasons explained above.

So the prime use-case for this plugin is to provide a way of configuring gamemodes via the environment not via a file or a database - this is useful if you package your gamemode into a VM image or a Docker image - without environment variables, you'd need to either hard-code configuration into the image or mount a configuration file to the image at runtime, both of these are not really ideal options.
[HLF]Southclaw is offline   Reply With Quote
Old 13/07/2018, 10:48 AM   #5
SyS
High-roller
 
SyS's Avatar
 
Join Date: Oct 2015
Posts: 1,984
Reputation: 461
Default Re: Environment Variables

Nice
SyS is offline   Reply With Quote
Old 13/07/2018, 10:06 PM   #6
haoss
Little Clucker
 
Join Date: Jul 2013
Posts: 10
Reputation: 0
Default Re: Environment Variables

Thank you for another good release
haoss is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment for scripting?? YoussefHammad Scripting Help 4 17/03/2018 01:22 PM
Your programming environment? Luis- Everything and Nothing 22 28/10/2014 11:32 PM
Show your coding environment Smally Everything and Nothing 50 12/07/2014 10:57 AM
show environment/map of SA at onplayerrequestclass, how? niels44 Scripting Help 7 24/05/2012 05:34 PM
What environment do use use for Web designing SourceCode Everything and Nothing 20 12/01/2012 04:14 PM


All times are GMT. The time now is 10:22 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.