PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label segment. Show all posts
Showing posts with label segment. Show all posts

Wednesday, September 14, 2022

[FIXED] How to route the first URI segment using NGINX as a reverse-proxy?

 September 14, 2022     nginx, nginx-reverse-proxy, routes, segment, uri     No comments   

Issue

I'm trying to construct an architecture/infrastructure on Amazon Web Services. Today I have an EC2 working like a gateway, with NGINX on the background. Btw, I'm new with NGINX.

The last week I had this NGINX config file:

server {
    listen 80;

    # I put * for hide the real domain name
    server_name ******.com.ar www.******.com.ar;

    location / {
         proxy_set_header   X-Forwarded-For $remote_addr;
         proxy_set_header   Host $http_host;
         proxy_pass         "http://private.ip.1:80/";
    }
}

And it worked great! When I go to www.domain.com.ar, I get a redirect to private ip 1 on port 80.

But, nowdays, I need to adjust the config file a bit.

1) First, I need to redirect some known paths to private ip 1 (ex. /company, /portfolio, /services, /contact and subsequences: /company/ourvision, /services/software, /contact/workwithus). I'm using NodeJS, not PHP.

2) And if none of the before paths get a match, I need to get the first URI segment as a wildcard (ex. http://domain.com.ar/*) matching only this characters: A-z0-9 and send to private ip 2 on port 3000, also I need sending the wildcard word too (ex. http://private.ip.2:3000/wildcard-word)

I was only trying to success on my second point, but I couldn't deal with it.

server {
    listen 80;
    server_name ******.com.ar www.******.com.ar;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://private.ip.1:80/";
    }

    location ~ ^/(.*)/?$ { 
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         "http://private.ip.2:3000/$1";
    }
}

But this isn't working. When I go to http://example.com.ar I go directly to the private ip 2 on port 3000. Btw, in another scenario, I get the follow error when I use the nginx -t command: "proxy_pass" cannot have URI part in location given by regular expression, or inside named location

So, can anyone help this noob to solve his problem? Thanks in advance. I will let the links that I was reading below:

  • nginx rule for wildcard url
  • nginx location based on uri path
  • How to do URL Redirects with Nginx
  • How to rewrite URI nginx reverse proxy using proxy_pass?
  • Understanding Nginx Server and Location Blocks (DigitalOcean)

Solution

You want to use the location / { ... } block as your wildcard, as it matches any URI that will not match another location block.

You do not need a URI component in the proxy_pass statement, as you are not modifying the URI before sending it upstream. See this document for details.

You can save on typing by specifying the proxy_set_header statements within the outer block and allowing them to be inherited by both location blocks. See this document for details.

For example:

proxy_set_header   X-Forwarded-For $remote_addr;
proxy_set_header   Host $http_host;

location ~ ^/(company|portfolio|services|contact|)(/|$) {
    proxy_pass         "http://private.ip.1:80";
}

location / {
    proxy_pass         "http://private.ip.2:3000";
}


Answered By - Richard Smith
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, August 20, 2022

[FIXED] How to access .env variables in a Nuxt plugin?

 August 20, 2022     analytics, environment-variables, nuxt.js, segment     No comments   

Issue

Segment Analytics provides a snippet with a secret API key in it. In my Nuxt.js project I created a plugin called segment.js which I registered in my nuxt.config.js:

nuxt.config.js

plugins: [
  {
    src: "~/plugins/segment.js",
    mode: 'client'
  }
]

In my plugins/segment.js file I have my snippet:

!function(){var analytics=window.analytics=...analytics.SNIPPET_VERSION="4.13.2";
analytics.load(process.env.SEGMENT_API_SECRET);
analytics.page();
}}();

Obviously I don't want to have my secret API key exposed there so I have it stored in my .env file instead:

.env

SEGMENT_API_SECRET=FR4....GSDF3S

Problem: process.env.SEGMENT_API_SECRET in plugins/segment.js is undefined so the snippet doesn't work. How can I access my .env variable SEGMENT_API_SECRET from my plugin plugins/segment.js?


Solution

Set your env variable into nuxt.config.js

export default {
  publicRuntimeConfig: {
    segmentApiSecret: process.env.SEGMENT_API_SECRET,
  }
}

And then, this one should do the trick

// segment.js
export default ({ $config: { segmentApiSecret } }) => {
  !function(){var analytics=window.analytics=...analytics.SNIPPET_VERSION="4.13.2";
  analytics.load(segmentApiSecret);
  analytics.page();
  }}();
}

UPDATE: A more in-depth answer of mine can be found here too.



Answered By - kissu
Answer Checked By - Robin (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, July 11, 2022

[FIXED] How to parse DFT_P03 message with ZPM segment

 July 11, 2022     hapi, java, message, parsing, segment     No comments   

Issue

I am coding a server application that will receive DFT_P03 messages with an added ZPM segment (which i have created a class for as per the HAPI documentation). Currently i am able to access this field as a generic segment when doing the following :

@Override
public Message processMessage(Message t, Map map) throws ReceivingApplicationException, HL7Exception 
{
    String encodedMessage = new DefaultHapiContext().getPipeParser().encode(t);

    logEntryService.logDebug(LogEntry.CONNECTIVITY, "Received message:\n" + encodedMessage + "\n\n");

    try 
    {
        InboundMessage inboundMessage = new InboundMessage();
        inboundMessage.setMessageTime(new Date());
        inboundMessage.setMessageType("Usage");

        DFT_P03 usageMessage = (DFT_P03) t;
        Segment ZPMSegment = (Segment)usageMessage.get("ZPM");

        inboundMessage.setMessage(usageMessage.toString());
        Facility facility = facilityService.findByCode(usageMessage.getMSH().getReceivingFacility().getNamespaceID().getValue());
        inboundMessage.setTargetFacility(facility);
        String controlID = usageMessage.getMSH().getMessageControlID().encode();
        controlID = controlID.substring(controlID.indexOf("^") + 1, controlID.length());
        inboundMessage.setControlId(controlID);

        Message response;

        try
        {
            inboundMessageService.save(inboundMessage);
            response = t.generateACK();
            logEntryService.logDebug(LogEntry.CONNECTIVITY, "Message ACKed");
        }
        catch (Exception ex)
        {
            response = t.generateACK(AcknowledgmentCode.AE, new HL7Exception(ex));
            logEntryService.logDebug(LogEntry.CONNECTIVITY, "Message NACKed");
        }

        return response;
    } 
    catch (IOException e) 
    {
        logEntryService.logDebug(LogEntry.CONNECTIVITY, "Message rejected");

        throw new HL7Exception(e);
    }
}

I have created a DFT_P03_Custom class as following :

public class DFT_P03_Custom extends DFT_P03
{
    public DFT_P03_Custom() throws HL7Exception
    {
        this(new DefaultModelClassFactory());
    }

    public DFT_P03_Custom(ModelClassFactory factory) throws HL7Exception
    {
        super(factory);
        String[] segmentNames = getNames();
        int indexOfPid = Arrays.asList(segmentNames).indexOf("FT1");
        int index = indexOfPid + 1;
        Class<ZPM> type = ZPM.class;
        boolean required = true;
        boolean repeating = false;
        this.add(type, required, repeating, index);
    }

    public ZPM getZPM()
    {
        return getTyped("ZPM", ZPM.class);
    }
}

When trying to typecast the message to a DFT_P03_Custom instance i get a ClassCastException. As per their documentation, i did create the CustomModelClassFactory class but using this i just get tons of validation errors on the controlId field.

I am already using an identical logic to send custom MFN_M01 messages with an added ZFX segment and that works flawlessly. I understand there is some automatic typecasting being done by HAPI when it receives a DFT_P03 message and that is likely what i need to somehow override for it to be able to give me a DFT_P03_Custom instance instead.

If you have some insight on how i can achieve this without having to use a generic segment instance please help!

Thank you!


Solution

I finally figured this out. The only way i got this to work was to generate a conformance profile XML file (using an example message from our application as a base) with the messaging workbench on the HAPI site and use the maven plugin to generate the message and segment classes. Only with these classes am i able to correctly parse a message to my custom class. One thing to note is that it DOES NOT work if i try to use the MSH, PID, PV1 or FT1 classes provided by HAPI and use my Z-segment class. It only works if all the segments are the classes generated by the conformance plugin. This combined with a CustomModelClassFactory class (as shown on the HAPI website) and the proper package structure finally allowed me to access my Z-segment.



Answered By - Martin
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Wednesday, May 18, 2022

[FIXED] How to apply a function (some processing steps) only on a specific part of an image in MATLAB?

 May 18, 2022     apply, image, matlab, partial, segment     No comments   

Issue

I have an image X(m by n) and it is sent to a segmentation process. The new image, Y(m by n), contains 9 segments. However, the segments are some random shapes (I mean, they are not square or rectangular).

Now I need to apply other algorithms separately on each segment. Noting that the segments are non-square or non-rectangular, I cannot use techniques such as BlockProc.

How can I read each segment separately and then apply a specific process only on that segment? Thank you very much.


Solution

You can use

Props=regionprops(YourBinaryImage, 'Image');

Then retrieve a cell of the individual objects with Props.Image Then apply the function of your choice to each object using cellfun, for example.

Hope it helps.



Answered By - Cape Code
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing