jpatters / static-comments

2 stars 0 forks source link

How to apply the Lmbda staticman? #1

Open zxdawn opened 1 year ago

zxdawn commented 1 year ago

Hi Jordan, thanks a lot for this repo. I have read your post and realize this is a good option because Heroku isn't free anymore.

Could you tell me how to use this repo? Should I fork it and then add which endpoint to POST in Hugo blog?

zxdawn commented 1 year ago

Ha, finally I deployed it successfully on AWS and have written the tutorial. I still missed last step ...

Is it correct to add a comment via this link?

https://****.execute-api.us-east-2.amazonaws.com/prod/****/v3/entry/github/<username>/<blog_repo>/main/comments

I got this error: Cannot POST ......

zxdawn commented 1 year ago

I suppose @VincentTam is also interested in this self-hosted staticman. The last question maybe easy for him.

Note that the root URL works well in the browser:

https://****.execute-api.us-east-2.amazonaws.com/prod/

Output: Hello from Staticman version 3.0.0!

When I copied the full API link:

https://****.execute-api.us-east-2.amazonaws.com/prod/some_number_here

Output: Cannot GET /some_number_here
jpatters commented 1 year ago

@zxdawn I guess I never wrote part 2. I am quite terrible at following through on my blogging goals. I guess it's perfect timing for another New Years resolution.

I just pushed up the rest of the code. You can see it here: https://github.com/jpatters/jpatters.dev/blob/master/layouts/_default/single.html#L38-L82. However it seems like it's not quite working properly. I'll try to get that fixed and update you here.

I'll try to get around to writing the second blog post sometime in the next two years as well.

zxdawn commented 1 year ago

@jpatters Thanks! It seems the link is 404.

Anyway, I have made some progresses yesterday. You can see here. Briefly, I switched to staticman v2 to make sure it works first. However, I tested it with several RSA_PRIVATE_KEY, but got error:

$ curl -d "fields[email]=foobar@gmail.com" -X POST  [your-staticman-url]/prod/v2/entry/[your-main-github-username]/[your-site-repo]/main/comments

{"success":false,"rawError":"Error: Error during decryption (probably incorrect key). Original error: Error: error:04099079:rsa routines:RSA_padding_check_PKCS1_OAEP_mgf1:oaep decoding error"}%

Hope it can be fixed ;) I will find some free time to dig more.

jpatters commented 1 year ago

Yeah, sorry. I forgot my blog repo was private. Here's the relevant code (note that by blog is hugo so your templates may vary):

<div id="comments">
  <form method="POST" action="<url to function>">
    <h3>Say Something</h3>
    <input name="options[redirect]" type="hidden" value="{{ .Permalink }}">
    <!-- e.g. "2016-01-02-this-is-a-post" -->
    <input name="options[slug]" type="hidden" value="{{ $slug }}">
    <input type="hidden" name="options[reCaptcha][siteKey]" value="<recaptcha site key>">
    <input type="hidden" name="options[reCaptcha][secret]" value="<recaptcha secret>">
    <input type="text" name="fields[name]" class="form-input" placeholder="Name">
    <input name="fields[email]" type="email" class="form-input" placeholder="Email">
    <textarea name="fields[body]" class="form-textarea" rows="8" placeholder="Message..."></textarea>
    <div class="g-recaptcha" data-sitekey="<recaptcha site key>"></div>
    <script src='https://www.google.com/recaptcha/api.js'></script>
    <button type="submit">Submit</button>
  </form>
  {{ $comments := readDir "data/comments" }}
  {{ $.Scratch.Add "hasComments" 0 }}
  {{ $.Scratch.Set "commentStripe" "even" }}

  <h3 class="title">
    <a href="#comments">Comments</a>
  </h3>
  {{ range $comments }}
    {{ if eq .Name $slug }}
      {{ $.Scratch.Add "hasComments" 1 }}
      {{ range $index, $comments := (index $.Site.Data.comments $slug ) }}
        <div id="comment-{{ ._id }}" class="post-comment stripe-{{ $.Scratch.Get "commentStripe"}}">
          <div class="post-comment-header">
            <img class="post-comment-avatar" src="https://www.gravatar.com/avatar/{{ .email }}?s=70&r=pg&d=identicon">
            <p class="post-comment-info"><strong>{{ .name }}</strong><br><span class="post-comment-date"><a href="#comment-{{._id}}">{{ dateFormat "Jan 2, 2006 at 3:04 pm" .date }}</a></span></p>
          </div>
          <div class="post-comment-body">{{ .body | markdownify }}</div>
        </div>
        {{ if eq ($.Scratch.Get "commentStripe") "odd" }}
          {{ $.Scratch.Set "commentStripe" "even" }}
        {{ else }}
        {{ $.Scratch.Set "commentStripe" "odd" }}
        {{ end }}
      {{ end }}
    {{ end }}
  {{ end }}
  {{ if eq ($.Scratch.Get "hasComments") 0 }}
  <p>Be the first to comment...</p>
  {{ end }}
</div>
jpatters commented 1 year ago

And it is all working now. I had just deleted the function from my aws account.

zxdawn commented 1 year ago

@jpatters Thanks, I see it works well on your blog now!

I realize that static-comments repo was set on my robot GitHub account. Do I have to deploy it using my main GitHub account (which also hosts the blog)?

BTW, for the secrets, I set them in Settings -> Secrets -> Actions -> New repository. 1) STATICMAN_RSA_PRIVATE_KEY is set by ssh-keygen -t rsa -f staticman.key -m PEM and cat staticman.key | tr -d '\n'. Is tr -d '\n' necessary? How did you copy the key to GitHub console? 2) STATICMAN_GITHUB_TOKEN is the personal token of my robot account. Is everything all right?

Sorry for so many questions ... It seems one mistake can ruin the whole thing, hah.

jpatters commented 1 year ago

It shouldn't need to deploy from the same account as long as the token you use has access to your blog repo. The tr -d '\n' shouldn't be necessary and, in fact, could be causing the issue you mentioned above. Typically a new line is expected at the end of a private key file.

zxdawn commented 1 year ago

Ha, I recreated the function and keys, but still got the same error ... Did you use this repo for deploying the function?

I realize you update node to version 16 and keep serverless as the old version. Actually, the warning of using node12 still comes up in AWS Lambda.

jpatters commented 1 year ago

Yes. It is being deployed from GitHub actions using the workflow in this repo. I'm away from my computer for the holidays but would be happy to help you troubleshoot further in the new year.

zxdawn commented 1 year ago

Thanks a lot, Jordan! Have a nice holiday. I will test it again later.